-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Typecheck
|
||
ActivationMutationVariables, | ||
Check failure on line 14 in src/views/Activation/index.tsx GitHub Actions / Typecheck
|
||
} 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters