LocChat/client/components/sign_up/_sign_up.jsx

88 lines
2.9 KiB
React
Raw Normal View History

2021-11-22 16:21:53 -05:00
import { useContext, useState } from 'react';
import { useNavigate } from 'react-router';
2021-11-23 16:04:12 -05:00
import { AuthContext } from '../../utils/auth_context';
2021-12-27 12:39:59 -05:00
import { ApiContext } from '../../utils/api_context';
2021-11-23 16:04:12 -05:00
import { Paper } from '../common/paper';
import { Input } from '../common/input';
import { Button } from '../common/button';
2021-11-20 21:34:10 -05:00
export const SignUp = () => {
2021-11-23 16:04:12 -05:00
const [, setAuthToken] = useContext(AuthContext);
2021-12-27 12:39:59 -05:00
const api = useContext(ApiContext);
2021-11-22 16:21:53 -05:00
const navigate = useNavigate();
2021-12-27 12:39:59 -05:00
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
2021-11-20 21:34:10 -05:00
const [email, setEmail] = useState('');
const [emailConfirmation, setEmailConfirmation] = useState('');
const [password, setPassword] = useState('');
2021-12-27 12:39:59 -05:00
const [passwordConfirmation, setPasswordConfirmation] = useState('');
2021-11-20 21:34:10 -05:00
const [errorMessage, setErrorMessage] = useState('');
const signUp = async () => {
if (email === '') {
setErrorMessage('Email cannot be blank');
return;
}
if (email !== emailConfirmation) {
setErrorMessage('Email does not match.');
return;
}
if (password === '') {
setErrorMessage('Password cannot be blank');
return;
}
2021-12-27 12:39:59 -05:00
if (password !== passwordConfirmation) {
2021-11-20 21:34:10 -05:00
setErrorMessage('Password does not match');
return;
}
2021-12-27 12:39:59 -05:00
if (firstName === '') {
setErrorMessage('First name cannot be blank.');
2021-11-20 21:34:10 -05:00
return;
}
2021-12-27 12:39:59 -05:00
if (lastName === '') {
setErrorMessage('Last name cannot be blank.');
return;
2021-11-20 21:34:10 -05:00
}
2021-12-27 12:39:59 -05:00
const { token } = await api.post('/users', {
firstName,
lastName,
email,
password,
});
setAuthToken(token);
navigate('/');
2021-11-20 21:34:10 -05:00
};
return (
2021-11-23 16:04:12 -05:00
<div className="flex flex-row justify-center m-4">
<div className="w-96">
<Paper>
2021-12-27 12:39:59 -05:00
<div>First Name</div>
<Input type="text" value={firstName} onChange={(e) => setFirstName(e.target.value)} />
<div>Last Name</div>
<Input type="text" value={lastName} onChange={(e) => setLastName(e.target.value)} />
2021-11-23 16:04:12 -05:00
<div>Email</div>
<Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
<div>Confirm Email</div>
<Input type="email" value={emailConfirmation} onChange={(e) => setEmailConfirmation(e.target.value)} />
<div>Password</div>
<Input type="password" value={password} onChange={(e) => setPassword(e.target.value)} />
<div>Confirm Password</div>
<Input
type="password"
2021-12-27 12:39:59 -05:00
value={passwordConfirmation}
2021-11-23 16:04:12 -05:00
onChange={(e) => setPasswordConfirmation(e.target.value)}
/>
<div className="flex flex-row justify-end mt-2">
<Button type="button" onClick={signUp}>
Sign up
</Button>
</div>
<div className="flex">{errorMessage}</div>
</Paper>
2021-11-20 21:34:10 -05:00
</div>
</div>
);
};