2020-03-18 06:33:53 +00:00
|
|
|
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 Spinner from '@/components/elements/Spinner';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
|
|
|
|
import ScheduleTaskRow from '@/components/server/schedules/ScheduleTaskRow';
|
|
|
|
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
|
2020-03-19 05:36:19 +00:00
|
|
|
import NewTaskButton from '@/components/server/schedules/NewTaskButton';
|
2020-03-22 20:56:15 +00:00
|
|
|
import DeleteScheduleButton from '@/components/server/schedules/DeleteScheduleButton';
|
2020-03-30 05:20:27 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-10 17:46:00 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2020-07-05 00:00:19 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
2020-03-18 06:33:53 +00:00
|
|
|
|
|
|
|
interface Params {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
schedule?: Schedule;
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:30:50 +00:00
|
|
|
export default ({ match, history, location: { state } }: RouteComponentProps<Params, Record<string, unknown>, State>) => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const id = ServerContext.useStoreState(state => state.server.data!.id);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
|
2020-04-10 17:46:00 +00:00
|
|
|
const { clearFlashes, addError } = useFlash();
|
2020-03-18 06:33:53 +00:00
|
|
|
const [ isLoading, setIsLoading ] = useState(true);
|
|
|
|
const [ showEditModal, setShowEditModal ] = useState(false);
|
2020-04-10 17:46:00 +00:00
|
|
|
|
|
|
|
const schedule = ServerContext.useStoreState(st => st.schedules.data.find(s => s.id === state.schedule?.id), [ match ]);
|
|
|
|
const appendSchedule = ServerContext.useStoreActions(actions => actions.schedules.appendSchedule);
|
2020-03-18 06:33:53 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (schedule?.id === Number(match.params.id)) {
|
|
|
|
setIsLoading(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFlashes('schedules');
|
|
|
|
getServerSchedule(uuid, Number(match.params.id))
|
2020-04-10 17:46:00 +00:00
|
|
|
.then(schedule => appendSchedule(schedule))
|
2020-03-18 06:33:53 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ message: httpErrorToHuman(error), key: 'schedules' });
|
|
|
|
})
|
|
|
|
.then(() => setIsLoading(false));
|
2020-04-10 17:46:00 +00:00
|
|
|
}, [ match ]);
|
2020-03-18 06:33:53 +00:00
|
|
|
|
|
|
|
return (
|
2020-04-17 18:17:01 +00:00
|
|
|
<PageContentBlock>
|
2020-07-05 00:00:19 +00:00
|
|
|
<FlashMessageRender byKey={'schedules'} css={tw`mb-4`}/>
|
2020-03-18 06:33:53 +00:00
|
|
|
{!schedule || isLoading ?
|
2020-07-05 00:00:19 +00:00
|
|
|
<Spinner size={'large'} centered/>
|
2020-03-18 06:33:53 +00:00
|
|
|
:
|
|
|
|
<>
|
2020-07-05 00:00:19 +00:00
|
|
|
<GreyRowBox>
|
2020-03-18 06:33:53 +00:00
|
|
|
<ScheduleRow schedule={schedule}/>
|
2020-07-05 00:00:19 +00:00
|
|
|
</GreyRowBox>
|
2020-03-18 06:33:53 +00:00
|
|
|
<EditScheduleModal
|
|
|
|
visible={showEditModal}
|
|
|
|
schedule={schedule}
|
|
|
|
onDismissed={() => setShowEditModal(false)}
|
|
|
|
/>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`flex items-center mt-8 mb-4`}>
|
|
|
|
<div css={tw`flex-1`}>
|
|
|
|
<h2 css={tw`text-2xl`}>Configured Tasks</h2>
|
2020-03-18 06:33:53 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-10 17:46:00 +00:00
|
|
|
{schedule.tasks.length > 0 ?
|
2020-03-18 06:33:53 +00:00
|
|
|
<>
|
|
|
|
{
|
|
|
|
schedule.tasks
|
|
|
|
.sort((a, b) => a.sequenceId - b.sequenceId)
|
|
|
|
.map(task => (
|
2020-04-10 17:46:00 +00:00
|
|
|
<ScheduleTaskRow key={task.id} task={task} schedule={schedule}/>
|
2020-03-18 06:33:53 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
{schedule.tasks.length > 1 &&
|
2020-07-05 00:00:19 +00:00
|
|
|
<p css={tw`text-xs text-neutral-400`}>
|
2020-03-18 06:33:53 +00:00
|
|
|
Task delays are relative to the previous task in the listing.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
:
|
2020-07-05 00:00:19 +00:00
|
|
|
<p css={tw`text-sm text-neutral-400`}>
|
2020-03-30 05:20:27 +00:00
|
|
|
There are no tasks configured for this schedule.
|
2020-03-18 06:33:53 +00:00
|
|
|
</p>
|
|
|
|
}
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`mt-8 flex justify-end`}>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'schedule.delete'}>
|
|
|
|
<DeleteScheduleButton
|
|
|
|
scheduleId={schedule.id}
|
|
|
|
onDeleted={() => history.push(`/server/${id}/schedules`)}
|
|
|
|
/>
|
|
|
|
</Can>
|
|
|
|
<Can action={'schedule.update'}>
|
2020-07-05 00:00:19 +00:00
|
|
|
<Button css={tw`mr-4`} onClick={() => setShowEditModal(true)}>
|
2020-03-30 05:20:27 +00:00
|
|
|
Edit
|
2020-07-05 00:00:19 +00:00
|
|
|
</Button>
|
2020-04-10 17:46:00 +00:00
|
|
|
<NewTaskButton schedule={schedule}/>
|
2020-03-30 05:20:27 +00:00
|
|
|
</Can>
|
2020-03-22 20:56:15 +00:00
|
|
|
</div>
|
2020-03-18 06:33:53 +00:00
|
|
|
</>
|
|
|
|
}
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2020-03-18 06:33:53 +00:00
|
|
|
);
|
|
|
|
};
|