import React, { useEffect } from 'react'; import Modal from '@/components/elements/Modal'; import { Schedule, Task } from '@/api/server/schedules/getServerSchedules'; import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik'; import { ServerContext } from '@/state/server'; import createOrUpdateScheduleTask from '@/api/server/schedules/createOrUpdateScheduleTask'; import { httpErrorToHuman } from '@/api/http'; import Field from '@/components/elements/Field'; import FlashMessageRender from '@/components/FlashMessageRender'; import { number, object, string } from 'yup'; import useFlash from '@/plugins/useFlash'; import useServer from '@/plugins/useServer'; interface Props { schedule: Schedule; // If a task is provided we can assume we're editing it. If not provided, // we are creating a new one. task?: Task; onDismissed: () => void; } interface Values { action: string; payload: string; timeOffset: string; } const TaskDetailsForm = ({ isEditingTask }: { isEditingTask: boolean }) => { const { values: { action }, setFieldValue, setFieldTouched } = useFormikContext(); useEffect(() => { return () => { setFieldValue('payload', ''); setFieldTouched('payload', false); }; }, [ action ]); return (

Edit Task

{action === 'command' ? :
}
); }; export default ({ task, schedule, onDismissed }: Props) => { const { uuid } = useServer(); const { clearFlashes, addError } = useFlash(); const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule); useEffect(() => { clearFlashes('schedule:task'); }, []); const submit = (values: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('schedule:task'); createOrUpdateScheduleTask(uuid, schedule.id, task?.id, values) .then(task => { let tasks = schedule.tasks.map(t => t.id === task.id ? task : t); if (!schedule.tasks.find(t => t.id === task.id)) { tasks = [ ...tasks, task ]; } appendSchedule({ ...schedule, tasks }); onDismissed(); }) .catch(error => { console.error(error); setSubmitting(false); addError({ message: httpErrorToHuman(error), key: 'schedule:task' }); }); }; return ( {({ isSubmitting }) => ( onDismissed()} showSpinnerOverlay={isSubmitting} > )} ); };