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

81 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
import { useHistory, useRouteMatch } from 'react-router-dom';
2020-02-08 23:23:08 +00:00
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';
2020-08-26 05:09:54 +00:00
import ServerContentBlock from '@/components/elements/ServerContentBlock';
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);
const { clearFlashes, addError } = useFlash();
const [ loading, setLoading ] = useState(true);
const [ visible, setVisible ] = useState(false);
2020-02-08 23:23:08 +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);
})
.then(() => setLoading(false));
}, []);
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`}/>
{(!schedules.length && loading) ?
2020-07-05 00:00:19 +00:00
<Spinner size={'large'} centered/>
2020-02-08 23:23:08 +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 => (
2020-07-05 00:00:19 +00:00
<GreyRowBox
as={'a'}
key={schedule.id}
href={`${match.url}/${schedule.id}`}
css={tw`cursor-pointer mb-2 flex-wrap`}
2020-07-05 00:00:19 +00:00
onClick={(e: any) => {
e.preventDefault();
history.push(`${match.url}/${schedule.id}`);
}}
>
<ScheduleRow schedule={schedule}/>
2020-07-05 00:00:19 +00:00
</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-02-08 23:23:08 +00:00
}
2020-08-26 05:09:54 +00:00
</ServerContentBlock>
2020-02-08 23:23:08 +00:00
);
};