-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aliwoto <[email protected]>
- Loading branch information
Showing
7 changed files
with
188 additions
and
5 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
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
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,131 @@ | ||
import { useState } from 'react'; | ||
import { CircularProgress, Container, Paper, Box, Typography, Grid, TextField, Button } from '@mui/material'; | ||
import apiClient from '../apiClient'; | ||
import { ConfirmAccountData } from '../api'; | ||
import DashboardContainer from '../components/containers/dashboardContainer'; | ||
import CheckCircleIcon from '@mui/icons-material/CheckCircle'; | ||
import { CurrentAppTranslation } from '../translations/appTranslation'; | ||
import useAppSnackbar from '../components/snackbars/useAppSnackbars'; | ||
import { extractErrorDetails } from '../utils/errorUtils'; | ||
import { getTextInputTypeFromFieldName } from '../utils/textUtils'; | ||
|
||
interface ConfirmationRequiredFields { | ||
user_id: string; | ||
new_password: string; | ||
repeat_password: string; | ||
} | ||
|
||
const ConfirmAccountRedirectPage = () => { | ||
apiClient.clearTokens(); | ||
const urlSearch = new URLSearchParams(window.location.search); | ||
|
||
const [requiredData, setRequiredData] = useState<ConfirmationRequiredFields>({ | ||
user_id: '', | ||
new_password: '', | ||
repeat_password: '', | ||
}); | ||
const [confirmData, setConfirmData] = useState<ConfirmAccountData>({ | ||
user_id: '', | ||
confirm_token: urlSearch.get('confirmToken') ?? '', | ||
rl_token: urlSearch.get('rlToken') ?? '', | ||
lt_token: urlSearch.get('lt') ?? '', | ||
raw_password: '', | ||
}); | ||
const [isConfirmed, setIsConfirmed] = useState(false); | ||
|
||
const snackbar = useAppSnackbar(); | ||
|
||
const handleChange = (e: any) => { | ||
setRequiredData({ ...requiredData, [e.target.name]: e.target.value }); | ||
setConfirmData({ | ||
...confirmData, | ||
[e.target.name]: e.target.value, | ||
}); | ||
}; | ||
|
||
const handleConfirm = async () => { | ||
try { | ||
confirmData.raw_password = requiredData.new_password; | ||
const result = await apiClient.confirmAccount(confirmData); | ||
if (!result) { | ||
snackbar.error(CurrentAppTranslation.FailedToConfirmAccountCreationText); | ||
return; | ||
} | ||
|
||
setIsConfirmed(true); | ||
// Redirect the user to /login page in 3 seconds | ||
setTimeout(() => { | ||
window.location.href = '/login'; | ||
}, 3000); | ||
} catch (error: any) { | ||
const [errCode, errMessage] = extractErrorDetails(error); | ||
snackbar.error(`Failed (${errCode}): ${errMessage}`); | ||
return; | ||
} | ||
|
||
}; | ||
|
||
if (confirmData.confirm_token === '' || | ||
confirmData.rl_token === '' || | ||
confirmData.lt_token === '') { | ||
window.location.href = '/login'; | ||
return ( | ||
<CircularProgress /> | ||
); | ||
} | ||
|
||
return ( | ||
<DashboardContainer disableSlideMenu={true}> | ||
<Container maxWidth="sm"> | ||
<Paper elevation={3} sx={{ p: 3, mt: 6 }}> | ||
<Box display="flex" justifyContent="space-between" alignItems="center" mb={2}> | ||
<Typography variant="h4">{CurrentAppTranslation.ConfirmYourAccountText}</Typography> | ||
</Box> | ||
<Grid container spacing={2}> | ||
{Object.keys(requiredData).map((field) => ( | ||
<Grid item xs={12} key={field}> | ||
{!isConfirmed && ( | ||
<TextField | ||
fullWidth | ||
type={getTextInputTypeFromFieldName(field)} | ||
name={field} | ||
label={CurrentAppTranslation[field as keyof (typeof CurrentAppTranslation)]} | ||
value={requiredData[field as keyof (typeof requiredData)]} | ||
onChange={handleChange} | ||
/> | ||
)} | ||
</Grid> | ||
))} | ||
{isConfirmed && ( | ||
<Grid item xs={12} sx={{ display: 'flex', justifyContent: 'center' }}> | ||
<CheckCircleIcon color="success" sx={{ fontSize: 30 }} /> | ||
<Typography variant="body2" sx={{ color: 'green' }}> | ||
{CurrentAppTranslation.ConfirmationSuccessText} | ||
</Typography> | ||
</Grid> | ||
)} | ||
{(requiredData.new_password !== requiredData.repeat_password) ? ( | ||
<Grid item xs={12} sx={{ display: 'flex', justifyContent: 'center' }}> | ||
<Typography variant="body2" sx={{ color: 'red' }}> | ||
{CurrentAppTranslation.PasswordsDoNotMatchText} | ||
</Typography> | ||
</Grid> | ||
) : null} | ||
<Grid item xs={12} sx={{ display: 'flex', justifyContent: 'center' }}> | ||
<Button variant="contained" onClick={handleConfirm} style={ | ||
{ | ||
display: isConfirmed ? 'none' : 'block', | ||
backgroundColor: requiredData.new_password === requiredData.repeat_password ? 'green' : 'red', | ||
} | ||
}> | ||
{CurrentAppTranslation.ConfirmText} | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
</Paper> | ||
</Container> | ||
</DashboardContainer> | ||
); | ||
}; | ||
|
||
export default ConfirmAccountRedirectPage; |
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
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,18 @@ | ||
|
||
|
||
|
||
export function getTextInputTypeFromFieldName(fieldName: string): React.HTMLInputTypeAttribute { | ||
if (fieldName.toLowerCase().includes("password")) { | ||
return "password"; | ||
} | ||
|
||
if (fieldName.toLowerCase().includes("email")) { | ||
return "email"; | ||
} | ||
|
||
if (fieldName.toLowerCase().includes("phone")) { | ||
return "tel"; | ||
} | ||
|
||
return "text"; | ||
} |