trainingForm.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Formik, Form } from 'formik'
  2. import { Mutation } from 'react-apollo'
  3. import { adopt } from 'react-adopt'
  4. import * as Yup from 'yup'
  5. import { TextInput } from '../lib/forms'
  6. import { CREATE_TRAINING } from '../lib/graphql'
  7. const TrainingAdoption = adopt({
  8. mutation: ({ render }) => (
  9. <Mutation mutation={CREATE_TRAINING}>
  10. {(createTraining, { loading, data, error }) => render({ createTraining, loading, data, error })}
  11. </Mutation>
  12. ),
  13. formik: ({ mutation: { createTraining }, render }) => (
  14. <Formik
  15. initialValues={{
  16. title: 'Winter HIIT 7',
  17. type: 'HIIT',
  18. content: [],
  19. trainingDate: Date.now(),
  20. location: 'Yoga room',
  21. attendance: 0,
  22. published: false
  23. }}
  24. validationSchema={Yup.object({
  25. title: Yup.string()
  26. .required('required'),
  27. type: Yup.string(),
  28. trainingDate: Yup.date()
  29. .required('required'),
  30. location: Yup.string()
  31. .required('required'),
  32. attendance: Yup.number()
  33. .integer('integer number'),
  34. published: Yup.bool()
  35. })}
  36. onSubmit={values => console.log(values)}
  37. >
  38. {render}
  39. </Formik>
  40. )
  41. })
  42. const TrainingForm = props => (
  43. <TrainingAdoption>
  44. {({ mutation: { createTraining, data, loading, error }, formik }) => (
  45. <Form>
  46. <h2>Create Training</h2>
  47. <TextInput
  48. label='Title'
  49. name='title'
  50. type='text'
  51. placeholder='title'
  52. />
  53. <TextInput
  54. label='Type'
  55. name='type'
  56. type='text'
  57. placeholder='type'
  58. />
  59. <TextInput
  60. label='Training date'
  61. name='trainingDate'
  62. type='date'
  63. />
  64. <TextInput
  65. label='Location'
  66. name='location'
  67. type='text'
  68. placeholder='location'
  69. />
  70. <TextInput
  71. label='Attendance'
  72. name='attendance'
  73. type='number'
  74. placeholder='attendance'
  75. />
  76. <TextInput
  77. label='Published'
  78. name='published'
  79. type='text'
  80. placeholder='published'
  81. />
  82. <button type='submit'>Create!</button>
  83. </Form>
  84. )}
  85. </TrainingAdoption>
  86. )
  87. export default TrainingForm