|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import { useNavigate, useSearchParams } from 'react-router-dom' |
| 3 | + |
| 4 | +// material-ui |
| 5 | +import { Stack, Typography, Box, useTheme, CircularProgress } from '@mui/material' |
| 6 | + |
| 7 | +// project imports |
| 8 | +import MainCard from '@/ui-component/cards/MainCard' |
| 9 | + |
| 10 | +// API |
| 11 | +import accountApi from '@/api/account.api' |
| 12 | + |
| 13 | +// Hooks |
| 14 | +import useApi from '@/hooks/useApi' |
| 15 | + |
| 16 | +// icons |
| 17 | +import { IconCheck, IconX } from '@tabler/icons-react' |
| 18 | + |
| 19 | +const ConfirmEmailChange = () => { |
| 20 | + const confirmApi = useApi(accountApi.confirmEmailChange) |
| 21 | + |
| 22 | + const [searchParams] = useSearchParams() |
| 23 | + const [loading, setLoading] = useState(false) |
| 24 | + const [errorMessage, setErrorMessage] = useState('') |
| 25 | + const [success, setSuccess] = useState(false) |
| 26 | + const navigate = useNavigate() |
| 27 | + |
| 28 | + const theme = useTheme() |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (confirmApi.data) { |
| 32 | + setLoading(false) |
| 33 | + setErrorMessage('') |
| 34 | + setSuccess(true) |
| 35 | + setTimeout(() => { |
| 36 | + navigate('/signin') |
| 37 | + }, 3000) |
| 38 | + } |
| 39 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 40 | + }, [confirmApi.data]) |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (confirmApi.error) { |
| 44 | + setLoading(false) |
| 45 | + setErrorMessage(confirmApi.error) |
| 46 | + setSuccess(false) |
| 47 | + } |
| 48 | + }, [confirmApi.error]) |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + const token = searchParams.get('token') |
| 52 | + if (token) { |
| 53 | + setLoading(true) |
| 54 | + setErrorMessage('') |
| 55 | + setSuccess(false) |
| 56 | + confirmApi.request({ user: { tempToken: token } }) |
| 57 | + } |
| 58 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 59 | + }, []) |
| 60 | + |
| 61 | + return ( |
| 62 | + <MainCard> |
| 63 | + <Stack flexDirection='column' sx={{ width: '480px', gap: 3 }}> |
| 64 | + <Stack sx={{ width: '100%', alignItems: 'center', justifyContent: 'center', gap: 4 }}> |
| 65 | + <Stack sx={{ alignItems: 'center', gap: 2 }}> |
| 66 | + {loading && ( |
| 67 | + <> |
| 68 | + <CircularProgress |
| 69 | + sx={{ |
| 70 | + width: '48px', |
| 71 | + height: '48px' |
| 72 | + }} |
| 73 | + /> |
| 74 | + <Typography variant='h1'>Confirming email change...</Typography> |
| 75 | + </> |
| 76 | + )} |
| 77 | + {errorMessage && ( |
| 78 | + <> |
| 79 | + <Box |
| 80 | + sx={{ |
| 81 | + width: '48px', |
| 82 | + height: '48px', |
| 83 | + borderRadius: '100%', |
| 84 | + backgroundColor: theme.palette.error.main, |
| 85 | + color: 'white', |
| 86 | + display: 'flex', |
| 87 | + alignItems: 'center', |
| 88 | + justifyContent: 'center' |
| 89 | + }} |
| 90 | + > |
| 91 | + <IconX /> |
| 92 | + </Box> |
| 93 | + <Typography variant='h1'>Confirmation failed.</Typography> |
| 94 | + <Typography variant='body2' color='textSecondary' sx={{ textAlign: 'center' }}> |
| 95 | + {errorMessage} |
| 96 | + </Typography> |
| 97 | + </> |
| 98 | + )} |
| 99 | + {success && ( |
| 100 | + <> |
| 101 | + <Box |
| 102 | + sx={{ |
| 103 | + width: '48px', |
| 104 | + height: '48px', |
| 105 | + borderRadius: '100%', |
| 106 | + backgroundColor: theme.palette.success.main, |
| 107 | + color: 'white', |
| 108 | + display: 'flex', |
| 109 | + alignItems: 'center', |
| 110 | + justifyContent: 'center' |
| 111 | + }} |
| 112 | + > |
| 113 | + <IconCheck /> |
| 114 | + </Box> |
| 115 | + <Typography variant='h1'>Email updated successfully.</Typography> |
| 116 | + <Typography variant='body2' color='textSecondary' sx={{ textAlign: 'center' }}> |
| 117 | + Please sign in with your new email address. |
| 118 | + </Typography> |
| 119 | + </> |
| 120 | + )} |
| 121 | + </Stack> |
| 122 | + </Stack> |
| 123 | + </Stack> |
| 124 | + </MainCard> |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +export default ConfirmEmailChange |
0 commit comments