82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
|
|
import { Button } from "@/components/ui/button";
|
||
|
|
import { Trash2 } from "lucide-react";
|
||
|
|
import { useState } from "react";
|
||
|
|
import {
|
||
|
|
Dialog,
|
||
|
|
DialogContent,
|
||
|
|
DialogDescription,
|
||
|
|
DialogFooter,
|
||
|
|
DialogHeader,
|
||
|
|
DialogTitle,
|
||
|
|
} from "@/components/ui/dialog";
|
||
|
|
|
||
|
|
interface DeleteButtonProps {
|
||
|
|
onClick: () => void | Promise<void>;
|
||
|
|
loading?: boolean;
|
||
|
|
disabled?: boolean;
|
||
|
|
label?: string;
|
||
|
|
title?: string;
|
||
|
|
description?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function DeleteButton({
|
||
|
|
onClick,
|
||
|
|
loading = false,
|
||
|
|
disabled = false,
|
||
|
|
label = "Xóa",
|
||
|
|
title = "Xác nhận xóa",
|
||
|
|
description = "Bạn có chắc chắn muốn xóa các mục này không?",
|
||
|
|
}: DeleteButtonProps) {
|
||
|
|
const [open, setOpen] = useState(false);
|
||
|
|
const [isConfirming, setIsConfirming] = useState(false);
|
||
|
|
|
||
|
|
const handleConfirm = async () => {
|
||
|
|
setIsConfirming(true);
|
||
|
|
try {
|
||
|
|
await onClick();
|
||
|
|
} finally {
|
||
|
|
setIsConfirming(false);
|
||
|
|
setOpen(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<>
|
||
|
|
<Button
|
||
|
|
variant="destructive"
|
||
|
|
onClick={() => setOpen(true)}
|
||
|
|
disabled={loading || disabled}
|
||
|
|
className="gap-2"
|
||
|
|
>
|
||
|
|
<Trash2 className="h-4 w-4" />
|
||
|
|
{label}
|
||
|
|
</Button>
|
||
|
|
|
||
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
||
|
|
<DialogContent>
|
||
|
|
<DialogHeader>
|
||
|
|
<DialogTitle>{title}</DialogTitle>
|
||
|
|
<DialogDescription>{description}</DialogDescription>
|
||
|
|
</DialogHeader>
|
||
|
|
<DialogFooter>
|
||
|
|
<Button
|
||
|
|
variant="outline"
|
||
|
|
onClick={() => setOpen(false)}
|
||
|
|
disabled={isConfirming}
|
||
|
|
>
|
||
|
|
Hủy
|
||
|
|
</Button>
|
||
|
|
<Button
|
||
|
|
variant="destructive"
|
||
|
|
onClick={handleConfirm}
|
||
|
|
disabled={isConfirming || loading}
|
||
|
|
>
|
||
|
|
{isConfirming ? "Đang xóa..." : "Xóa"}
|
||
|
|
</Button>
|
||
|
|
</DialogFooter>
|
||
|
|
</DialogContent>
|
||
|
|
</Dialog>
|
||
|
|
</>
|
||
|
|
);
|
||
|
|
}
|