|
@@ -1,30 +1,37 @@
|
|
|
import {
|
|
|
useTrainingsQuery,
|
|
|
useCreateTrainingMutation,
|
|
|
- BlockCreateInput
|
|
|
+ CreateTrainingMutationVariables,
|
|
|
+ BlockCreateManyInput
|
|
|
} from '../../gql'
|
|
|
-import { useForm } from '../../form/hooks/useForm'
|
|
|
-import EditBlock from './EditBlock'
|
|
|
+import { useForm, TextInput, Checkbox, DateTimeInput } from '../../form'
|
|
|
+import BlockFormInputs from './BlockFormInputs'
|
|
|
import TrainingTypeSelector from './TrainingTypeSelector'
|
|
|
|
|
|
const TrainingList = () => {
|
|
|
const { data, error, loading } = useTrainingsQuery()
|
|
|
|
|
|
return (
|
|
|
- <ul>{data && data.trainings.map(training => <li>{training.id}</li>)}</ul>
|
|
|
+ <ul>
|
|
|
+ {data &&
|
|
|
+ data.trainings.map(training => (
|
|
|
+ <li key={training.id}>{training.id}</li>
|
|
|
+ ))}
|
|
|
+ </ul>
|
|
|
)
|
|
|
}
|
|
|
|
|
|
const NewTraining = ({ id }: { id?: string }) => {
|
|
|
- const { values, touched, handleChange } = useForm({
|
|
|
+ const { values, touched, ...props } = useForm({
|
|
|
title: '',
|
|
|
- type: undefined,
|
|
|
- trainingDate: '',
|
|
|
location: '',
|
|
|
attendance: 0,
|
|
|
- published: false,
|
|
|
- blocks: [] as BlockCreateInput[]
|
|
|
- })
|
|
|
+ trainingDate: new Date().toISOString(),
|
|
|
+ type: { connect: { id: '' } },
|
|
|
+ blocks: { connect: [], create: [] },
|
|
|
+ published: false
|
|
|
+ } as CreateTrainingMutationVariables)
|
|
|
+ console.log(values.trainingDate)
|
|
|
const [createTraining, createData] = useCreateTrainingMutation()
|
|
|
|
|
|
return (
|
|
@@ -34,72 +41,61 @@ const NewTraining = ({ id }: { id?: string }) => {
|
|
|
<form
|
|
|
onSubmit={ev => {
|
|
|
ev.preventDefault()
|
|
|
- createTraining({
|
|
|
- variables: {
|
|
|
- ...values,
|
|
|
- type: { connect: { id: values.type } }
|
|
|
- }
|
|
|
- })
|
|
|
+ createTraining({ variables: values })
|
|
|
}}
|
|
|
>
|
|
|
- <label>Title</label>
|
|
|
- <input
|
|
|
- type='text'
|
|
|
- name='title'
|
|
|
- id='title'
|
|
|
- value={values.title}
|
|
|
- onChange={handleChange}
|
|
|
- />
|
|
|
- <TrainingTypeSelector value={values.type} onChange={handleChange} />
|
|
|
- <label>Training date</label>
|
|
|
- <input
|
|
|
- type='date'
|
|
|
+ <TextInput name='title' label='Title' value={values.title} {...props} />
|
|
|
+ <TrainingTypeSelector value={values.type} {...props} />
|
|
|
+ <DateTimeInput
|
|
|
name='trainingDate'
|
|
|
- id='trainingDate'
|
|
|
+ label='Training date'
|
|
|
value={values.trainingDate}
|
|
|
- onChange={handleChange}
|
|
|
+ {...props}
|
|
|
/>
|
|
|
- <label>Location</label>
|
|
|
- <input
|
|
|
- type='text'
|
|
|
+ <TextInput
|
|
|
name='location'
|
|
|
- id='location'
|
|
|
+ label='Location'
|
|
|
value={values.location}
|
|
|
- onChange={handleChange}
|
|
|
+ {...props}
|
|
|
/>
|
|
|
- <label>Attendance</label>
|
|
|
- <input
|
|
|
- type='number'
|
|
|
+ <TextInput
|
|
|
name='attendance'
|
|
|
- id='attendance'
|
|
|
+ label='Attendance'
|
|
|
+ type='number'
|
|
|
value={values.attendance}
|
|
|
- onChange={handleChange}
|
|
|
+ {...props}
|
|
|
/>
|
|
|
- <label>Published</label>
|
|
|
- <input
|
|
|
- type='checkbox'
|
|
|
+ <Checkbox
|
|
|
name='published'
|
|
|
- id='published'
|
|
|
- checked={values.published}
|
|
|
- onChange={handleChange}
|
|
|
+ label='Published'
|
|
|
+ value={values.published}
|
|
|
+ {...props}
|
|
|
/>
|
|
|
<label>Blocks</label>
|
|
|
- {values.blocks.map((block, index) => (
|
|
|
- <EditBlock
|
|
|
- key={index}
|
|
|
- name={`blocks.${index}`}
|
|
|
- value={block}
|
|
|
- onChange={handleChange}
|
|
|
- />
|
|
|
- ))}
|
|
|
+ {values.blocks &&
|
|
|
+ values.blocks.map((block, index) => (
|
|
|
+ <BlockFormInputs
|
|
|
+ key={index}
|
|
|
+ name={`blocks.${index}`}
|
|
|
+ value={block}
|
|
|
+ {...props}
|
|
|
+ />
|
|
|
+ ))}
|
|
|
<button
|
|
|
onClick={event => {
|
|
|
event.preventDefault()
|
|
|
- handleChange({
|
|
|
+ const { onChange } = props
|
|
|
+ onChange({
|
|
|
target: {
|
|
|
type: 'custom',
|
|
|
name: 'blocks',
|
|
|
- value: [...values.blocks, { sequence: 0, title: '' }]
|
|
|
+ value: {
|
|
|
+ create:
|
|
|
+ values.blocks && values.blocks.create
|
|
|
+ ? [...values.blocks.create]
|
|
|
+ : [{ title: '' }],
|
|
|
+ connect: []
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
}}
|