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';
|
2020-03-22 20:56:15 +00:00
|
|
|
import { RouteComponentProps } 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 useServer from '@/plugins/useServer';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-02-12 07:23:06 +00:00
|
|
|
|
2020-03-18 06:33:53 +00:00
|
|
|
export default ({ match, history }: RouteComponentProps) => {
|
2020-04-10 17:46:00 +00:00
|
|
|
const { uuid } = useServer();
|
|
|
|
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 (
|
|
|
|
<div className={'my-10 mb-6'}>
|
|
|
|
<FlashMessageRender byKey={'schedules'} className={'mb-4'}/>
|
2020-04-10 17:46:00 +00:00
|
|
|
{(!schedules.length && loading) ?
|
2020-02-08 23:23:08 +00:00
|
|
|
<Spinner size={'large'} centered={true}/>
|
|
|
|
:
|
2020-03-22 20:56:15 +00:00
|
|
|
<>
|
|
|
|
{
|
|
|
|
schedules.length === 0 ?
|
2020-03-30 05:20:27 +00:00
|
|
|
<p className={'text-sm text-center text-neutral-400'}>
|
|
|
|
There are no schedules configured for this server.
|
2020-03-22 20:56:15 +00:00
|
|
|
</p>
|
|
|
|
:
|
|
|
|
schedules.map(schedule => (
|
|
|
|
<a
|
|
|
|
key={schedule.id}
|
|
|
|
href={`${match.url}/${schedule.id}`}
|
|
|
|
className={'grey-row-box cursor-pointer mb-2'}
|
|
|
|
onClick={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
history.push(`${match.url}/${schedule.id}`, { schedule });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ScheduleRow schedule={schedule}/>
|
|
|
|
</a>
|
|
|
|
))
|
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'schedule.create'}>
|
|
|
|
<div className={'mt-8 flex justify-end'}>
|
|
|
|
{visible && <EditScheduleModal
|
|
|
|
appear={true}
|
|
|
|
visible={true}
|
|
|
|
onDismissed={() => setVisible(false)}
|
|
|
|
/>}
|
|
|
|
<button
|
|
|
|
type={'button'}
|
|
|
|
className={'btn btn-sm btn-primary'}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
>
|
|
|
|
Create schedule
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Can>
|
2020-03-22 20:56:15 +00:00
|
|
|
</>
|
2020-02-08 23:23:08 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|