2020-02-12 07:23:06 +00:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2020-02-08 23:23:08 +00:00
|
|
|
import getServerSchedules, { Schedule } from '@/api/server/schedules/getServerSchedules';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-02-23 04:07:56 +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-02-08 23:23:08 +00:00
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
2020-02-12 07:23:06 +00:00
|
|
|
import EditScheduleModal from '@/components/server/schedules/EditScheduleModal';
|
|
|
|
|
|
|
|
interface Params {
|
|
|
|
schedule?: string;
|
|
|
|
}
|
2019-12-07 23:58:37 +00:00
|
|
|
|
2020-02-23 04:07:56 +00:00
|
|
|
export default ({ history, match }: RouteComponentProps<Params>) => {
|
2020-02-08 23:23:08 +00:00
|
|
|
const { id, uuid } = ServerContext.useStoreState(state => state.server.data!);
|
2020-02-23 04:07:56 +00:00
|
|
|
const [ active, setActive ] = useState(0);
|
2020-02-08 23:23:08 +00:00
|
|
|
const [ schedules, setSchedules ] = useState<Schedule[] | null>(null);
|
2020-02-12 07:23:06 +00:00
|
|
|
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2020-02-08 23:23:08 +00:00
|
|
|
|
2020-02-12 07:23:06 +00:00
|
|
|
useMemo(() => {
|
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-02-12 07:23:06 +00:00
|
|
|
}, [ setSchedules ]);
|
|
|
|
|
2020-02-23 04:07:56 +00:00
|
|
|
const matched = useMemo(() => {
|
|
|
|
return schedules?.find(schedule => schedule.id === active);
|
|
|
|
}, [ active ]);
|
2020-02-08 23:23:08 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={'my-10 mb-6'}>
|
|
|
|
<FlashMessageRender byKey={'schedules'} className={'mb-4'}/>
|
|
|
|
{!schedules ?
|
|
|
|
<Spinner size={'large'} centered={true}/>
|
|
|
|
:
|
|
|
|
schedules.map(schedule => (
|
2020-02-23 04:07:56 +00:00
|
|
|
<div
|
2020-02-12 07:23:06 +00:00
|
|
|
key={schedule.id}
|
2020-02-23 04:07:56 +00:00
|
|
|
onClick={() => setActive(schedule.id)}
|
|
|
|
className={'grey-row-box cursor-pointer'}
|
2020-02-12 07:23:06 +00:00
|
|
|
>
|
|
|
|
<ScheduleRow schedule={schedule}/>
|
2020-02-23 04:07:56 +00:00
|
|
|
</div>
|
2020-02-08 23:23:08 +00:00
|
|
|
))
|
|
|
|
}
|
2020-02-12 07:23:06 +00:00
|
|
|
{matched &&
|
|
|
|
<EditScheduleModal
|
|
|
|
schedule={matched}
|
|
|
|
visible={true}
|
|
|
|
appear={true}
|
2020-02-23 04:07:56 +00:00
|
|
|
onDismissed={() => setActive(0)}
|
2020-02-12 07:23:06 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-02-08 23:23:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|