SignupForm.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { SyntheticEvent } from 'react'
  2. import { useMutation } from '@apollo/react-hooks'
  3. import * as Yup from 'yup'
  4. import { useFormHandler, TextInput } from '../form/forms'
  5. import { USER_SIGNUP } from './graphql'
  6. import { userValidation } from './validation'
  7. const initialValues = {
  8. name: 'Tomi',
  9. email: 'tomi@cvetic.ch',
  10. password: '1234',
  11. passwordAgain: '1234'
  12. }
  13. const SignupForm = () => {
  14. const { inputProps, values } = useFormHandler(initialValues, userValidation.validate)
  15. const [userSignup, { loading, error }] = useMutation(USER_SIGNUP)
  16. return (
  17. <form onSubmit={async (event: SyntheticEvent) => {
  18. event.preventDefault()
  19. try {
  20. const data = await userSignup({ variables: values })
  21. } catch (error) {
  22. console.log(error)
  23. }
  24. }}>
  25. <TextInput label='Name' {...inputProps('name')} />
  26. <TextInput label='Email' {...inputProps('email')} />
  27. <TextInput label='Password' {...inputProps('password')} />
  28. <TextInput label='Repeat password' {...inputProps('passwordAgain')} />
  29. <button type='reset' disabled={loading}>Reset</button>
  30. <button type='submit' disabled={loading}>Sign Up!</button>
  31. <style jsx>
  32. {`
  33. select, input {
  34. color: rgba(0,0,127,1);
  35. }
  36. .error {
  37. font-size: 12px;
  38. color: rgba(127,0,0,1);
  39. width: 400px;
  40. margin-top: 0.25rem;
  41. }
  42. .error:before {
  43. content: "❌ ";
  44. font-size: 10px;
  45. }
  46. label {
  47. color: red;
  48. margin-top: 1rem;
  49. }
  50. `}
  51. </style>
  52. </form>
  53. )
  54. }
  55. export default SignupForm