signup.js 2.6 KB

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