"use client"; import { useActionState, useEffect } from "react"; import { Loader2 } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; import { createPropertyAction, updatePropertyAction } from "@/lib/appwrite/property-actions"; import type { Property } from "@/lib/appwrite/schema"; type ActionState = { ok: boolean; error?: string; fieldErrors?: Record }; const INITIAL: ActionState = { ok: false }; interface PropertyFormSheetProps { open: boolean; onOpenChange: (v: boolean) => void; property?: Property | null; onSuccess?: () => void; } export function PropertyFormSheet({ open, onOpenChange, property, onSuccess }: PropertyFormSheetProps) { const action = property ? updatePropertyAction.bind(null, property.$id) : createPropertyAction; const [state, formAction, isPending] = useActionState(action, INITIAL); useEffect(() => { if (state.ok) { toast.success(property ? "İlan güncellendi." : "İlan oluşturuldu."); onSuccess?.(); onOpenChange(false); } else if (state.error) { toast.error(state.error); } }, [state]); const fe = state.fieldErrors ?? {}; return ( {property ? "İlanı Düzenle" : "Yeni İlan"}
{fe.title &&

{fe.title[0]}

}
{fe.price &&

{fe.price[0]}

}
{fe.city &&

{fe.city[0]}

}