LoginForm.tsx 989 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { useMutation } from '@apollo/react-hooks'
  2. import { USER_LOGIN, CURRENT_USER } from './graphql'
  3. import { useFormHandler, TextInput } from '../form/forms'
  4. import { FormEvent } from 'react'
  5. const initialValues = {
  6. email: 'tomislav.cvetic@u-blox.com',
  7. password: '1234'
  8. }
  9. const LoginForm = () => {
  10. const [login, { loading, error }] = useMutation(USER_LOGIN)
  11. const { inputProps, values } = useFormHandler(initialValues)
  12. return (
  13. <form onSubmit={async (event: FormEvent) => {
  14. event.preventDefault()
  15. try {
  16. const data = await login({
  17. variables: values,
  18. refetchQueries: [{ query: CURRENT_USER }]
  19. })
  20. console.log(data)
  21. } catch (error) {
  22. console.log(error)
  23. }
  24. }}>
  25. <TextInput label='Email' {...inputProps('email')} />
  26. <TextInput label='Password' {...inputProps('password')} />
  27. <button type='submit' disabled={loading}>Login!</button>
  28. </form>
  29. )
  30. }
  31. export default LoginForm