misc_pterodactyl-panel/resources/scripts/components/server/schedules/ScheduleContainer.tsx

77 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { 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';
import FlashMessageRender from '@/components/FlashMessageRender';
import ScheduleRow from '@/components/server/schedules/ScheduleRow';
import { httpErrorToHuman } from '@/api/http';
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
import Can from '@/components/elements/Can';
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/index';
2020-08-26 05:09:54 +00:00
import ServerContentBlock from '@/components/elements/ServerContentBlock';
2022-11-25 20:25:03 +00:00
import { Link } from 'react-router-dom';
2022-11-25 20:25:03 +00:00
function ScheduleContainer() {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const { clearFlashes, addError } = useFlash();
const [loading, setLoading] = useState(true);
const [visible, setVisible] = useState(false);
2020-02-08 23:23:08 +00:00
2022-11-25 20:25:03 +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');
2022-11-25 20:25:03 +00:00
2020-02-08 23:23:08 +00:00
getServerSchedules(uuid)
2022-11-25 20:25:03 +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);
})
.then(() => setLoading(false));
}, []);
2020-02-08 23:23:08 +00:00
return (
2020-08-26 05:09:54 +00:00
<ServerContentBlock title={'Schedules'}>
<FlashMessageRender byKey={'schedules'} css={tw`mb-4`} />
{!schedules.length && loading ? (
<Spinner size={'large'} centered />
) : (
<>
{schedules.length === 0 ? (
<p css={tw`text-sm text-center text-neutral-300`}>
There are no schedules configured for this server.
</p>
) : (
2022-11-25 20:25:03 +00:00
schedules.map(schedule => (
// @ts-expect-error go away
<GreyRowBox
key={schedule.id}
2022-11-25 20:25:03 +00:00
as={Link}
to={schedule.id}
css={tw`cursor-pointer mb-2 flex-wrap`}
>
<ScheduleRow schedule={schedule} />
</GreyRowBox>
))
)}
<Can action={'schedule.create'}>
2020-07-05 00:00:19 +00:00
<div css={tw`mt-8 flex justify-end`}>
<EditScheduleModal visible={visible} onModalDismissed={() => setVisible(false)} />
2020-07-05 00:00:19 +00:00
<Button type={'button'} onClick={() => setVisible(true)}>
Create schedule
2020-07-05 00:00:19 +00:00
</Button>
</div>
</Can>
</>
)}
2020-08-26 05:09:54 +00:00
</ServerContentBlock>
2020-02-08 23:23:08 +00:00
);
2022-11-25 20:25:03 +00:00
}
export default ScheduleContainer;