Finish building out schedule management functionality

This commit is contained in:
Dane Everitt 2020-03-22 13:56:15 -07:00
parent 4ac6507b69
commit 1e0d630e1f
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
16 changed files with 510 additions and 79 deletions

View file

@ -0,0 +1,19 @@
import { rawDataToServerSchedule, Schedule } from '@/api/server/schedules/getServerSchedules';
import http from '@/api/http';
type Data = Pick<Schedule, 'cron' | 'name' | 'isActive'> & { id?: number }
export default (uuid: string, schedule: Data): Promise<Schedule> => {
return new Promise((resolve, reject) => {
http.post(`/api/client/servers/${uuid}/schedules${schedule.id ? `/${schedule.id}` : ''}`, {
is_active: schedule.isActive,
name: schedule.name,
minute: schedule.cron.minute,
hour: schedule.cron.hour,
day_of_month: schedule.cron.dayOfMonth,
day_of_week: schedule.cron.dayOfWeek,
})
.then(({ data }) => resolve(rawDataToServerSchedule(data.attributes)))
.catch(reject);
});
};

View file

@ -0,0 +1,9 @@
import http from '@/api/http';
export default (uuid: string, schedule: number): Promise<void> => {
return new Promise((resolve, reject) => {
http.delete(`/api/client/servers/${uuid}/schedules/${schedule}`)
.then(() => resolve())
.catch(reject);
});
};

View file

@ -0,0 +1,69 @@
import React, { useState } from 'react';
import Modal from '@/components/elements/Modal';
import deleteSchedule from '@/api/server/schedules/deleteSchedule';
import { ServerContext } from '@/state/server';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http';
interface Props {
scheduleId: number;
onDeleted: () => void;
}
export default ({ scheduleId, onDeleted }: Props) => {
const [ visible, setVisible ] = useState(false);
const [ isLoading, setIsLoading ] = useState(false);
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const onDelete = () => {
setIsLoading(true);
clearFlashes('schedules');
deleteSchedule(uuid, scheduleId)
.then(() => {
setIsLoading(false);
onDeleted();
})
.catch(error => {
console.error(error);
addError({ key: 'schedules', message: httpErrorToHuman(error) });
setIsLoading(false);
setVisible(false);
});
};
return (
<>
<Modal
visible={visible}
onDismissed={() => setVisible(false)}
showSpinnerOverlay={isLoading}
>
<h3 className={'mb-6'}>Delete schedule</h3>
<p className={'text-sm'}>
Are you sure you want to delete this schedule? All tasks will be removed and any running processes
will be terminated.
</p>
<div className={'mt-6 flex justify-end'}>
<button
className={'btn btn-secondary btn-sm mr-4'}
onClick={() => setVisible(false)}
>
Cancel
</button>
<button
className={'btn btn-red btn-sm'}
onClick={() => onDelete()}
>
Yes, delete schedule
</button>
</div>
</Modal>
<button className={'btn btn-red btn-secondary btn-sm mr-4'} onClick={() => setVisible(true)}>
Delete
</button>
</>
);
};

View file

@ -1,21 +1,20 @@
import React from 'react';
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 { connect } from 'react-redux';
import { Form, FormikProps, withFormik } from 'formik';
import { Actions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import Switch from '@/components/elements/Switch';
import { boolean, object, string } from 'yup';
import createOrUpdateSchedule from '@/api/server/schedules/createOrUpdateSchedule';
import { ServerContext } from '@/state/server';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http';
import FlashMessageRender from '@/components/FlashMessageRender';
type OwnProps = { schedule: Schedule } & RequiredModalProps;
interface ReduxProps {
addError: ApplicationStore['flashes']['addError'];
}
type ComponentProps = OwnProps & ReduxProps;
type Props = {
schedule?: Schedule;
onScheduleUpdated: (schedule: Schedule) => void;
} & RequiredModalProps;
interface Values {
name: string;
@ -26,9 +25,13 @@ interface Values {
enabled: boolean;
}
const EditScheduleModal = ({ values, schedule, ...props }: ComponentProps & FormikProps<Values>) => {
const EditScheduleModal = ({ schedule, ...props }: Omit<Props, 'onScheduleUpdated'>) => {
const { isSubmitting } = useFormikContext();
return (
<Modal {...props}>
<Modal {...props} showSpinnerOverlay={isSubmitting}>
<h3 className={'mb-6'}>{schedule ? 'Edit schedule' : 'Create new schedule'}</h3>
<FlashMessageRender byKey={'schedule:edit'} className={'mb-6'}/>
<Form>
<Field
name={'name'}
@ -61,8 +64,8 @@ const EditScheduleModal = ({ values, schedule, ...props }: ComponentProps & Form
/>
</div>
<div className={'mt-6 text-right'}>
<button className={'btn btn-lg btn-primary'} type={'button'}>
Save
<button className={'btn btn-sm btn-primary'} type={'submit'}>
{schedule ? 'Save changes' : 'Create schedule'}
</button>
</div>
</Form>
@ -70,29 +73,60 @@ const EditScheduleModal = ({ values, schedule, ...props }: ComponentProps & Form
);
};
export default connect(
null,
// @ts-ignore
(dispatch: Actions<ApplicationStore>) => ({
addError: dispatch.flashes.addError,
}),
)(
withFormik<ComponentProps, Values>({
handleSubmit: (values, { props }) => {
},
export default ({ schedule, onScheduleUpdated, visible, ...props }: Props) => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const [ modalVisible, setModalVisible ] = useState(visible);
mapPropsToValues: ({ schedule }) => ({
name: schedule.name,
dayOfWeek: schedule.cron.dayOfWeek,
dayOfMonth: schedule.cron.dayOfMonth,
hour: schedule.cron.hour,
minute: schedule.cron.minute,
enabled: schedule.isActive,
}),
useEffect(() => {
setModalVisible(visible);
clearFlashes('schedule:edit');
}, [visible]);
validationSchema: object().shape({
name: string().required(),
enabled: boolean().required(),
}),
})(EditScheduleModal),
);
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);
onScheduleUpdated(schedule);
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>
);
};

View file

@ -21,7 +21,7 @@ export default ({ scheduleId, onTaskAdded }: Props) => {
}}
/>
}
<button className={'btn btn-primary btn-sm ml-4'} onClick={() => setVisible(true)}>
<button className={'btn btn-primary btn-sm'} onClick={() => setVisible(true)}>
New Task
</button>
</>

View file

@ -2,16 +2,18 @@ import React, { useMemo, useState } from 'react';
import getServerSchedules, { Schedule } from '@/api/server/schedules/getServerSchedules';
import { ServerContext } from '@/state/server';
import Spinner from '@/components/elements/Spinner';
import { RouteComponentProps, Link } from 'react-router-dom';
import { RouteComponentProps } from 'react-router-dom';
import FlashMessageRender from '@/components/FlashMessageRender';
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
import { httpErrorToHuman } from '@/api/http';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
export default ({ match, history }: RouteComponentProps) => {
const { uuid } = ServerContext.useStoreState(state => state.server.data!);
const [ schedules, setSchedules ] = useState<Schedule[] | null>(null);
const [ visible, setVisible ] = useState(false);
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
useMemo(() => {
@ -30,19 +32,44 @@ export default ({ match, history }: RouteComponentProps) => {
{!schedules ?
<Spinner size={'large'} centered={true}/>
:
schedules.map(schedule => (
<a
key={schedule.id}
href={`${match.url}/${schedule.id}`}
className={'grey-row-box cursor-pointer'}
onClick={e => {
e.preventDefault();
history.push(`${match.url}/${schedule.id}`, { schedule });
}}
>
<ScheduleRow schedule={schedule}/>
</a>
))
<>
{
schedules.length === 0 ?
<p className={'text-sm text-neutral-400'}>
There are no schedules configured for this server. Click the button below to get
started.
</p>
:
schedules.map(schedule => (
<a
key={schedule.id}
href={`${match.url}/${schedule.id}`}
className={'grey-row-box cursor-pointer mb-2'}
onClick={e => {
e.preventDefault();
history.push(`${match.url}/${schedule.id}`, { schedule });
}}
>
<ScheduleRow schedule={schedule}/>
</a>
))
}
<div className={'mt-8 flex justify-end'}>
{visible && <EditScheduleModal
appear={true}
visible={true}
onScheduleUpdated={schedule => setSchedules(s => [...(s || []), schedule])}
onDismissed={() => setVisible(false)}
/>}
<button
type={'button'}
className={'btn btn-lg btn-primary'}
onClick={() => setVisible(true)}
>
Create schedule
</button>
</div>
</>
}
</div>
);

View file

@ -12,6 +12,7 @@ import ScheduleRow from '@/components/server/schedules/ScheduleRow';
import ScheduleTaskRow from '@/components/server/schedules/ScheduleTaskRow';
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
import NewTaskButton from '@/components/server/schedules/NewTaskButton';
import DeleteScheduleButton from '@/components/server/schedules/DeleteScheduleButton';
interface Params {
id: string;
@ -21,8 +22,8 @@ interface State {
schedule?: Schedule;
}
export default ({ match, location: { state } }: RouteComponentProps<Params, {}, State>) => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
export default ({ match, history, location: { state } }: RouteComponentProps<Params, {}, State>) => {
const { id, uuid } = ServerContext.useStoreState(state => state.server.data!);
const [ isLoading, setIsLoading ] = useState(true);
const [ showEditModal, setShowEditModal ] = useState(false);
const [ schedule, setSchedule ] = useState<Schedule | undefined>(state?.schedule);
@ -57,21 +58,13 @@ export default ({ match, location: { state } }: RouteComponentProps<Params, {},
<EditScheduleModal
visible={showEditModal}
schedule={schedule}
onScheduleUpdated={schedule => setSchedule(schedule)}
onDismissed={() => setShowEditModal(false)}
/>
<div className={'flex items-center my-4'}>
<div className={'flex items-center mt-8 mb-4'}>
<div className={'flex-1'}>
<h2>Schedule Tasks</h2>
<h2>Configured Tasks</h2>
</div>
<button className={'btn btn-secondary btn-sm'} onClick={() => setShowEditModal(true)}>
Edit
</button>
<NewTaskButton
scheduleId={schedule.id}
onTaskAdded={task => setSchedule(s => ({
...s!, tasks: [ ...s!.tasks, task ],
}))}
/>
</div>
{schedule?.tasks.length > 0 ?
<>
@ -101,9 +94,24 @@ export default ({ match, location: { state } }: RouteComponentProps<Params, {},
:
<p className={'text-sm text-neutral-400'}>
There are no tasks configured for this schedule. Consider adding a new one using the
button above.
button below.
</p>
}
<div className={'mt-8 flex justify-end'}>
<DeleteScheduleButton
scheduleId={schedule.id}
onDeleted={() => history.push(`/server/${id}/schedules`)}
/>
<button className={'btn btn-primary btn-sm mr-4'} onClick={() => setShowEditModal(true)}>
Edit
</button>
<NewTaskButton
scheduleId={schedule.id}
onTaskAdded={task => setSchedule(s => ({
...s!, tasks: [ ...s!.tasks, task ],
}))}
/>
</div>
</>
}
</div>