|
@@ -0,0 +1,96 @@
|
|
|
+import { Query, Mutation } from 'react-apollo'
|
|
|
+import { adopt } from 'react-adopt'
|
|
|
+import { Formik, Form } from 'formik'
|
|
|
+
|
|
|
+import { TextInput } from '../lib/forms'
|
|
|
+import { CREATE_TRAINING_TYPE, TRAINING_TYPES } from '../lib/graphql'
|
|
|
+import Overlay from './overlay'
|
|
|
+
|
|
|
+const TrainingTypeInput = props => {
|
|
|
+ const [displayForm, setDisplayForm] = React.useState(false)
|
|
|
+
|
|
|
+ const toggleForm = ev => {
|
|
|
+ ev.preventDefault()
|
|
|
+ setDisplayForm(!displayForm)
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Query query={TRAINING_TYPES}>
|
|
|
+ {({ data, error, loading }) => (
|
|
|
+ <>
|
|
|
+ <label>Training type</label>
|
|
|
+ <div>
|
|
|
+ <select disabled={(loading || error)}>
|
|
|
+ {data ? data.trainingTypes.map(trainingType =>
|
|
|
+ <option key={trainingType.id} value={trainingType.id}>{trainingType.name}</option>
|
|
|
+ ) : null}
|
|
|
+ </select>
|
|
|
+ <button
|
|
|
+ type='button'
|
|
|
+ onClick={toggleForm}
|
|
|
+ >Add
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ {displayForm ? (
|
|
|
+ <Overlay close={toggleForm}>
|
|
|
+ <TrainingTypeForm />
|
|
|
+ </Overlay>
|
|
|
+ ) : null}
|
|
|
+
|
|
|
+ <style jsx>
|
|
|
+ {`
|
|
|
+ div {
|
|
|
+ width: 100%;
|
|
|
+ }
|
|
|
+ `}
|
|
|
+ </style>
|
|
|
+ </>
|
|
|
+ )}
|
|
|
+ </Query>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+const TrainingTypeAdoption = adopt({
|
|
|
+ mutation: ({ render }) => (
|
|
|
+ <Mutation
|
|
|
+ mutation={CREATE_TRAINING_TYPE}
|
|
|
+ refetchQueries={[{ query: TRAINING_TYPES }]}
|
|
|
+ >
|
|
|
+ {(createTrainingType, { data, error, loading }) => render({ createTrainingType, data, error, loading })}
|
|
|
+ </Mutation>
|
|
|
+ ),
|
|
|
+ form: ({ mutation: { createTrainingType }, render }) => (
|
|
|
+ <Formik
|
|
|
+ initialValues={{
|
|
|
+ name: '',
|
|
|
+ description: ''
|
|
|
+ }}
|
|
|
+ onSubmit={values => createTrainingType({ variables: values })}
|
|
|
+ >
|
|
|
+ {render}
|
|
|
+ </Formik>
|
|
|
+ )
|
|
|
+})
|
|
|
+
|
|
|
+const TrainingTypeForm = props => (
|
|
|
+ <TrainingTypeAdoption>{({ mutation, form }) => (
|
|
|
+ <Form>
|
|
|
+ <TextInput
|
|
|
+ label='Name'
|
|
|
+ name='name'
|
|
|
+ type='text'
|
|
|
+ />
|
|
|
+ <TextInput
|
|
|
+ label='Description'
|
|
|
+ name='description'
|
|
|
+ type='text'
|
|
|
+ />
|
|
|
+ <button type='reset'>Reset</button>
|
|
|
+ <button type='submit'>Save</button>
|
|
|
+ </Form>
|
|
|
+ )}
|
|
|
+ </TrainingTypeAdoption>
|
|
|
+)
|
|
|
+
|
|
|
+export { TrainingTypeForm }
|
|
|
+export default TrainingTypeInput
|