|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { registerUser } from '../../utils/queries'; |
| 4 | +import { Link, Redirect } from 'react-router-dom'; |
| 5 | +import { Box, Button, TextField } from '@material-ui/core/'; |
| 6 | +import { useAuth } from './AuthContext'; |
| 7 | +import { validationResolver, defaultValues } from './SignUpForm.schema'; |
| 8 | +import { Form, Field } from '../form'; |
| 9 | + |
| 10 | +const Register = ({ toggleActiveForm }) => { |
| 11 | + const [errorMessage, setErrorMessage] = useState(null); |
| 12 | + const [isLoggedIn, setIsLoggedIn] = useState(false); |
| 13 | + const referer = '/profile'; |
| 14 | + const auth = useAuth(); |
| 15 | + |
| 16 | + const onSubmit = ({ username, email, password1, password2 }) => { |
| 17 | + const data = { |
| 18 | + username, |
| 19 | + password1, |
| 20 | + password2, |
| 21 | + email, |
| 22 | + }; |
| 23 | + console.log('ON SUBMIT FIRE'); |
| 24 | + console.log(data); |
| 25 | + registerUser(data); |
| 26 | + }; |
| 27 | + |
| 28 | + if (isLoggedIn) { |
| 29 | + return <Redirect to={referer} />; |
| 30 | + } |
| 31 | + |
| 32 | + if (auth && auth.authTokens) { |
| 33 | + return <p>Welcome!</p>; |
| 34 | + } |
| 35 | + |
| 36 | + return ( |
| 37 | + <Box |
| 38 | + component={Form} |
| 39 | + display="flex" |
| 40 | + flexWrap="wrap" |
| 41 | + noValidate |
| 42 | + autoComplete="off" |
| 43 | + onSubmit={onSubmit} |
| 44 | + data-testid="signupForm" |
| 45 | + validationResolver={validationResolver} |
| 46 | + defaultValues={defaultValues} |
| 47 | + > |
| 48 | + <Box component="h1" fontSize={18}> |
| 49 | + Create an account |
| 50 | + </Box> |
| 51 | + <Field |
| 52 | + as={TextField} |
| 53 | + fullWidth |
| 54 | + variant="outlined" |
| 55 | + margin="dense" |
| 56 | + name="username" |
| 57 | + label="Username*" |
| 58 | + id="username" |
| 59 | + /> |
| 60 | + <Field |
| 61 | + as={TextField} |
| 62 | + fullWidth |
| 63 | + variant="outlined" |
| 64 | + margin="dense" |
| 65 | + name="email" |
| 66 | + label="Email*" |
| 67 | + id="email" |
| 68 | + /> |
| 69 | + <Field |
| 70 | + as={TextField} |
| 71 | + fullWidth |
| 72 | + variant="outlined" |
| 73 | + margin="dense" |
| 74 | + name="password1" |
| 75 | + label="Password1*" |
| 76 | + type="text" |
| 77 | + id="password1" |
| 78 | + /> |
| 79 | + <Field |
| 80 | + as={TextField} |
| 81 | + fullWidth |
| 82 | + variant="outlined" |
| 83 | + margin="dense" |
| 84 | + name="password2" |
| 85 | + label="Password2*" |
| 86 | + type="text" |
| 87 | + id="password2" |
| 88 | + /> |
| 89 | + |
| 90 | + {errorMessage && <Box color="error.main"> {errorMessage}</Box>} |
| 91 | + |
| 92 | + <Box width="100%" marginTop={2}> |
| 93 | + <Button |
| 94 | + variant="contained" |
| 95 | + color="primary" |
| 96 | + type="submit" |
| 97 | + data-testid="submitButton" |
| 98 | + > |
| 99 | + Sign Up |
| 100 | + </Button> |
| 101 | + </Box> |
| 102 | + <p> |
| 103 | + Already have an account? |
| 104 | + {toggleActiveForm ? ( |
| 105 | + <Box |
| 106 | + component="button" |
| 107 | + color="primary.main" |
| 108 | + padding={0} |
| 109 | + marginLeft={1} |
| 110 | + border={0} |
| 111 | + bgcolor="transparent" |
| 112 | + fontSize={16} |
| 113 | + onClick={toggleActiveForm} |
| 114 | + > |
| 115 | + Log in |
| 116 | + </Box> |
| 117 | + ) : ( |
| 118 | + <Link to="/login"> Log in</Link> |
| 119 | + )} |
| 120 | + . |
| 121 | + </p> |
| 122 | + </Box> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +const { func } = PropTypes; |
| 127 | + |
| 128 | +Register.propTypes = { |
| 129 | + toggleActiveForm: func, |
| 130 | +}; |
| 131 | + |
| 132 | +export default Register; |
0 commit comments