2026-10-05 18:10:18 -04:00
|
|
|
import { useStore } from '@tanstack/react-form';
|
|
|
|
|
import { useAppForm, Form } from '@/components/form';
|
|
|
|
|
import type { AdminEgg } from '@/api/admin/eggs/queries';
|
|
|
|
|
import { deleteAdminEggInput, useDeleteAdminEgg } from '@/api/admin/eggs/queries';
|
|
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
egg: AdminEgg;
|
|
|
|
|
onDeleted: () => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteEggDialogProps = Props & {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// The API rejects deleting an egg that servers or child eggs still use.
|
|
|
|
|
function DeleteEggDialog({ egg, onDeleted, open, onClose }: DeleteEggDialogProps) {
|
|
|
|
|
const deleteEgg = useDeleteAdminEgg();
|
|
|
|
|
|
|
|
|
|
const form = useAppForm({
|
|
|
|
|
defaultValues: { confirm: '' },
|
|
|
|
|
onSubmit: async () => {
|
|
|
|
|
try {
|
|
|
|
|
await deleteEgg.mutateAsync(deleteAdminEggInput(egg.attributes.id, egg.attributes.name));
|
|
|
|
|
await onDeleted();
|
|
|
|
|
onClose();
|
|
|
|
|
} catch {
|
|
|
|
|
// Error toast is handled by the mutation.
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const isSubmitting = useStore(form.store, (state) => state.isSubmitting);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
2026-10-08 16:53:20 -04:00
|
|
|
title='Confirm egg deletion'
|
2026-10-05 18:10:18 -04:00
|
|
|
preventExternalClose={isSubmitting}
|
|
|
|
|
hideCloseIcon={isSubmitting}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
onClose();
|
|
|
|
|
form.reset();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<SpinnerOverlay visible={isSubmitting} />
|
2026-10-08 16:53:20 -04:00
|
|
|
<p className='text-sm'>
|
2026-10-05 18:10:18 -04:00
|
|
|
Deleting an egg is a permanent action, it cannot be undone. This will permanently delete the{' '}
|
|
|
|
|
<strong>{egg.attributes.name}</strong> egg. An egg with attached servers cannot be deleted.
|
|
|
|
|
</p>
|
2026-10-08 16:53:20 -04:00
|
|
|
<Form form={form} className='m-0 mt-6'>
|
2026-10-05 18:10:18 -04:00
|
|
|
<form.AppField
|
2026-10-08 16:53:20 -04:00
|
|
|
name='confirm'
|
2026-10-05 18:10:18 -04:00
|
|
|
validators={{
|
|
|
|
|
onChange: ({ value }) =>
|
|
|
|
|
value === egg.attributes.name ? undefined : 'The egg name must be provided.',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{(field) => (
|
|
|
|
|
<field.TextField
|
2026-10-08 16:53:20 -04:00
|
|
|
type='text'
|
|
|
|
|
id='confirm_egg_name'
|
|
|
|
|
label='Confirm Name'
|
|
|
|
|
description='Enter the name of this egg to confirm deletion.'
|
2026-10-05 18:10:18 -04:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</form.AppField>
|
2026-10-08 16:53:20 -04:00
|
|
|
<div className='mt-6 text-right'>
|
|
|
|
|
<Button type='button' isSecondary className='mr-2' onClick={onClose}>
|
2026-10-05 18:10:18 -04:00
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<form.AppForm>
|
2026-10-08 16:53:20 -04:00
|
|
|
<form.SubmitButton color='red'>Delete Egg</form.SubmitButton>
|
2026-10-05 18:10:18 -04:00
|
|
|
</form.AppForm>
|
|
|
|
|
</div>
|
|
|
|
|
</Form>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function DeleteEggButton({ egg, onDeleted }: Props) {
|
|
|
|
|
return (
|
|
|
|
|
<Dialog.Trigger
|
|
|
|
|
trigger={({ onClick }) => (
|
2026-10-08 16:53:20 -04:00
|
|
|
<Button color='red' isSecondary onClick={onClick}>
|
2026-10-05 18:10:18 -04:00
|
|
|
Delete Egg
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{({ open, onClose }) => <DeleteEggDialog egg={egg} onDeleted={onDeleted} open={open} onClose={onClose} />}
|
|
|
|
|
</Dialog.Trigger>
|
|
|
|
|
);
|
|
|
|
|
}
|