"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" import type { Property } from "@/types" const PROPERTY_TYPES = [ { value: "residential", label: "Residential" }, { value: "commercial", label: "Commercial" }, { value: "mixed", label: "Mixed Use" }, ] interface PropertyFormProps { property?: Property } export function PropertyForm({ property }: PropertyFormProps) { const router = useRouter() const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [isDirty, setIsDirty] = useState(false) useWarnUnsaved(isDirty) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setLoading(true) setError("") const formData = new FormData(e.currentTarget) const body = { name: formData.get("name"), address_line1: formData.get("address_line1"), address_line2: formData.get("address_line2") || undefined, city: formData.get("city"), state: formData.get("state") || undefined, postal_code: formData.get("postal_code") || undefined, country: formData.get("country") || "US", property_type: formData.get("property_type"), total_units: Number(formData.get("total_units")) || 1, notes: formData.get("notes") || undefined, } const res = await fetch( property ? `/api/properties/${property.id}` : "/api/properties", { method: property ? "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(property ? "Property updated" : "Property added") router.push(`/properties/${data.id}`) router.refresh() } 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" return (
setIsDirty(true)} className="space-y-5 rounded-xl border border-white/[0.06] bg-[#16161f] p-6"> {error && (
{error}
)}