signup.js.bak 2.7 KB

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