Clean up most of the schedules code to use server context
This commit is contained in:
parent
07d19ad326
commit
0ebf842757
9 changed files with 132 additions and 92 deletions
|
@ -2,11 +2,8 @@ import React, { useEffect, useState } from 'react';
|
|||
import { RouteComponentProps } from 'react-router-dom';
|
||||
import { Schedule } from '@/api/server/schedules/getServerSchedules';
|
||||
import getServerSchedule from '@/api/server/schedules/getServerSchedule';
|
||||
import { ServerContext } from '@/state/server';
|
||||
import Spinner from '@/components/elements/Spinner';
|
||||
import FlashMessageRender from '@/components/FlashMessageRender';
|
||||
import { Actions, useStoreActions } from 'easy-peasy';
|
||||
import { ApplicationStore } from '@/state';
|
||||
import { httpErrorToHuman } from '@/api/http';
|
||||
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
|
||||
import ScheduleTaskRow from '@/components/server/schedules/ScheduleTaskRow';
|
||||
|
@ -14,6 +11,9 @@ import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
|
|||
import NewTaskButton from '@/components/server/schedules/NewTaskButton';
|
||||
import DeleteScheduleButton from '@/components/server/schedules/DeleteScheduleButton';
|
||||
import Can from '@/components/elements/Can';
|
||||
import useServer from '@/plugins/useServer';
|
||||
import useFlash from '@/plugins/useFlash';
|
||||
import { ServerContext } from '@/state/server';
|
||||
|
||||
interface Params {
|
||||
id: string;
|
||||
|
@ -24,11 +24,13 @@ interface State {
|
|||
}
|
||||
|
||||
export default ({ match, history, location: { state } }: RouteComponentProps<Params, {}, State>) => {
|
||||
const { id, uuid } = ServerContext.useStoreState(state => state.server.data!);
|
||||
const { id, uuid } = useServer();
|
||||
const { clearFlashes, addError } = useFlash();
|
||||
const [ isLoading, setIsLoading ] = useState(true);
|
||||
const [ showEditModal, setShowEditModal ] = useState(false);
|
||||
const [ schedule, setSchedule ] = useState<Schedule | undefined>(state?.schedule);
|
||||
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
||||
|
||||
const schedule = ServerContext.useStoreState(st => st.schedules.data.find(s => s.id === state.schedule?.id), [ match ]);
|
||||
const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule);
|
||||
|
||||
useEffect(() => {
|
||||
if (schedule?.id === Number(match.params.id)) {
|
||||
|
@ -38,13 +40,13 @@ export default ({ match, history, location: { state } }: RouteComponentProps<Par
|
|||
|
||||
clearFlashes('schedules');
|
||||
getServerSchedule(uuid, Number(match.params.id))
|
||||
.then(schedule => setSchedule(schedule))
|
||||
.then(schedule => appendSchedule(schedule))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
addError({ message: httpErrorToHuman(error), key: 'schedules' });
|
||||
})
|
||||
.then(() => setIsLoading(false));
|
||||
}, [ schedule, match ]);
|
||||
}, [ match ]);
|
||||
|
||||
return (
|
||||
<div className={'my-10 mb-6'}>
|
||||
|
@ -59,7 +61,6 @@ export default ({ match, history, location: { state } }: RouteComponentProps<Par
|
|||
<EditScheduleModal
|
||||
visible={showEditModal}
|
||||
schedule={schedule}
|
||||
onScheduleUpdated={schedule => setSchedule(schedule)}
|
||||
onDismissed={() => setShowEditModal(false)}
|
||||
/>
|
||||
<div className={'flex items-center mt-8 mb-4'}>
|
||||
|
@ -67,23 +68,13 @@ export default ({ match, history, location: { state } }: RouteComponentProps<Par
|
|||
<h2>Configured Tasks</h2>
|
||||
</div>
|
||||
</div>
|
||||
{schedule?.tasks.length > 0 ?
|
||||
{schedule.tasks.length > 0 ?
|
||||
<>
|
||||
{
|
||||
schedule.tasks
|
||||
.sort((a, b) => a.sequenceId - b.sequenceId)
|
||||
.map(task => (
|
||||
<ScheduleTaskRow
|
||||
key={task.id}
|
||||
task={task}
|
||||
schedule={schedule.id}
|
||||
onTaskUpdated={task => setSchedule(s => ({
|
||||
...s!, tasks: s!.tasks.map(t => t.id === task.id ? task : t),
|
||||
}))}
|
||||
onTaskRemoved={() => setSchedule(s => ({
|
||||
...s!, tasks: s!.tasks.filter(t => t.id !== task.id),
|
||||
}))}
|
||||
/>
|
||||
<ScheduleTaskRow key={task.id} task={task} schedule={schedule}/>
|
||||
))
|
||||
}
|
||||
{schedule.tasks.length > 1 &&
|
||||
|
@ -108,12 +99,7 @@ export default ({ match, history, location: { state } }: RouteComponentProps<Par
|
|||
<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 ],
|
||||
}))}
|
||||
/>
|
||||
<NewTaskButton schedule={schedule}/>
|
||||
</Can>
|
||||
</div>
|
||||
</>
|
||||
|
|
Reference in a new issue