"use client" import { useState } from "react" import { useRouter } from "next/navigation" import Link from "next/link" import { toast } from "sonner" import { Pencil, Ban, Trash2 } from "lucide-react" import { ConfirmModal } from "@/components/ui/confirm-modal" type Action = "terminate" | "delete" | null export function LeaseActions({ lease }: { lease: any }) { const router = useRouter() const [pending, setPending] = useState(null) const [loading, setLoading] = useState(false) async function handleTerminate() { setLoading(true) try { const res = await fetch(`/api/leases/${lease.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: "terminated" }), }) const data = await res.json().catch(() => ({})) if (!res.ok) { toast.error(typeof data?.error === "string" ? data.error : "Could not terminate lease") return } toast.success("Lease terminated") setPending(null) router.refresh() } catch { toast.error("Something went wrong") } finally { setLoading(false) } } async function handleDelete() { setLoading(true) try { const res = await fetch(`/api/leases/${lease.id}`, { method: "DELETE" }) const data = await res.json().catch(() => ({})) if (!res.ok) { toast.error(typeof data?.error === "string" ? data.error : "Could not delete lease") return } toast.success("Lease deleted") setPending(null) router.push("/leases") router.refresh() } catch { toast.error("Something went wrong") } finally { setLoading(false) } } return ( <>
Edit {lease.status !== "terminated" && ( )}
!loading && setPending(null)} /> !loading && setPending(null)} /> ) }