| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import {
- Box,
- Button,
- Container,
- Grid,
- IconButton,
- InputAdornment,
- TextField,
- Typography,
- } from '@mui/material';
- import { useFormik } from 'formik';
- import Link from 'next/link';
- import { useState } from 'react';
-
- import { FORGOT_PASSWORD_PAGE, LOGIN_PAGE } from '../../../constants/pages';
- import { createUser } from '../../../requests/accountRequests';
- import { registerSchema } from '../../../schemas/registerSchema';
- import ErrorMessageComponent from '../../mui/ErrorMessageComponent';
-
- const RegisterForm = () => {
- const [showPassword, setShowPassword] = useState(false);
- const handleClickShowPassword = () => setShowPassword(!showPassword);
- const handleMouseDownPassword = () => setShowPassword(!showPassword);
-
- const [showConfirmPassword, setShowConfirmPassword] = useState(false);
- const handleClickShowConfirmPassword = () =>
- setShowConfirmPassword(!showConfirmPassword);
- const handleMouseDownConfirmPassword = () =>
- setShowConfirmPassword(!showConfirmPassword);
-
- const [error, setError] = useState({ hasError: false, errorMessage: '' });
-
- const submitHandler = async (values) => {
- try {
- const result = await createUser(
- values.fullName,
- values.username,
- values.email,
- values.password
- );
- console.log(result);
- } catch (error) {
- setError({ hasError: true, errorMessage: error.message });
- }
- };
-
- const formik = useFormik({
- initialValues: {
- fullName: '',
- username: '',
- email: '',
- password: '',
- confirmPassword: '',
- },
- validationSchema: registerSchema,
- onSubmit: submitHandler,
- validateOnBlur: true,
- enableReinitialize: true,
- });
-
- return (
- <Container component="main" maxWidth="md">
- <Box
- sx={{
- marginTop: 10,
- display: 'flex',
- flexDirection: 'column',
- alignItems: 'center',
- }}
- >
- <Typography component="h1" variant="h5">
- Register
- </Typography>
- {error.hasError && <ErrorMessageComponent error={error.errorMessage} />}
- <Box
- component="form"
- onSubmit={formik.handleSubmit}
- sx={{ position: 'relative', mt: 1, p: 1 }}
- >
- <TextField
- name="fullName"
- label="Full name"
- margin="normal"
- value={formik.values.fullName}
- onChange={formik.handleChange}
- error={formik.touched.fullName && Boolean(formik.errors.fullName)}
- helperText={formik.touched.fullName && formik.errors.fullName}
- autoFocus
- fullWidth
- />
- <TextField
- name="username"
- label="Username"
- margin="normal"
- value={formik.values.username}
- onChange={formik.handleChange}
- error={formik.touched.username && Boolean(formik.errors.username)}
- helperText={formik.touched.username && formik.errors.username}
- fullWidth
- />
- <TextField
- name="email"
- label="Email"
- margin="normal"
- value={formik.values.email}
- onChange={formik.handleChange}
- error={formik.touched.email && Boolean(formik.errors.email)}
- helperText={formik.touched.email && formik.errors.email}
- fullWidth
- />
- <TextField
- name="password"
- label="Password"
- margin="normal"
- type={showPassword ? 'text' : 'password'}
- value={formik.values.password}
- onChange={formik.handleChange}
- error={formik.touched.password && Boolean(formik.errors.password)}
- helperText={formik.touched.password && formik.errors.password}
- fullWidth
- InputProps={{
- endAdornment: (
- <InputAdornment position="end">
- <IconButton
- onClick={handleClickShowPassword}
- onMouseDown={handleMouseDownPassword}
- ></IconButton>
- </InputAdornment>
- ),
- }}
- />
- <TextField
- name="confirmPassword"
- label="Confirm password"
- margin="normal"
- type={showPassword ? 'text' : 'password'}
- value={formik.values.confirmPassword}
- onChange={formik.handleChange}
- error={
- formik.touched.confirmPassword &&
- Boolean(formik.errors.confirmPassword)
- }
- helperText={
- formik.touched.confirmPassword && formik.errors.confirmPassword
- }
- fullWidth
- InputProps={{
- endAdornment: (
- <InputAdornment position="end">
- <IconButton
- onClick={handleClickShowConfirmPassword}
- onMouseDown={handleMouseDownConfirmPassword}
- ></IconButton>
- </InputAdornment>
- ),
- }}
- />
- <Button
- type="submit"
- variant="contained"
- sx={{ mt: 3, mb: 2 }}
- fullWidth
- >
- Register
- </Button>
- <Grid container>
- <Grid
- item
- xs={12}
- md={6}
- sx={{ textAlign: { xs: 'center', md: 'left' } }}
- >
- <Link href={FORGOT_PASSWORD_PAGE}>Forgot your password?</Link>
- </Grid>
- <Grid
- item
- xs={12}
- md={6}
- sx={{ textAlign: { xs: 'center', md: 'right' } }}
- >
- <Link href={LOGIN_PAGE}>Already have an account?</Link>
- </Grid>
- </Grid>
- </Box>
- </Box>
- </Container>
- );
- };
-
- export default RegisterForm;
|