"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslations } from "next-intl";
import { toast } from "sonner";
import { Trash2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
export function DeleteCollectionDialog({ collectionId }: { collectionId: string }) {
const t = useTranslations("collections");
const tCommon = useTranslations("common");
const router = useRouter();
const [open, setOpen] = useState(false);
const [deleteRecipes, setDeleteRecipes] = useState(false);
const [deleting, setDeleting] = useState(false);
async function handleDelete() {
setDeleting(true);
try {
const res = await fetch(`/api/v1/collections/${collectionId}?deleteRecipes=${deleteRecipes}`, { method: "DELETE" });
if (!res.ok) throw new Error();
toast.success(t("deleteSuccess"));
router.push("/collections");
router.refresh();
} catch {
toast.error(t("deleteFailed"));
setDeleting(false);
}
}
return (
<>
setOpen(true)} aria-label={tCommon("delete")}>
} />
{tCommon("delete")}
{t("deleteConfirmTitle")}
{t("deleteConfirmDescription")}
{tCommon("cancel")}
{ e.preventDefault(); void handleDelete(); }}
disabled={deleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{deleting ? t("deleting") : tCommon("delete")}
>
);
}