2022-11-25 20:25:03 +00:00
|
|
|
import { useState } from 'react';
|
2020-11-01 04:22:44 +00:00
|
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Icon from '@/components/elements/Icon';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import deleteServerAllocation from '@/api/server/network/deleteServerAllocation';
|
|
|
|
import getServerAllocations from '@/api/swr/getServerAllocations';
|
2022-06-20 19:28:27 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-11-01 04:22:44 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
allocation: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeleteAllocationButton = ({ allocation }: Props) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [confirm, setConfirm] = useState(false);
|
2020-11-09 01:12:07 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const setServerFromState = ServerContext.useStoreActions(actions => actions.server.setServerFromState);
|
2020-11-09 01:12:07 +00:00
|
|
|
|
2020-11-01 04:22:44 +00:00
|
|
|
const { mutate } = getServerAllocations();
|
2022-06-20 19:28:27 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlashKey('server:network');
|
2020-11-01 04:22:44 +00:00
|
|
|
|
|
|
|
const deleteAllocation = () => {
|
2022-06-20 19:28:27 +00:00
|
|
|
clearFlashes();
|
2020-11-01 04:22:44 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
mutate(data => data?.filter(a => a.id !== allocation), false);
|
|
|
|
setServerFromState(s => ({ ...s, allocations: s.allocations.filter(a => a.id !== allocation) }));
|
2020-11-09 01:12:07 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
deleteServerAllocation(uuid, allocation).catch(error => {
|
2022-06-26 19:13:52 +00:00
|
|
|
clearAndAddHttpError(error);
|
|
|
|
mutate();
|
|
|
|
});
|
2020-11-01 04:22:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-20 19:28:27 +00:00
|
|
|
<Dialog.Confirm
|
|
|
|
open={confirm}
|
|
|
|
onClose={() => setConfirm(false)}
|
|
|
|
title={'Remove Allocation'}
|
|
|
|
confirm={'Delete'}
|
2020-11-01 04:22:44 +00:00
|
|
|
onConfirmed={deleteAllocation}
|
|
|
|
>
|
2022-06-20 19:28:27 +00:00
|
|
|
This allocation will be immediately removed from your server.
|
|
|
|
</Dialog.Confirm>
|
|
|
|
<Button.Danger
|
|
|
|
variant={Button.Variants.Secondary}
|
|
|
|
size={Button.Sizes.Small}
|
|
|
|
shape={Button.Shapes.IconSquare}
|
2020-11-01 04:22:44 +00:00
|
|
|
type={'button'}
|
|
|
|
onClick={() => setConfirm(true)}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Icon icon={faTrashAlt} css={tw`w-3 h-auto`} />
|
2022-06-20 19:28:27 +00:00
|
|
|
</Button.Danger>
|
2020-11-01 04:22:44 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DeleteAllocationButton;
|