Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RegisterForm.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { useState } from 'react';
  15. import { FORGOT_PASSWORD_PAGE, LOGIN_PAGE } from '../../../constants/pages';
  16. import { createUser } from '../../../requests/accountRequests';
  17. import { registerSchema } from '../../../schemas/registerSchema';
  18. import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
  19. interface FormValues {
  20. fullName: string;
  21. username: string;
  22. email: string;
  23. password: string;
  24. confirmPassword: string;
  25. }
  26. const RegisterForm = () => {
  27. const { t } = useTranslation(['forms', 'register']);
  28. const [showPassword, setShowPassword] = useState(false);
  29. const handleClickShowPassword = () => setShowPassword(!showPassword);
  30. const handleMouseDownPassword = () => setShowPassword(!showPassword);
  31. const [showConfirmPassword, setShowConfirmPassword] = useState(false);
  32. const handleClickShowConfirmPassword = () =>
  33. setShowConfirmPassword(!showConfirmPassword);
  34. const handleMouseDownConfirmPassword = () =>
  35. setShowConfirmPassword(!showConfirmPassword);
  36. const [error, setError] = useState({ hasError: false, errorMessage: '' });
  37. const submitHandler = async (values: FormValues) => {
  38. try {
  39. const result = await createUser(
  40. values.fullName,
  41. values.username,
  42. values.email,
  43. values.password
  44. );
  45. console.log(result);
  46. } catch (error) {
  47. if (error instanceof Error)
  48. setError({ hasError: true, errorMessage: error.message });
  49. }
  50. };
  51. const formik = useFormik({
  52. initialValues: {
  53. fullName: '',
  54. username: '',
  55. email: '',
  56. password: '',
  57. confirmPassword: '',
  58. },
  59. validationSchema: registerSchema,
  60. onSubmit: submitHandler,
  61. validateOnBlur: true,
  62. enableReinitialize: true,
  63. });
  64. return (
  65. <Container component="main" maxWidth="md">
  66. <Box
  67. sx={{
  68. marginTop: 10,
  69. display: 'flex',
  70. flexDirection: 'column',
  71. alignItems: 'center',
  72. }}
  73. >
  74. <Typography component="h1" variant="h5">
  75. {t('register:Title')}
  76. </Typography>
  77. {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
  78. <Box
  79. component="form"
  80. onSubmit={formik.handleSubmit}
  81. sx={{ position: 'relative', mt: 1, p: 1 }}
  82. >
  83. <TextField
  84. name="fullName"
  85. label={t('forms:FullName')}
  86. margin="normal"
  87. value={formik.values.fullName}
  88. onChange={formik.handleChange}
  89. error={formik.touched.fullName && Boolean(formik.errors.fullName)}
  90. helperText={formik.touched.fullName && formik.errors.fullName}
  91. autoFocus
  92. fullWidth
  93. />
  94. <TextField
  95. name="username"
  96. label={t('forms:Username')}
  97. margin="normal"
  98. value={formik.values.username}
  99. onChange={formik.handleChange}
  100. error={formik.touched.username && Boolean(formik.errors.username)}
  101. helperText={formik.touched.username && formik.errors.username}
  102. fullWidth
  103. />
  104. <TextField
  105. name="email"
  106. label={t('forms:Email')}
  107. margin="normal"
  108. value={formik.values.email}
  109. onChange={formik.handleChange}
  110. error={formik.touched.email && Boolean(formik.errors.email)}
  111. helperText={formik.touched.email && formik.errors.email}
  112. fullWidth
  113. />
  114. <TextField
  115. name="password"
  116. label={t('forms:Password')}
  117. margin="normal"
  118. type={showPassword ? 'text' : 'password'}
  119. value={formik.values.password}
  120. onChange={formik.handleChange}
  121. error={formik.touched.password && Boolean(formik.errors.password)}
  122. helperText={formik.touched.password && formik.errors.password}
  123. fullWidth
  124. InputProps={{
  125. endAdornment: (
  126. <InputAdornment position="end">
  127. <IconButton
  128. onClick={handleClickShowPassword}
  129. onMouseDown={handleMouseDownPassword}
  130. ></IconButton>
  131. </InputAdornment>
  132. ),
  133. }}
  134. />
  135. <TextField
  136. name="confirmPassword"
  137. label={t('forms:ConfirmPassword')}
  138. margin="normal"
  139. type={showPassword ? 'text' : 'password'}
  140. value={formik.values.confirmPassword}
  141. onChange={formik.handleChange}
  142. error={
  143. formik.touched.confirmPassword &&
  144. Boolean(formik.errors.confirmPassword)
  145. }
  146. helperText={
  147. formik.touched.confirmPassword && formik.errors.confirmPassword
  148. }
  149. fullWidth
  150. InputProps={{
  151. endAdornment: (
  152. <InputAdornment position="end">
  153. <IconButton
  154. onClick={handleClickShowConfirmPassword}
  155. onMouseDown={handleMouseDownConfirmPassword}
  156. ></IconButton>
  157. </InputAdornment>
  158. ),
  159. }}
  160. />
  161. <Button
  162. type="submit"
  163. variant="contained"
  164. sx={{ mt: 3, mb: 2 }}
  165. fullWidth
  166. >
  167. {t('register:RegisterBtn')}
  168. </Button>
  169. <Grid container>
  170. <Grid
  171. item
  172. xs={12}
  173. md={6}
  174. sx={{ textAlign: { xs: 'center', md: 'left' } }}
  175. >
  176. <Link href={FORGOT_PASSWORD_PAGE}>
  177. {t('register:ForgotPassword')}
  178. </Link>
  179. </Grid>
  180. <Grid
  181. item
  182. xs={12}
  183. md={6}
  184. sx={{ textAlign: { xs: 'center', md: 'right' } }}
  185. >
  186. <Link href={LOGIN_PAGE}>{t('register:HaveAccount')}</Link>
  187. </Grid>
  188. </Grid>
  189. </Box>
  190. </Box>
  191. </Container>
  192. );
  193. };
  194. export default RegisterForm;