import React from 'react'; import { useCallback, useState } from 'react'; import { useSignUp } from '../../services/auth.service'; import { SignForm, ActualForm, Legend, Section, TextField, Button, ErrorMessage, } from './form-components'; import { RouteComponentProps } from 'react-router-dom'; const SignUpForm: React.FC> = ({ history }) => { const [name, setName] = useState(''); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [passwordConfirm, setPasswordConfirm] = useState(''); const [error, setError] = useState(''); const [signUp] = useSignUp(); const updateName = useCallback(({ target }) => { setError(''); setName(target.value); }, []); const updateUsername = useCallback(({ target }) => { setError(''); setUsername(target.value); }, []); const updatePassword = useCallback(({ target }) => { setError(''); setPassword(target.value); }, []); const updatePasswordConfirm = useCallback(({ target }) => { setError(''); setPasswordConfirm(target.value); }, []); const maySignUp = useCallback(() => { return !!(name && username && password && password === passwordConfirm); }, [name, username, password, passwordConfirm]); const handleSignUp = useCallback(() => { signUp({ variables: { username, password, passwordConfirm, name } }) .then(() => { history.replace('/sign-in'); }) .catch((error) => { setError(error.message || error); }); }, [name, username, password, passwordConfirm, history, signUp]); return ( Sign up
{error}
); }; export default SignUpForm;