48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { router } from '@inertiajs/vue3';
|
|
import { useConfirm } from '@/composables/useConfirm';
|
|
import { useTranslations } from '@/composables/useVanguard';
|
|
|
|
interface ConfirmDeleteOptions {
|
|
/** Endpoint to DELETE. */
|
|
url: string;
|
|
/** Dialog heading. Defaults to "Please Confirm". */
|
|
title?: string;
|
|
/** Dialog body copy. */
|
|
message: string;
|
|
/** Confirm button label. Defaults to "Yes, delete it!". */
|
|
confirmLabel?: string;
|
|
/** HTTP verb, for the handful of destructive actions that are not DELETE. */
|
|
method?: 'delete' | 'put' | 'post';
|
|
onSuccess?: () => void;
|
|
}
|
|
|
|
/**
|
|
* Confirm a destructive action, then send it through the Inertia router.
|
|
*
|
|
* Every delete in the application goes through here, so the confirmation copy
|
|
* and the request shape stay consistent.
|
|
*/
|
|
export function useConfirmDelete() {
|
|
const confirm = useConfirm();
|
|
const { __ } = useTranslations();
|
|
|
|
function confirmDelete(options: ConfirmDeleteOptions) {
|
|
confirm.require({
|
|
header: options.title ?? __('Please Confirm'),
|
|
message: options.message,
|
|
cancelLabel: __('Cancel'),
|
|
confirmLabel: options.confirmLabel ?? __('Yes, delete it!'),
|
|
severity: 'danger',
|
|
accept: () => {
|
|
router[options.method ?? 'delete'](
|
|
options.url,
|
|
{},
|
|
{ preserveScroll: true, onSuccess: options.onSuccess },
|
|
);
|
|
},
|
|
});
|
|
}
|
|
|
|
return { confirmDelete };
|
|
}
|