import React, { useContext, useEffect } from 'react'; import { Schedule } from '@/api/server/schedules/getServerSchedules'; import Field from '@/components/elements/Field'; import { Form, Formik, FormikHelpers } from 'formik'; import FormikSwitch from '@/components/elements/FormikSwitch'; import createOrUpdateSchedule from '@/api/server/schedules/createOrUpdateSchedule'; import { ServerContext } from '@/state/server'; import { httpErrorToHuman } from '@/api/http'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import tw from 'twin.macro'; import Button from '@/components/elements/Button'; import ModalContext from '@/context/ModalContext'; import asModal from '@/hoc/asModal'; interface Props { schedule?: Schedule; } interface Values { name: string; dayOfWeek: string; month: string; dayOfMonth: string; hour: string; minute: string; enabled: boolean; onlyWhenOnline: boolean; } const EditScheduleModal = ({ schedule }: Props) => { const { addError, clearFlashes } = useFlash(); const { dismiss } = useContext(ModalContext); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule); useEffect(() => { return () => { clearFlashes('schedule:edit'); }; }, []); const submit = (values: Values, { setSubmitting }: FormikHelpers) => { clearFlashes('schedule:edit'); createOrUpdateSchedule(uuid, { id: schedule?.id, name: values.name, cron: { minute: values.minute, hour: values.hour, dayOfWeek: values.dayOfWeek, month: values.month, dayOfMonth: values.dayOfMonth, }, onlyWhenOnline: values.onlyWhenOnline, isActive: values.enabled, }) .then(schedule => { setSubmitting(false); appendSchedule(schedule); dismiss(); }) .catch(error => { console.error(error); setSubmitting(false); addError({ key: 'schedule:edit', message: httpErrorToHuman(error) }); }); }; return ( {({ isSubmitting }) => (

{schedule ? 'Edit schedule' : 'Create new schedule'}

The schedule system supports the use of Cronjob syntax when defining when tasks should begin running. Use the fields above to specify when these tasks should begin running.

)}
); }; export default asModal()(EditScheduleModal);