import { useContext, useState } from 'react'; import { useNavigate } from 'react-router'; import { AuthContext } from '../../utils/auth_context'; import { ApiContext } from '../../utils/api_context'; import { Paper } from '../common/paper'; import { Input } from '../common/input'; import { Button } from '../common/button'; export const SignUp = () => { const [, setAuthToken] = useContext(AuthContext); const api = useContext(ApiContext); const navigate = useNavigate(); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [email, setEmail] = useState(''); const [emailConfirmation, setEmailConfirmation] = useState(''); const [password, setPassword] = useState(''); const [passwordConfirmation, setPasswordConfirmation] = useState(''); 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; } if (password !== passwordConfirmation) { setErrorMessage('Password does not match'); return; } if (firstName === '') { setErrorMessage('First name cannot be blank.'); return; } if (lastName === '') { setErrorMessage('Last name cannot be blank.'); return; } const { token } = await api.post('/users', { firstName, lastName, email, password, }); setAuthToken(token); navigate('/'); }; return (
First Name
setFirstName(e.target.value)} />
Last Name
setLastName(e.target.value)} />
Email
setEmail(e.target.value)} />
Confirm Email
setEmailConfirmation(e.target.value)} />
Password
setPassword(e.target.value)} />
Confirm Password
setPasswordConfirmation(e.target.value)} />
{errorMessage}
); };