Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RegisterForm.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import {
  2. Box,
  3. Button,
  4. Container,
  5. Grid,
  6. IconButton,
  7. InputAdornment,
  8. TextField,
  9. Typography,
  10. } from '@mui/material';
  11. import { useFormik } from 'formik';
  12. import { useTranslation } from 'next-i18next';
  13. import Link from 'next/link';
  14. import { useRouter } from 'next/router';
  15. import { useState } from 'react';
  16. import { FORGOT_PASSWORD_PAGE, LOGIN_PAGE } from '../../../constants/pages';
  17. import { createUser } from '../../../requests/accounts/accountRequests';
  18. import { registerSchema } from '../../../schemas/registerSchema';
  19. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  20. const RegisterForm = () => {
  21. const { t } = useTranslation('forms', 'register');
  22. const router = useRouter();
  23. const [showPassword, setShowPassword] = useState(false);
  24. const handleClickShowPassword = () => setShowPassword(!showPassword);
  25. const handleMouseDownPassword = () => setShowPassword(!showPassword);
  26. const [showConfirmPassword, setShowConfirmPassword] = useState(false);
  27. const handleClickShowConfirmPassword = () =>
  28. setShowConfirmPassword(!showConfirmPassword);
  29. const handleMouseDownConfirmPassword = () =>
  30. setShowConfirmPassword(!showConfirmPassword);
  31. const [error, setError] = useState({ hasError: false, errorMessage: '' });
  32. const submitHandler = async (values) => {
  33. try {
  34. const result = await createUser(
  35. values.fullName,
  36. values.username,
  37. values.email,
  38. values.password,
  39. values.address,
  40. values.address2,
  41. values.city,
  42. values.country,
  43. values.postcode
  44. );
  45. router.push(LOGIN_PAGE);
  46. } catch (error) {
  47. setError({ hasError: true, errorMessage: error.message });
  48. }
  49. };
  50. const formik = useFormik({
  51. initialValues: {
  52. fullName: '',
  53. username: '',
  54. email: '',
  55. password: '',
  56. confirmPassword: '',
  57. address: '',
  58. address2: '',
  59. city: '',
  60. country: '',
  61. postcode: '',
  62. },
  63. validationSchema: registerSchema,
  64. onSubmit: submitHandler,
  65. validateOnBlur: true,
  66. enableReinitialize: true,
  67. });
  68. return (
  69. <Container component="main" maxWidth="md">
  70. <Box
  71. sx={{
  72. marginTop: 10,
  73. display: 'flex',
  74. flexDirection: 'column',
  75. alignItems: 'center',
  76. }}
  77. >
  78. <Typography component="h1" variant="h5">
  79. {t('register:Title')}
  80. </Typography>
  81. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  82. <Box
  83. component="form"
  84. onSubmit={formik.handleSubmit}
  85. sx={{ position: 'relative', mt: 1, p: 1 }}
  86. >
  87. <TextField
  88. name="fullName"
  89. label={t('forms:FullName')}
  90. margin="normal"
  91. value={formik.values.fullName}
  92. onChange={formik.handleChange}
  93. error={formik.touched.fullName && Boolean(formik.errors.fullName)}
  94. helperText={formik.touched.fullName && formik.errors.fullName}
  95. autoFocus
  96. fullWidth
  97. />
  98. <TextField
  99. name="username"
  100. label={t('forms:Username')}
  101. margin="normal"
  102. value={formik.values.username}
  103. onChange={formik.handleChange}
  104. error={formik.touched.username && Boolean(formik.errors.username)}
  105. helperText={formik.touched.username && formik.errors.username}
  106. fullWidth
  107. />
  108. <TextField
  109. name="email"
  110. label={t('forms:Email')}
  111. margin="normal"
  112. value={formik.values.email}
  113. onChange={formik.handleChange}
  114. error={formik.touched.email && Boolean(formik.errors.email)}
  115. helperText={formik.touched.email && formik.errors.email}
  116. fullWidth
  117. />
  118. <TextField
  119. name="password"
  120. label={t('forms:Password')}
  121. margin="normal"
  122. type={showPassword ? 'text' : 'password'}
  123. value={formik.values.password}
  124. onChange={formik.handleChange}
  125. error={formik.touched.password && Boolean(formik.errors.password)}
  126. helperText={formik.touched.password && formik.errors.password}
  127. fullWidth
  128. InputProps={{
  129. endAdornment: (
  130. <InputAdornment position="end">
  131. <IconButton
  132. onClick={handleClickShowPassword}
  133. onMouseDown={handleMouseDownPassword}
  134. ></IconButton>
  135. </InputAdornment>
  136. ),
  137. }}
  138. />
  139. <TextField
  140. name="confirmPassword"
  141. label={t('forms:ConfirmPassword')}
  142. margin="normal"
  143. type={showPassword ? 'text' : 'password'}
  144. value={formik.values.confirmPassword}
  145. onChange={formik.handleChange}
  146. error={
  147. formik.touched.confirmPassword &&
  148. Boolean(formik.errors.confirmPassword)
  149. }
  150. helperText={
  151. formik.touched.confirmPassword && formik.errors.confirmPassword
  152. }
  153. fullWidth
  154. InputProps={{
  155. endAdornment: (
  156. <InputAdornment position="end">
  157. <IconButton
  158. onClick={handleClickShowConfirmPassword}
  159. onMouseDown={handleMouseDownConfirmPassword}
  160. ></IconButton>
  161. </InputAdornment>
  162. ),
  163. }}
  164. />
  165. <TextField
  166. name="address"
  167. label="Address"
  168. margin="normal"
  169. value={formik.values.address}
  170. onChange={formik.handleChange}
  171. error={formik.touched.address && Boolean(formik.errors.address)}
  172. helperText={formik.touched.address && formik.errors.address}
  173. fullWidth
  174. />
  175. <TextField
  176. name="address"
  177. label="Address2"
  178. margin="normal"
  179. value={formik.values.address2}
  180. onChange={formik.handleChange}
  181. error={formik.touched.address2 && Boolean(formik.errors.address2)}
  182. helperText={formik.touched.address2 && formik.errors.address2}
  183. fullWidth
  184. />
  185. <TextField
  186. name="city"
  187. label="City"
  188. margin="normal"
  189. value={formik.values.city}
  190. onChange={formik.handleChange}
  191. error={formik.touched.city && Boolean(formik.errors.city)}
  192. helperText={formik.touched.city && formik.errors.city}
  193. fullWidth
  194. />
  195. <TextField
  196. name="country"
  197. label="Country"
  198. margin="normal"
  199. value={formik.values.country}
  200. onChange={formik.handleChange}
  201. error={formik.touched.country && Boolean(formik.errors.country)}
  202. helperText={formik.touched.country && formik.errors.country}
  203. fullWidth
  204. />
  205. <TextField
  206. name="postcode"
  207. label="Postal Code"
  208. margin="normal"
  209. value={formik.values.postcode}
  210. onChange={formik.handleChange}
  211. error={formik.touched.postcode && Boolean(formik.errors.postcode)}
  212. helperText={formik.touched.postcode && formik.errors.postcode}
  213. fullWidth
  214. />
  215. <Button
  216. type="submit"
  217. variant="contained"
  218. sx={{ mt: 3, mb: 2 }}
  219. fullWidth
  220. >
  221. {t('register:RegisterBtn')}
  222. </Button>
  223. <Grid container>
  224. <Grid
  225. item
  226. xs={12}
  227. md={6}
  228. sx={{ textAlign: { xs: 'center', md: 'left' }, mt: 1 }}
  229. >
  230. <Link href={FORGOT_PASSWORD_PAGE}>
  231. <Typography sx={{ cursor: 'pointer' }}>
  232. {t('register:ForgotPassword')}
  233. </Typography>
  234. </Link>
  235. </Grid>
  236. <Grid
  237. item
  238. xs={12}
  239. md={6}
  240. sx={{ textAlign: { xs: 'center', md: 'right' }, mt: 1 }}
  241. >
  242. <Link href={LOGIN_PAGE}>
  243. <Typography sx={{ cursor: 'pointer' }}>
  244. {t('register:HaveAccount')}
  245. </Typography>
  246. </Link>
  247. </Grid>
  248. </Grid>
  249. </Box>
  250. </Box>
  251. </Container>
  252. );
  253. };
  254. export default RegisterForm;