2020-03-22 20:56:15 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-02-12 07:23:06 +00:00
|
|
|
import { Schedule } from '@/api/server/schedules/getServerSchedules';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
2020-02-23 04:07:56 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
2020-03-22 20:56:15 +00:00
|
|
|
import { Form, Formik, FormikHelpers, useFormikContext } 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';
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
type Props = {
|
|
|
|
schedule?: Schedule;
|
|
|
|
} & RequiredModalProps;
|
2020-02-23 04:07:56 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string;
|
|
|
|
dayOfWeek: string;
|
|
|
|
dayOfMonth: string;
|
|
|
|
hour: string;
|
|
|
|
minute: string;
|
|
|
|
enabled: boolean;
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
const EditScheduleModal = ({ schedule, ...props }: Omit<Props, 'onScheduleUpdated'>) => {
|
|
|
|
const { isSubmitting } = useFormikContext();
|
|
|
|
|
2020-02-12 07:23:06 +00:00
|
|
|
return (
|
2020-03-22 20:56:15 +00:00
|
|
|
<Modal {...props} showSpinnerOverlay={isSubmitting}>
|
2020-07-05 00:00:19 +00:00
|
|
|
<h3 css={tw`mb-6`}>{schedule ? 'Edit schedule' : 'Create new schedule'}</h3>
|
|
|
|
<FlashMessageRender byKey={'schedule:edit'} css={tw`mb-6`}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
<Form>
|
|
|
|
<Field
|
|
|
|
name={'name'}
|
|
|
|
label={'Schedule name'}
|
|
|
|
description={'A human readable identifer for this schedule.'}
|
|
|
|
/>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`flex mt-6`}>
|
|
|
|
<div css={tw`flex-1 mr-4`}>
|
2020-09-10 05:02:44 +00:00
|
|
|
<Field name={'minute'} label={'Minute'}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`flex-1 mr-4`}>
|
2020-09-10 05:02:44 +00:00
|
|
|
<Field name={'hour'} label={'Hour'}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`flex-1 mr-4`}>
|
2020-09-10 05:02:44 +00:00
|
|
|
<Field name={'dayOfMonth'} label={'Day of month'}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`flex-1`}>
|
2020-09-10 05:02:44 +00:00
|
|
|
<Field name={'dayOfWeek'} label={'Day of week'}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-07-05 00:00:19 +00:00
|
|
|
<p css={tw`text-neutral-400 text-xs mt-2`}>
|
2020-02-23 04:07:56 +00:00
|
|
|
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>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`mt-6 bg-neutral-700 border border-neutral-800 shadow-inner p-4 rounded`}>
|
2020-04-26 00:37:03 +00:00
|
|
|
<FormikSwitch
|
2020-02-23 04:07:56 +00:00
|
|
|
name={'enabled'}
|
|
|
|
description={'If disabled, this schedule and it\'s associated tasks will not run.'}
|
|
|
|
label={'Enabled'}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`mt-6 text-right`}>
|
2020-08-02 02:49:38 +00:00
|
|
|
<Button type={'submit'} disabled={isSubmitting}>
|
2020-03-22 20:56:15 +00:00
|
|
|
{schedule ? 'Save changes' : 'Create schedule'}
|
2020-07-05 00:00:19 +00:00
|
|
|
</Button>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
2020-02-12 07:23:06 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2020-04-10 17:46:00 +00:00
|
|
|
export default ({ schedule, visible, ...props }: Props) => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-04-10 17:46:00 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
2020-03-22 20:56:15 +00:00
|
|
|
const [ modalVisible, setModalVisible ] = useState(visible);
|
2020-02-23 04:07:56 +00:00
|
|
|
|
2020-04-10 17:46:00 +00:00
|
|
|
const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule);
|
|
|
|
|
2020-03-22 20:56:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setModalVisible(visible);
|
|
|
|
clearFlashes('schedule:edit');
|
2020-04-10 17:46:00 +00:00
|
|
|
}, [ visible ]);
|
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,
|
|
|
|
dayOfMonth: values.dayOfMonth,
|
|
|
|
},
|
|
|
|
isActive: values.enabled,
|
|
|
|
})
|
|
|
|
.then(schedule => {
|
|
|
|
setSubmitting(false);
|
2020-04-10 17:46:00 +00:00
|
|
|
appendSchedule(schedule);
|
2020-03-22 20:56:15 +00:00
|
|
|
setModalVisible(false);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
setSubmitting(false);
|
|
|
|
addError({ key: 'schedule:edit', message: httpErrorToHuman(error) });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
name: schedule?.name || '',
|
|
|
|
dayOfWeek: schedule?.cron.dayOfWeek || '*',
|
|
|
|
dayOfMonth: schedule?.cron.dayOfMonth || '*',
|
|
|
|
hour: schedule?.cron.hour || '*',
|
|
|
|
minute: schedule?.cron.minute || '*/5',
|
|
|
|
enabled: schedule ? schedule.isActive : true,
|
|
|
|
} as Values}
|
|
|
|
validationSchema={null}
|
|
|
|
>
|
|
|
|
<EditScheduleModal
|
|
|
|
visible={modalVisible}
|
|
|
|
schedule={schedule}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|