"use client"; import { useActionState, useEffect } from "react"; import { CircleNotch } from '@/lib/icons'; import { toast } from "sonner"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { ResponsiveSheet, FormWizard } from "@/components/ui/responsive-sheet"; import { createCustomerAction, updateCustomerAction } from "@/lib/appwrite/customer-actions"; import type { Customer } from "@/lib/appwrite/schema"; import { CUSTOMER_STAGE_LABELS, CUSTOMER_SOURCE_LABELS } from "@/lib/appwrite/schema"; type ActionState = { ok: boolean; error?: string; fieldErrors?: Record }; const INITIAL: ActionState = { ok: false }; interface CustomerFormSheetProps { open: boolean; onOpenChange: (v: boolean) => void; customer?: Customer | null; onSuccess?: () => void; } export function CustomerFormSheet({ open, onOpenChange, customer, onSuccess }: CustomerFormSheetProps) { const action = customer ? updateCustomerAction.bind(null, customer.$id) : createCustomerAction; const [state, formAction, isPending] = useActionState(action, INITIAL); useEffect(() => { if (state.ok) { toast.success(customer ? "Müşteri güncellendi." : "Müşteri oluşturuldu."); onSuccess?.(); onOpenChange(false); } else if (state.error) { toast.error(state.error); } }, [state]); const fe = state.fieldErrors ?? {}; const steps = [ { label: "Kimlik", content: ( <>
{fe.name &&

{fe.name[0]}

}
{fe.type &&

{fe.type[0]}

}
), }, { label: "İletişim", content: ( <>
{fe.email &&

{fe.email[0]}

}
), }, { label: "Not", content: (