misc_pterodactyl-panel/resources/scripts/components/server/schedules/DeleteScheduleButton.tsx
Dane Everitt c28cba92e2
Make modals programatically controllable via a HOC
This allows entire components to be unmounted when the modal is hidden without affecting the fade in/out of the modal itself.

This also makes it easier to programatically dismiss a modal without having to copy the visibility all over the place, and makes working with props much simpler in those modal components
2020-08-17 21:35:11 -07:00

57 lines
2 KiB
TypeScript

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';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import ConfirmationModal from '@/components/elements/ConfirmationModal';
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 (
<>
<ConfirmationModal
visible={visible}
title={'Delete schedule?'}
buttonText={'Yes, delete schedule'}
onConfirmed={onDelete}
showSpinnerOverlay={isLoading}
onModalDismissed={() => setVisible(false)}
>
Are you sure you want to delete this schedule? All tasks will be removed and any running processes
will be terminated.
</ConfirmationModal>
<Button css={tw`mr-4`} color={'red'} isSecondary onClick={() => setVisible(true)}>
Delete
</Button>
</>
);
};