import React, { useEffect, useState } from 'react'; import { Schedule } from '@/api/server/schedules/getServerSchedules'; import Modal, { RequiredModalProps } from '@/components/elements/Modal'; import Field from '@/components/elements/Field'; import { Form, Formik, FormikHelpers, useFormikContext } 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'; type Props = { schedule?: Schedule; } & RequiredModalProps; interface Values { name: string; dayOfWeek: string; dayOfMonth: string; hour: string; minute: string; enabled: boolean; } const EditScheduleModal = ({ schedule, ...props }: Omit) => { const { isSubmitting } = useFormikContext(); return (

{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 ({ schedule, visible, ...props }: Props) => { const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const { addError, clearFlashes } = useFlash(); const [ modalVisible, setModalVisible ] = useState(visible); const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule); useEffect(() => { setModalVisible(visible); clearFlashes('schedule:edit'); }, [ visible ]); 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, dayOfMonth: values.dayOfMonth, }, isActive: values.enabled, }) .then(schedule => { setSubmitting(false); appendSchedule(schedule); setModalVisible(false); }) .catch(error => { console.error(error); setSubmitting(false); addError({ key: 'schedule:edit', message: httpErrorToHuman(error) }); }); }; return ( ); };