2022-03-28 19:43:45 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2020-02-12 07:23:06 +00:00
|
|
|
import { Schedule } from '@/api/server/schedules/getServerSchedules';
|
2020-02-23 04:07:56 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
2021-05-01 17:44:40 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2020-04-26 00:37:03 +00:00
|
|
|
import FormikSwitch from '@/components/elements/FormikSwitch';
|
2020-03-22 20:56:15 +00:00
|
|
|
import createOrUpdateSchedule from '@/api/server/schedules/createOrUpdateSchedule';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-04-10 17:46:00 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-05 00:00:19 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2021-05-01 17:44:40 +00:00
|
|
|
import ModalContext from '@/context/ModalContext';
|
|
|
|
import asModal from '@/hoc/asModal';
|
2022-03-28 19:43:45 +00:00
|
|
|
import Switch from '@/components/elements/Switch';
|
|
|
|
import ScheduleCheatsheetCards from '@/components/server/schedules/ScheduleCheatsheetCards';
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2021-05-01 17:44:40 +00:00
|
|
|
interface Props {
|
2020-03-22 20:56:15 +00:00
|
|
|
schedule?: Schedule;
|
2021-05-01 17:44:40 +00:00
|
|
|
}
|
2020-02-23 04:07:56 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
dayOfWeek: string;
|
2021-01-17 03:07:39 +00:00
|
|
|
month: string;
|
2020-02-23 04:07:56 +00:00
|
|
|
dayOfMonth: string;
|
|
|
|
hour: string;
|
|
|
|
minute: string;
|
|
|
|
enabled: boolean;
|
2021-05-01 17:44:40 +00:00
|
|
|
onlyWhenOnline: boolean;
|
2020-02-23 04:07:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 17:44:40 +00:00
|
|
|
const EditScheduleModal = ({ schedule }: Props) => {
|
2020-04-10 17:46:00 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
2021-05-01 17:44:40 +00:00
|
|
|
const { dismiss } = useContext(ModalContext);
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2021-05-01 17:44:40 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-04-10 17:46:00 +00:00
|
|
|
const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule);
|
2022-03-28 19:43:45 +00:00
|
|
|
const [ showCheatsheet, setShowCheetsheet ] = useState(false);
|
2020-04-10 17:46:00 +00:00
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
useEffect(() => {
|
2021-05-01 17:44:40 +00:00
|
|
|
return () => {
|
|
|
|
clearFlashes('schedule:edit');
|
|
|
|
};
|
|
|
|
}, []);
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('schedule:edit');
|
|
|
|
createOrUpdateSchedule(uuid, {
|
|
|
|
id: schedule?.id,
|
|
|
|
name: values.name,
|
|
|
|
cron: {
|
|
|
|
minute: values.minute,
|
|
|
|
hour: values.hour,
|
|
|
|
dayOfWeek: values.dayOfWeek,
|
2021-01-17 03:07:39 +00:00
|
|
|
month: values.month,
|
2020-03-22 20:56:15 +00:00
|
|
|
dayOfMonth: values.dayOfMonth,
|
|
|
|
},
|
2021-05-01 17:44:40 +00:00
|
|
|
onlyWhenOnline: values.onlyWhenOnline,
|
2020-03-22 20:56:15 +00:00
|
|
|
isActive: values.enabled,
|
|
|
|
})
|
|
|
|
.then(schedule => {
|
|
|
|
setSubmitting(false);
|
2020-04-10 17:46:00 +00:00
|
|
|
appendSchedule(schedule);
|
2021-05-01 17:44:40 +00:00
|
|
|
dismiss();
|
2020-03-22 20:56:15 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
setSubmitting(false);
|
|
|
|
addError({ key: 'schedule:edit', message: httpErrorToHuman(error) });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
name: schedule?.name || '',
|
|
|
|
minute: schedule?.cron.minute || '*/5',
|
2021-01-17 03:07:39 +00:00
|
|
|
hour: schedule?.cron.hour || '*',
|
|
|
|
dayOfMonth: schedule?.cron.dayOfMonth || '*',
|
|
|
|
month: schedule?.cron.month || '*',
|
|
|
|
dayOfWeek: schedule?.cron.dayOfWeek || '*',
|
2021-05-01 18:25:35 +00:00
|
|
|
enabled: schedule?.isActive ?? true,
|
|
|
|
onlyWhenOnline: schedule?.onlyWhenOnline ?? true,
|
2020-03-22 20:56:15 +00:00
|
|
|
} as Values}
|
|
|
|
>
|
2021-05-01 17:44:40 +00:00
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<Form>
|
|
|
|
<h3 css={tw`text-2xl mb-6`}>{schedule ? 'Edit schedule' : 'Create new schedule'}</h3>
|
|
|
|
<FlashMessageRender byKey={'schedule:edit'} css={tw`mb-6`}/>
|
|
|
|
<Field
|
|
|
|
name={'name'}
|
|
|
|
label={'Schedule name'}
|
2022-06-18 16:16:54 +00:00
|
|
|
description={'A human readable identifier for this schedule.'}
|
2021-05-01 17:44:40 +00:00
|
|
|
/>
|
|
|
|
<div css={tw`grid grid-cols-2 sm:grid-cols-5 gap-4 mt-6`}>
|
|
|
|
<Field name={'minute'} label={'Minute'}/>
|
|
|
|
<Field name={'hour'} label={'Hour'}/>
|
|
|
|
<Field name={'dayOfMonth'} label={'Day of month'}/>
|
|
|
|
<Field name={'month'} label={'Month'}/>
|
|
|
|
<Field name={'dayOfWeek'} label={'Day of week'}/>
|
|
|
|
</div>
|
|
|
|
<p css={tw`text-neutral-400 text-xs mt-2`}>
|
|
|
|
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.
|
|
|
|
</p>
|
2022-03-28 19:43:45 +00:00
|
|
|
<div css={tw`mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded`}>
|
|
|
|
<Switch
|
|
|
|
name={'show_cheatsheet'}
|
|
|
|
description={'Show the cron cheatsheet for some examples.'}
|
|
|
|
label={'Show Cheatsheet'}
|
|
|
|
defaultChecked={showCheatsheet}
|
|
|
|
onChange={() => setShowCheetsheet(s => !s)}
|
|
|
|
/>
|
|
|
|
{showCheatsheet &&
|
|
|
|
<div css={tw`block md:flex w-full`}>
|
|
|
|
<ScheduleCheatsheetCards/>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
2021-05-01 17:44:40 +00:00
|
|
|
<div css={tw`mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded`}>
|
|
|
|
<FormikSwitch
|
2021-05-01 18:24:18 +00:00
|
|
|
name={'onlyWhenOnline'}
|
|
|
|
description={'Only execute this schedule when the server is in a running state.'}
|
2021-05-01 17:44:40 +00:00
|
|
|
label={'Only When Server Is Online'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded`}>
|
|
|
|
<FormikSwitch
|
|
|
|
name={'enabled'}
|
2021-05-01 18:24:18 +00:00
|
|
|
description={'This schedule will be executed automatically if enabled.'}
|
2021-05-01 17:44:40 +00:00
|
|
|
label={'Schedule Enabled'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button css={tw`w-full sm:w-auto`} type={'submit'} disabled={isSubmitting}>
|
|
|
|
{schedule ? 'Save changes' : 'Create schedule'}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)}
|
2020-03-22 20:56:15 +00:00
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
2021-05-01 17:44:40 +00:00
|
|
|
|
|
|
|
export default asModal<Props>()(EditScheduleModal);
|