import React, { useEffect } from 'react'; import Modal from '@/components/elements/Modal'; import { Task } from '@/api/server/schedules/getServerSchedules'; import { Field as FormikField, Form, Formik, FormikHelpers, useFormikContext } from 'formik'; import { ServerContext } from '@/state/server'; import { Actions, useStoreActions } from 'easy-peasy'; import { ApplicationStore } from '@/state'; 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'; interface Props { scheduleId: number; // If a task is provided we can assume we're editing it. If not provided, // we are creating a new one. task?: Task; onDismissed: (task: Task | undefined | void) => void; } interface Values { action: string; payload: string; timeOffset: string; } const TaskDetailsForm = ({ isEditingTask }: { isEditingTask: boolean }) => { const { values: { action }, setFieldValue, setFieldTouched } = useFormikContext(); useEffect(() => { setFieldValue('payload', ''); setFieldTouched('payload', false); }, [action]); return (

Edit Task

{action === 'command' ? :
}
); }; export default ({ task, scheduleId, onDismissed }: Props) => { const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const { clearFlashes, addError } = useStoreActions((actions: Actions) => actions.flashes); useEffect(() => { clearFlashes('schedule:task'); }, []); const submit = (values: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('schedule:task'); createOrUpdateScheduleTask(uuid, scheduleId, task?.id, values) .then(task => onDismissed(task)) .catch(error => { console.error(error); setSubmitting(false); addError({ message: httpErrorToHuman(error), key: 'schedule:task' }); }); }; return ( {({ isSubmitting }) => ( onDismissed()} showSpinnerOverlay={isSubmitting} > )} ); };