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';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
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();
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
2020-04-10 17:46:00 +00:00
|
|
|
const { clearFlashes, addError } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [visible, setVisible] = useState(false);
|
2020-02-08 23:23:08 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const schedules = ServerContext.useStoreState((state) => state.schedules.data);
|
|
|
|
const setSchedules = ServerContext.useStoreActions((actions) => actions.schedules.setSchedules);
|
2020-04-10 17:46:00 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-02-08 23:23:08 +00:00
|
|
|
clearFlashes('schedules');
|
|
|
|
getServerSchedules(uuid)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((schedules) => setSchedules(schedules))
|
|
|
|
.catch((error) => {
|
2020-02-08 23:23:08 +00:00
|
|
|
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'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender byKey={'schedules'} css={tw`mb-4`} />
|
|
|
|
{!schedules.length && loading ? (
|
|
|
|
<Spinner size={'large'} centered />
|
|
|
|
) : (
|
2020-03-22 20:56:15 +00:00
|
|
|
<>
|
2022-06-26 19:13:52 +00:00
|
|
|
{schedules.length === 0 ? (
|
|
|
|
<p css={tw`text-sm text-center text-neutral-300`}>
|
|
|
|
There are no schedules configured for this server.
|
|
|
|
</p>
|
|
|
|
) : (
|
|
|
|
schedules.map((schedule) => (
|
|
|
|
<GreyRowBox
|
|
|
|
as={'a'}
|
|
|
|
key={schedule.id}
|
|
|
|
href={`${match.url}/${schedule.id}`}
|
|
|
|
css={tw`cursor-pointer mb-2 flex-wrap`}
|
|
|
|
onClick={(e: any) => {
|
|
|
|
e.preventDefault();
|
|
|
|
history.push(`${match.url}/${schedule.id}`);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ScheduleRow schedule={schedule} />
|
|
|
|
</GreyRowBox>
|
|
|
|
))
|
|
|
|
)}
|
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`}>
|
2022-06-26 19:13:52 +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
|
|
|
</>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-08-26 05:09:54 +00:00
|
|
|
</ServerContentBlock>
|
2020-02-08 23:23:08 +00:00
|
|
|
);
|
|
|
|
};
|