2020-03-22 20:56:15 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import deleteSchedule from '@/api/server/schedules/deleteSchedule';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2020-03-22 20:56:15 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
scheduleId: number;
|
|
|
|
onDeleted: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ scheduleId, onDeleted }: Props) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
2020-03-22 20:56:15 +00:00
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const onDelete = () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
clearFlashes('schedules');
|
|
|
|
deleteSchedule(uuid, scheduleId)
|
|
|
|
.then(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
onDeleted();
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-03-22 20:56:15 +00:00
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
addError({ key: 'schedules', message: httpErrorToHuman(error) });
|
|
|
|
setIsLoading(false);
|
|
|
|
setVisible(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-20 19:28:27 +00:00
|
|
|
<Dialog.Confirm
|
|
|
|
open={visible}
|
|
|
|
onClose={() => setVisible(false)}
|
|
|
|
title={'Delete Schedule'}
|
|
|
|
confirm={'Delete'}
|
2020-07-05 00:00:19 +00:00
|
|
|
onConfirmed={onDelete}
|
2020-03-22 20:56:15 +00:00
|
|
|
>
|
2022-06-20 19:28:27 +00:00
|
|
|
<SpinnerOverlay visible={isLoading} />
|
|
|
|
All tasks will be removed and any running processes will be terminated.
|
|
|
|
</Dialog.Confirm>
|
|
|
|
<Button.Danger
|
|
|
|
variant={Button.Variants.Secondary}
|
|
|
|
className={'flex-1 sm:flex-none mr-4 border-transparent'}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
>
|
2020-03-22 20:56:15 +00:00
|
|
|
Delete
|
2022-06-20 19:28:27 +00:00
|
|
|
</Button.Danger>
|
2020-03-22 20:56:15 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|