import React, { useState } from 'react'; import { useHistory } from 'react-router'; import { useMutation, gql } from '@apollo/client'; import { APPOINTMENTS_PER_PAGE } from '../constants'; import { APPOINTMENTS_QUERY } from './AppointmentList'; import Datetime from 'react-datetime'; import "react-datetime/css/react-datetime.css"; const CREATE_APPOINTMENT_MUTATION = gql` mutation CreateAppointmentMutation( $title: String! $description: String! ) { createAppointment(title: $title, description: $description) { id title description timeStart timeEnd } } `; const CreateAppointment = () => { const history = useHistory(); const [formState, setFormState] = useState({ title: '', description: '', timeStart: '', timeEnd: '' }); const [createAppointment] = useMutation(CREATE_APPOINTMENT_MUTATION, { variables: { title: formState.title, description: formState.description, timeStart: formState.timeStart, timeEnd: formState.timeEnd }, update: (cache, { data: { createAppointment } }) => { const take = APPOINTMENTS_PER_PAGE; const skip = 0; const orderBy = { createdAt: 'desc' }; const data = cache.readQuery({ query: APPOINTMENTS_QUERY, variables: { take, skip, orderBy } }); cache.writeQuery({ query: APPOINTMENTS_QUERY, data: { allAppointments: { appointments: [createAppointment, ...data.allAppointments.appointments] } }, variables: { take, skip, orderBy } }); }, onCompleted: () => history.push('/new/1') }); return (
{ e.preventDefault(); createAppointment(); }} >
setFormState({ ...formState, title: e.target.value }) } type="text" placeholder="Input title" /> setFormState({ ...formState, description: e.target.value }) } type="text" placeholder="Input description" /> setFormState({ ...formState, timeStart: e }) } /> setFormState({ ...formState, timeEnd: e }) } /> {/* setFormState({ ...formState, timeStart: e.target.value }) } type="text" placeholder="Input end time" /> */}
); }; export default CreateAppointment;