"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 { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; import { createInvestorAction, updateInvestorAction } from "@/lib/appwrite/investor-actions"; import type { Investor } from "@/lib/appwrite/schema"; type ActionState = { ok: boolean; error?: string; fieldErrors?: Record }; const INITIAL: ActionState = { ok: false }; interface InvestorFormSheetProps { open: boolean; onOpenChange: (v: boolean) => void; investor?: Investor | null; onSuccess?: () => void; } export function InvestorFormSheet({ open, onOpenChange, investor, onSuccess }: InvestorFormSheetProps) { const action = investor ? updateInvestorAction.bind(null, investor.$id) : createInvestorAction; const [state, formAction, isPending] = useActionState(action, INITIAL); useEffect(() => { if (state.ok) { toast.success(investor ? "Yatırımcı güncellendi." : "Yatırımcı oluşturuldu."); onSuccess?.(); onOpenChange(false); } else if (state.error) { toast.error(state.error); } }, [state]); const fe = state.fieldErrors ?? {}; return ( {investor ? "Yatırımcıyı Düzenle" : "Yeni Yatırımcı"}
{fe.name &&

{fe.name[0]}

}
{fe.email &&

{fe.email[0]}

}