Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegisterForm.jsx 5.8KB

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