Skip to content

Commit

Permalink
Add activation redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
roshni73 committed Nov 26, 2024
1 parent 9c124bb commit 532c1cc
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 16 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';
57 changes: 43 additions & 14 deletions src/App/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,19 @@ const recoverAccount = customWrapRoute({
},
});

const resendValidationEmail = customWrapRoute({
parent: rootLayout,
path: 'resend-validation-email',
component: {
render: () => import('#views/ResendValidationEmail'),
props: {},
},
wrapperComponent: Auth,
context: {
title: 'Resend Validation Email',
visibility: 'is-not-authenticated',
},
});
// const resendValidationEmail = customWrapRoute({
// parent: rootLayout,
// path: 'resend-validation-email',
// component: {
// render: () => import('#views/ResendValidationEmail'),
// props: {},
// },
// wrapperComponent: Auth,
// context: {
// 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: 'is-not-authenticated',
},
});

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
8 changes: 8 additions & 0 deletions src/views/Activation/i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"namespace": "activation",
"strings": {
"activationSuccessMessage":"Activation successful!",
"activationFailMessage":"An error occurred during activation. Please try again."
}
}

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

import {
ActivationMutation,

Check failure on line 13 in src/views/Activation/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Module '"#generated/types/graphql"' has no exported member 'ActivationMutation'.

Check failure on line 13 in src/views/Activation/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

ActivationMutation not found in '#generated/types/graphql'
ActivationMutationVariables,

Check failure on line 14 in src/views/Activation/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

'"#generated/types/graphql"' has no exported member named 'ActivationMutationVariables'. Did you mean 'AccountActivationMutationVariables'?

Check failure on line 14 in src/views/Activation/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

ActivationMutationVariables not found in '#generated/types/graphql'
} from '#generated/types/graphql';
import useAlert from '#hooks/useAlert';

import i18n from './i18n.json';

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

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<ActivationParams>();
const navigate = useNavigate();
const alert = useAlert();
const strings = useTranslation(i18n);

const [
activate,
] = useMutation<ActivationMutation, ActivationMutationVariables>(ACCOUNT_ACTIVATION_MUTATION, {
onCompleted: (response) => {
const activateRes = response?.public?.accountActivation;
if (!activateRes) {
alert.show(
strings.activationFailMessage,
{ variant: 'danger' },
);
return;
}
if (activateRes.ok) {
alert.show(
strings.activationSuccessMessage,
{ variant: 'success' },
);
navigate('/login');
} else {
alert.show(
strings.activationFailMessage,
{ variant: 'danger' },
);
}
},
onError: () => {
alert.show(
strings.activationFailMessage,
{ variant: 'danger' },
);
navigate('/login');
},
});

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

return null;
}

Component.displayName = 'Activation';
4 changes: 2 additions & 2 deletions src/views/Login/index.tsx
Original file line number Diff line number Diff line change
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 532c1cc

Please sign in to comment.