2022-12-16 02:06:14 +00:00
|
|
|
import type { Actions } from 'easy-peasy';
|
|
|
|
import { useStoreActions } from 'easy-peasy';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
|
|
|
|
import deleteEgg from '@/api/admin/eggs/deleteEgg';
|
2022-12-21 21:27:50 +00:00
|
|
|
import { Button } from '@/components/elements/button';
|
|
|
|
import { Shape } from '@/components/elements/button/types';
|
2022-12-16 02:06:14 +00:00
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
|
|
|
import type { ApplicationStore } from '@/state';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
eggId: number;
|
|
|
|
onDeleted: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ eggId, onDeleted }: Props) => {
|
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions(
|
|
|
|
(actions: Actions<ApplicationStore>) => actions.flashes,
|
|
|
|
);
|
|
|
|
|
|
|
|
const onDelete = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('egg');
|
|
|
|
|
|
|
|
deleteEgg(eggId)
|
|
|
|
.then(() => {
|
|
|
|
setLoading(false);
|
|
|
|
onDeleted();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'egg', error });
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
setVisible(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmationModal
|
|
|
|
visible={visible}
|
|
|
|
title={'Delete egg?'}
|
|
|
|
buttonText={'Yes, delete egg'}
|
|
|
|
onConfirmed={onDelete}
|
|
|
|
showSpinnerOverlay={loading}
|
|
|
|
onModalDismissed={() => setVisible(false)}
|
|
|
|
>
|
|
|
|
Are you sure you want to delete this egg? You may only delete an egg with no servers using it.
|
|
|
|
</ConfirmationModal>
|
|
|
|
|
2022-12-21 21:27:50 +00:00
|
|
|
<Button.Danger type="button" shape={Shape.IconSquare} onClick={() => setVisible(true)}>
|
2022-12-16 02:06:14 +00:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
stroke="currentColor"
|
|
|
|
css={tw`h-5 w-5`}
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
strokeWidth={2}
|
|
|
|
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
|
|
|
/>
|
|
|
|
</svg>
|
2022-12-21 21:27:50 +00:00
|
|
|
</Button.Danger>
|
2022-12-16 02:06:14 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|