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

50 lines
2 KiB
TypeScript
Raw Normal View History

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-03-18 06:33:53 +00:00
import { RouteComponentProps, Link } 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';
2020-02-08 23:23:08 +00:00
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
2020-03-18 06:33:53 +00:00
export default ({ match, history }: RouteComponentProps) => {
const { uuid } = ServerContext.useStoreState(state => state.server.data!);
2020-02-08 23:23:08 +00:00
const [ schedules, setSchedules ] = useState<Schedule[] | null>(null);
const { clearFlashes, addError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2020-02-08 23:23:08 +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);
});
}, [ setSchedules ]);
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-03-18 06:33:53 +00:00
<a
key={schedule.id}
2020-03-18 06:33:53 +00:00
href={`${match.url}/${schedule.id}`}
className={'grey-row-box cursor-pointer'}
2020-03-18 06:33:53 +00:00
onClick={e => {
e.preventDefault();
history.push(`${match.url}/${schedule.id}`, { schedule });
}}
>
<ScheduleRow schedule={schedule}/>
2020-03-18 06:33:53 +00:00
</a>
2020-02-08 23:23:08 +00:00
))
}
</div>
);
};