"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { toast } from "sonner" import { useWarnUnsaved } from "@/lib/hooks/use-warn-unsaved" import { Select } from "@/components/ui/select" const CATEGORIES = [ { value: "repairs", label: "Repairs" }, { value: "utilities", label: "Utilities" }, { value: "insurance", label: "Insurance" }, { value: "mortgage", label: "Mortgage" }, { value: "taxes", label: "Taxes" }, { value: "management", label: "Management" }, { value: "supplies", label: "Supplies" }, { value: "other", label: "Other" }, ] const RECURRENCE = [ { value: "monthly", label: "Monthly" }, { value: "quarterly", label: "Quarterly" }, { value: "yearly", label: "Yearly" }, ] export function ExpenseForm({ properties, expense }: { properties: any[]; expense?: any }) { const router = useRouter() const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [selectedPropertyId, setSelectedPropertyId] = useState(expense?.property_id ?? "") const [isRecurring, setIsRecurring] = useState(expense?.is_recurring ?? false) const [isDirty, setIsDirty] = useState(false) useWarnUnsaved(isDirty) const propertyOptions = properties.map((p: any) => ({ value: p.id, label: p.name })) const units = properties.find((p: any) => p.id === selectedPropertyId)?.units ?? [] const unitOptions = [ { value: "", label: "Whole property" }, ...units.map((u: any) => ({ value: u.id, label: `Unit ${u.unit_number}` })), ] const cls = "w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-sm text-white placeholder-white/30 outline-none ring-indigo-500 transition focus:border-indigo-500/50 focus:ring-1" const lbl = "mb-1.5 block text-sm font-medium text-white/70" async function handleSubmit(e: React.FormEvent) { e.preventDefault() setLoading(true) setError("") const fd = new FormData(e.currentTarget) const body = { property_id: fd.get("property_id"), unit_id: fd.get("unit_id") || undefined, category: fd.get("category"), description: fd.get("description"), amount: Number(fd.get("amount")), expense_date: fd.get("expense_date"), vendor: fd.get("vendor") || undefined, is_recurring: fd.get("is_recurring") === "on", recurrence: fd.get("recurrence") || undefined, notes: fd.get("notes") || undefined, } const res = await fetch(expense ? `/api/expenses/${expense.id}` : "/api/expenses", { method: expense ? "PATCH" : "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }) const data = await res.json() setLoading(false) if (!res.ok) { setError(typeof data.error === "string" ? data.error : "Something went wrong") return } setIsDirty(false) toast.success(expense ? "Expense updated" : "Expense added") router.push("/expenses") router.refresh() } const today = new Date().toISOString().slice(0, 10) return (
setIsDirty(true)} className="space-y-5 rounded-xl border border-white/[0.06] bg-[#16161f] p-6"> {error &&
{error}
}
)}
setIsRecurring(e.target.checked)} className="h-4 w-4 rounded accent-indigo-600" />
{isRecurring && (