2020-03-22 20:56:15 +00:00
|
|
|
import React, { useState } from 'react';
|
2022-02-27 17:35:27 +00:00
|
|
|
import { deleteSchedule } from '@/api/server/schedules';
|
2020-03-22 20:56:15 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-07-05 00:00:19 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
2020-03-22 20:56:15 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
scheduleId: number;
|
|
|
|
onDeleted: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ scheduleId, onDeleted }: Props) => {
|
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const [ isLoading, setIsLoading ] = useState(false);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const onDelete = () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
clearFlashes('schedules');
|
|
|
|
deleteSchedule(uuid, scheduleId)
|
|
|
|
.then(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
onDeleted();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
addError({ key: 'schedules', message: httpErrorToHuman(error) });
|
|
|
|
setIsLoading(false);
|
|
|
|
setVisible(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-07-05 00:00:19 +00:00
|
|
|
<ConfirmationModal
|
2020-08-18 04:35:11 +00:00
|
|
|
visible={visible}
|
2020-07-05 00:00:19 +00:00
|
|
|
title={'Delete schedule?'}
|
|
|
|
buttonText={'Yes, delete schedule'}
|
|
|
|
onConfirmed={onDelete}
|
2020-08-18 04:35:11 +00:00
|
|
|
showSpinnerOverlay={isLoading}
|
|
|
|
onModalDismissed={() => setVisible(false)}
|
2020-03-22 20:56:15 +00:00
|
|
|
>
|
2020-07-05 00:00:19 +00:00
|
|
|
Are you sure you want to delete this schedule? All tasks will be removed and any running processes
|
|
|
|
will be terminated.
|
|
|
|
</ConfirmationModal>
|
2020-10-15 03:13:36 +00:00
|
|
|
<Button css={tw`flex-1 sm:flex-none mr-4 border-transparent`} color={'red'} isSecondary onClick={() => setVisible(true)}>
|
2020-03-22 20:56:15 +00:00
|
|
|
Delete
|
2020-07-05 00:00:19 +00:00
|
|
|
</Button>
|
2020-03-22 20:56:15 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|