"use client"
import { useState } from "react"
import { useRouter } from "next/navigation"
import { Trash2 } from "lucide-react"
import { ConfirmModal } from "@/components/ui/confirm-modal"
import { toast } from "sonner"
interface DeleteButtonProps {
id: string
endpoint: string
label?: string
onDeleted?: () => void
}
export function DeleteButton({ id, endpoint, label = "this item", onDeleted }: DeleteButtonProps) {
const router = useRouter()
const [open, setOpen] = useState(false)
const [loading, setLoading] = useState(false)
async function handleDelete() {
setLoading(true)
try {
const res = await fetch(`${endpoint}/${id}`, { method: "DELETE" })
if (!res.ok) {
const data = await res.json().catch(() => null)
toast.error(data?.error ?? "Failed to delete")
return
}
toast.success("Deleted successfully")
if (onDeleted) onDeleted()
else router.refresh()
} catch {
toast.error("Network error — please try again")
} finally {
setLoading(false)
setOpen(false)
}
}
return (
<>
setOpen(false)}
/>
>
)
}