66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import type { ComponentProps, ReactNode } from "react";
|
|
import { toast } from "sonner";
|
|
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
AlertDialogTrigger,
|
|
} from "@/components/ui/alert-dialog";
|
|
|
|
interface DeleteConfirmationDialogProps {
|
|
title: ReactNode;
|
|
description: ReactNode;
|
|
onConfirm: () => void | Promise<void>;
|
|
confirmLabel?: ReactNode;
|
|
trigger?: ComponentProps<typeof AlertDialogTrigger>["render"];
|
|
children?: ReactNode;
|
|
open?: ComponentProps<typeof AlertDialog>["open"];
|
|
onOpenChange?: ComponentProps<typeof AlertDialog>["onOpenChange"];
|
|
showSuccessToast?: boolean;
|
|
}
|
|
|
|
export function DeleteConfirmationDialog({
|
|
title,
|
|
description,
|
|
onConfirm,
|
|
confirmLabel = "ยืนยันลบ",
|
|
trigger,
|
|
children,
|
|
open,
|
|
onOpenChange,
|
|
showSuccessToast = true,
|
|
}: DeleteConfirmationDialogProps) {
|
|
const handleConfirm = async () => {
|
|
await onConfirm();
|
|
if (showSuccessToast) toast.success("ลบสำเร็จ!");
|
|
};
|
|
|
|
return (
|
|
<AlertDialog open={open} onOpenChange={onOpenChange}>
|
|
{trigger ? (
|
|
<AlertDialogTrigger render={trigger}>{children}</AlertDialogTrigger>
|
|
) : null}
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>{title}</AlertDialogTitle>
|
|
<AlertDialogDescription>{description}</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>ยกเลิก</AlertDialogCancel>
|
|
<AlertDialogAction variant="destructive" onClick={handleConfirm}>
|
|
{confirmLabel}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
);
|
|
}
|