SignupForm.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Formik, Form } from 'formik'
  2. import { Mutation } from 'react-apollo'
  3. import { adopt } from 'react-adopt'
  4. import { useFormValidation } from '../../lib/forms'
  5. import { USER_SIGNUP, CURRENT_USER } from './graphql'
  6. import { signupValidation } from './validation'
  7. const initialValues = {
  8. name: 'Tomi',
  9. email: 'tomi@cvetic.ch',
  10. password: '1234',
  11. passwordAgain: '1234'
  12. }
  13. async function onSubmit(values, mutation) {
  14. try {
  15. const user = await mutation({ variables: values })
  16. console.log(user)
  17. } catch (error) {
  18. console.log(error)
  19. }
  20. }
  21. const SignupAdoption = adopt({
  22. mutation: ({ render }) => (
  23. <Mutation
  24. mutation={USER_SIGNUP}
  25. refetchQueries={[{ query: CURRENT_USER }]}
  26. >{(signup, { data, error, loading }) => render({ signup, data, error, loading, render })}
  27. </Mutation>
  28. ),
  29. form: ({ mutation, render }) => (
  30. <Formik
  31. initialValues={initialValues}
  32. validationSchema={validationSchema}
  33. onSubmit={values => onSubmit(values, mutation)}
  34. >
  35. {render}
  36. </Formik>
  37. )
  38. })
  39. const SignupForm = props => (
  40. <SignupAdoption>
  41. {({ form, mutation: { signup, data, error, loading } }) => (
  42. <Form>
  43. <TextInput
  44. label='Name'
  45. name='name'
  46. type='text'
  47. placeholder='Name'
  48. />
  49. <TextInput
  50. label='Email'
  51. name='email'
  52. type='email'
  53. placeholder='Email Address'
  54. />
  55. <TextInput
  56. label='Password'
  57. name='password'
  58. type='password'
  59. placeholder='1234'
  60. />
  61. <TextInput
  62. label='Repeat password'
  63. name='passwordAgain'
  64. type='password'
  65. placeholder='1234'
  66. />
  67. <button type='reset' disabled={loading}>Sign Up!</button>
  68. <button type='submit' disabled={loading}>Sign Up!</button>
  69. <style jsx>
  70. {`
  71. select, input {
  72. color: rgba(0,0,127,1);
  73. }
  74. .error {
  75. font-size: 12px;
  76. color: rgba(127,0,0,1);
  77. width: 400px;
  78. margin-top: 0.25rem;
  79. }
  80. .error:before {
  81. content: "❌ ";
  82. font-size: 10px;
  83. }
  84. label {
  85. color: red;
  86. margin-top: 1rem;
  87. }
  88. `}
  89. </style>
  90. </Form>
  91. )}
  92. </SignupAdoption>
  93. )
  94. export default SignupForm