2020-04-10 19:41:08 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import getServerSchedules from '@/api/server/schedules/getServerSchedules';
|
2020-02-08 23:23:08 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2021-01-01 17:21:13 +00:00
|
|
|
import { useHistory, useRouteMatch } from 'react-router-dom';
|
2020-02-08 23:23:08 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-02-12 07:23:06 +00:00
|
|
|
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-03-22 20:56:15 +00:00
|
|
|
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
|
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';
|
2020-07-05 00:00:19 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
|
|
|
import Button from '@/components/elements/Button';
|
2020-08-26 05:09:54 +00:00
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
2020-02-12 07:23:06 +00:00
|
|
|
|
2021-01-01 17:21:13 +00:00
|
|
|
export default () => {
|
|
|
|
const match = useRouteMatch();
|
|
|
|
const history = useHistory();
|
|
|
|
|
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 { clearFlashes, addError } = useFlash();
|
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-03-22 20:56:15 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
2020-02-08 23:23:08 +00:00
|
|
|
|
2020-04-10 17:46:00 +00:00
|
|
|
const schedules = ServerContext.useStoreState(state => state.schedules.data);
|
|
|
|
const setSchedules = ServerContext.useStoreActions(actions => actions.schedules.setSchedules);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-02-08 23:23:08 +00:00
|
|
|
clearFlashes('schedules');
|
|
|
|
getServerSchedules(uuid)
|
|
|
|
.then(schedules => setSchedules(schedules))
|
|
|
|
.catch(error => {
|
|
|
|
addError({ message: httpErrorToHuman(error), key: 'schedules' });
|
|
|
|
console.error(error);
|
2020-04-10 17:46:00 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, []);
|
2020-02-12 07:23:06 +00:00
|
|
|
|
2020-02-08 23:23:08 +00:00
|
|
|
return (
|
2020-08-26 05:09:54 +00:00
|
|
|
<ServerContentBlock title={'Schedules'}>
|
2020-07-05 00:00:19 +00:00
|
|
|
<FlashMessageRender byKey={'schedules'} css={tw`mb-4`}/>
|
2020-04-10 17:46:00 +00:00
|
|
|
{(!schedules.length && loading) ?
|
2020-07-05 00:00:19 +00:00
|
|
|
<Spinner size={'large'} centered/>
|
2020-02-08 23:23:08 +00:00
|
|
|
:
|
2020-03-22 20:56:15 +00:00
|
|
|
<>
|
|
|
|
{
|
|
|
|
schedules.length === 0 ?
|
2020-11-09 02:09:22 +00:00
|
|
|
<p css={tw`text-sm text-center text-neutral-300`}>
|
2020-03-30 05:20:27 +00:00
|
|
|
There are no schedules configured for this server.
|
2020-03-22 20:56:15 +00:00
|
|
|
</p>
|
|
|
|
:
|
|
|
|
schedules.map(schedule => (
|
2020-07-05 00:00:19 +00:00
|
|
|
<GreyRowBox
|
|
|
|
as={'a'}
|
2020-03-22 20:56:15 +00:00
|
|
|
key={schedule.id}
|
|
|
|
href={`${match.url}/${schedule.id}`}
|
2020-09-13 17:02:25 +00:00
|
|
|
css={tw`cursor-pointer mb-2 flex-wrap`}
|
2020-07-05 00:00:19 +00:00
|
|
|
onClick={(e: any) => {
|
2020-03-22 20:56:15 +00:00
|
|
|
e.preventDefault();
|
2021-08-03 03:07:39 +00:00
|
|
|
history.push(`${match.url}/${schedule.id}`);
|
2020-03-22 20:56:15 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ScheduleRow schedule={schedule}/>
|
2020-07-05 00:00:19 +00:00
|
|
|
</GreyRowBox>
|
2020-03-22 20:56:15 +00:00
|
|
|
))
|
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'schedule.create'}>
|
2020-07-05 00:00:19 +00:00
|
|
|
<div css={tw`mt-8 flex justify-end`}>
|
2021-05-01 18:24:18 +00:00
|
|
|
<EditScheduleModal visible={visible} onModalDismissed={() => setVisible(false)}/>
|
2020-07-05 00:00:19 +00:00
|
|
|
<Button type={'button'} onClick={() => setVisible(true)}>
|
2020-03-30 05:20:27 +00:00
|
|
|
Create schedule
|
2020-07-05 00:00:19 +00:00
|
|
|
</Button>
|
2020-03-30 05:20:27 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2020-03-22 20:56:15 +00:00
|
|
|
</>
|
2020-02-08 23:23:08 +00:00
|
|
|
}
|
2020-08-26 05:09:54 +00:00
|
|
|
</ServerContentBlock>
|
2020-02-08 23:23:08 +00:00
|
|
|
);
|
|
|
|
};
|