Skip to content

Commit

Permalink
Add verify email feature (#730)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Marques <[email protected]>
  • Loading branch information
hugomarquesdev and Hugo Marques authored Jun 5, 2024
1 parent 123cccc commit 6a12f54
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ module.exports = {
'no-underscore-dangle': 'off',
'prefer-destructuring': 'off'
}
};
};
17 changes: 17 additions & 0 deletions public/img/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ export const userApi = emptySplitApi.injectEndpoints({
url: 'users'
}),
transformResponse: (response: { data: User }) => response.data
}),
// Verify Email
verifyEmail: builder.mutation<any, any>({
query: (body) => ({
body,
method: 'POST',
url: 'users/verify'
}),
transformResponse: (response: { data: any }) => response.data
})
})
});
Expand All @@ -183,5 +192,6 @@ export const {
useRecoverAccountMutation,
useUpdateNotificationsMutation,
useUpdateUserMutation,
useGetPreSignedMutation
useGetPreSignedMutation,
useVerifyEmailMutation
} = userApi;
5 changes: 3 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const apolloClient = new ApolloClient({
});

const InnerApp = (props: AppProps) => {
const { Component, pageProps } = props;
const { Component, pageProps, router } = props;
const { asPath } = router;

const { withPreview } = pageProps;
const { authorized, isLoading } = useGuard({ withPreview });
Expand Down Expand Up @@ -96,7 +97,7 @@ const InnerApp = (props: AppProps) => {

return (
<AppContainer>
<Sidebar />
{!asPath.includes('/verify-email') && <Sidebar />}
{isLoading ? (
<ViewContainer isLoading />
) : (
Expand Down
26 changes: 26 additions & 0 deletions src/pages/verify-email.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ClientConfig } from '@prismicio/client';
import { GetStaticProps } from 'next';
import Prismic from '../libs/Prismic/Prismic';
import VerifyEmail from 'src/views/VerifyEmail';

export const getStaticProps: GetStaticProps = async ({
locale: lang,
previewData
}) => {
const clientOptions = previewData as ClientConfig;
const data = await Prismic.getByTypes({
clientOptions,
lang,
types: 'pwa-view-beneficiary'
});

return {
props: {
data,
view: 'beneficiary',
withPreview: !!previewData
}
};
};

export default VerifyEmail;
3 changes: 2 additions & 1 deletion src/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const publicRoutes: Routes = [
'/microcredit/application',
'/microcredit/apply',
'/microcredit/form/[id]',
'/microcredit'
'/microcredit',
'/verify-email'
];

export const privateRoutes: Routes = ['/profile', '/settings', '/mycommunity'];
Expand Down
90 changes: 90 additions & 0 deletions src/views/VerifyEmail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Box, Button, Text, ViewContainer, toast } from '@impact-market/ui';
import { useVerifyEmailMutation } from 'src/api/user';
import CheckPermission from 'src/components/CheckPermission';
import React, { useEffect, useState } from 'react';
import useFilters from 'src/hooks/useFilters';

const VerifyEmail: React.FC<{ isLoading?: boolean }> = () => {
const { getByKey, update } = useFilters();
const [isLoading, setIsLoading] = useState(false);
const [emailConfirmed, setEmailConfirmed] = useState(false);
const [verifyEmail] = useVerifyEmailMutation();

useEffect(() => {
if (getByKey('verified')) {
setEmailConfirmed(true);
}
}, [getByKey('verified')]);

const confirmVerification = async () => {
try {
setIsLoading(true);

await verifyEmail({
code: getByKey('code'),
email: getByKey('email'),
userId: getByKey('userId')
}).unwrap();

update('verified', 1);
setIsLoading(false);
setEmailConfirmed(true);
} catch (error) {
console.log(error);
toast.error('Something went wrong. Please try again later.');
setIsLoading(false);
}
};

const showPage =
!!getByKey('code') && !!getByKey('email') && !!getByKey('userId');

if (!showPage) {
return <CheckPermission />;
}

return (
<ViewContainer {...({} as any)}>
<Box
column
fLayout="start"
flex
style={{ gap: '2rem', margin: '0 auto', maxWidth: '500px' }}
>
<img
alt="impactMarket logo"
src="/img/logo.svg"
width="125px"
/>
<div>
<Text
g900
semibold
style={{ fontSize: '1.5rem', lineHeight: '2rem' }}
>
{emailConfirmed
? 'Email Confirmed.'
: 'Confirm your email.'}
</Text>
<Text g500 large mt="0.5rem">
{emailConfirmed
? 'Thanks for confirming your email. You can now close this page and go back to claim your rewards.'
: 'In order to be able to claim your rewards, please confirm your email by clicking the button below.'}
</Text>
</div>
{!emailConfirmed && (
<Box flex>
<Button
isLoading={isLoading}
onClick={confirmVerification}
>
<Text>Confirm Email Address</Text>
</Button>
</Box>
)}
</Box>
</ViewContainer>
);
};

export default VerifyEmail;

0 comments on commit 6a12f54

Please sign in to comment.