import React from 'react'; import { useCallback, useState } from 'react'; import { useSignIn } from '../../services/auth.service'; import { SignForm, ActualForm, Legend, Section, TextField, Button, ErrorMessage, } from './form-components'; import { RouteComponentProps } from 'react-router-dom'; const SignInForm: React.FC> = ({ history }) => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [signIn] = useSignIn(); const onUsernameChange = useCallback(({ target }) => { setError(''); setUsername(target.value); }, []); const onPasswordChange = useCallback(({ target }) => { setError(''); setPassword(target.value); }, []); const maySignIn = useCallback(() => { return !!(username && password); }, [username, password]); const handleSignIn = useCallback(() => { signIn({ variables: { username, password } }) .then(() => { history.replace('/chats'); }) .catch((error) => { setError(error.message || error); }); }, [username, password, history, signIn]); return ( Sign in
{error}
); }; export default SignInForm;