Skip to content

Commit

Permalink
Add activation redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
roshni73 authored and barshathakuri committed Nov 28, 2024
1 parent c5354c5 commit 88e6287
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/App/redirects/ActivationRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
generatePath,
useNavigate,
useParams,
} from 'react-router-dom';

import routes from '#routes';

interface ActivationParams {
userId: string | undefined;
token: string | undefined;
[key: string]: string | undefined;
}

// eslint-disable-next-line import/prefer-default-export
export function Component() {
const { userId, token } = useParams<ActivationParams>();
const navigate = useNavigate();

const activationLink = (userId && token && routes.activation.path) ? ({
pathname: (generatePath(routes.activation.path, { userId, token })),
})
: routes.pageNotFound.path;

if (activationLink) {
navigate(activationLink);
}

return null;
}

Component.displayName = 'ActivationRedirect';
35 changes: 32 additions & 3 deletions src/App/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const recoverAccount = customWrapRoute({
},
});

const resendValidationEmail = customWrapRoute({
/* const resendValidationEmail = customWrapRoute({
parent: rootLayout,
path: 'resend-validation-email',
component: {
Expand All @@ -267,7 +267,7 @@ const resendValidationEmail = customWrapRoute({
title: 'Resend Validation Email',
visibility: 'is-not-authenticated',
},
});
}); */

const cookiePolicy = customWrapRoute({
parent: rootLayout,
Expand Down Expand Up @@ -310,6 +310,33 @@ const resetPasswordRedirect = customWrapRoute({
visibility: 'is-not-authenticated',
},
});
const activation = customWrapRoute({
parent: rootLayout,
path: 'activation/:userId/:token',
component: {
render: () => import('#views/Activation'),
props: {},
},
wrapperComponent: Auth,
context: {
title: 'Activation',
visibility: 'anything',
},
});

const activationRedirect = customWrapRoute({
parent: rootLayout,
path: 'permalink/user-activation/:userId/:token',
component: {
render: () => import('../redirects/ActivationRedirect'),
props: {},
},
wrapperComponent: Auth,
context: {
title: 'Activation Redirect',
visibility: 'anything',
},
});

const wrappedRoutes = {
rootLayout,
Expand All @@ -325,14 +352,16 @@ const wrappedRoutes = {
pageNotFound,
login,
recoverAccount,
resendValidationEmail,
// resendValidationEmail,
mySubscription,
cookiePolicy,
register,
historicalAlerts,
subscriptionDetail,
recoverAccountConfirm,
resetPasswordRedirect,
activationRedirect,
activation,
};

export const unwrappedRoutes = unwrapRoute(Object.values(wrappedRoutes));
Expand Down
9 changes: 9 additions & 0 deletions src/views/Activation/i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"namespace": "activation",
"strings": {
"activationSuccessMessage":"Your account has been successfully activated!",
"goToLogin":"Go to Login",
"activationFailMessage":"An error occurred during activation. Please try again."
}
}

103 changes: 103 additions & 0 deletions src/views/Activation/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
useEffect,
useState,
} from 'react';
import { useParams } from 'react-router-dom';
import {
gql,
useMutation,
} from '@apollo/client';
import { Message } from '@ifrc-go/ui';
import { useTranslation } from '@ifrc-go/ui/hooks';

import Link from '#components/Link';
import Page from '#components/Page';
import useAlert from '#hooks/useAlert';

import i18n from './i18n.json';
import styles from './styles.module.css';

const ACCOUNT_ACTIVATION_MUTATION = gql`
mutation AccountActivation($data: UserActivationInput!) {
public {
accountActivation(data: $data) {
errors
ok
}
}
}
`;

// eslint-disable-next-line import/prefer-default-export
export function Component() {
const { userId, token } = useParams<{ userId?: string, token?: string }>();
const alert = useAlert();
const strings = useTranslation(i18n);
const [isErrored, setIsError] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const [
activate,
] = useMutation(ACCOUNT_ACTIVATION_MUTATION, {
onCompleted: (response) => {
const activateRes = response?.public?.accountActivation;
if (!response) {
return;
}
if (activateRes.ok) {
setIsSubmitted(true);
} else {
setIsError(true);
}
},
onError: () => {
alert.show(
strings.activationFailMessage,
{ variant: 'danger' },
);
},
});

useEffect(() => {
if (userId && token) {
activate({
variables: {
data: {
uuid: userId,
token,
},
},
});
}
}, [token, activate, userId]);

if (isSubmitted) {
return (
<Page>
<Message
title={strings.activationSuccessMessage}
/>
<div className={styles.activation}>
<Link
to="login"
variant="primary"
>
{strings.goToLogin}
</Link>
</div>

</Page>
);
}
if (isErrored) {
return (
<Page>
<Message
title={strings.activationFailMessage}
/>
</Page>
);
}
return null;
}

Component.displayName = 'Activation';
8 changes: 8 additions & 0 deletions src/views/Activation/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.activation {
display: flex;
flex-direction: column;
gap: var(--go-ui-spacing-xs);
align-items: center;
text-align: center;

}
6 changes: 3 additions & 3 deletions src/views/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function Component() {
{
signUpLink: (
<Link
to="login" // FIXME :add Register
to="register"
withUnderline
>
{strings.loginSignUp}
Expand Down Expand Up @@ -214,13 +214,13 @@ export function Component() {
>
{strings.loginForgotUserPass}
</Link>
<Link
{/* <Link
to="resendValidationEmail"
title={strings.loginResendValidationTitle}
withUnderline
>
{strings.loginResendValidation}
</Link>
</Link> */}
</div>
<div className={styles.actions}>
<Button
Expand Down

0 comments on commit 88e6287

Please sign in to comment.