"use client"; import { useActionState, useEffect } from "react"; import { Loader2, Save } 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; import { Switch } from "@/components/ui/switch"; import { Textarea } from "@/components/ui/textarea"; import { createServiceAction, updateServiceAction, } from "@/lib/appwrite/service-actions"; import { initialServiceState } from "@/lib/appwrite/service-types"; import type { CustomerOption, ServiceRow } from "./types"; type Props = { open: boolean; onOpenChange: (v: boolean) => void; service?: ServiceRow | null; customers: CustomerOption[]; defaultCustomerId?: string; }; export function ServiceFormSheet({ open, onOpenChange, service, customers, defaultCustomerId, }: Props) { const isEdit = Boolean(service); const action = isEdit ? updateServiceAction : createServiceAction; const [state, formAction, isPending] = useActionState(action, initialServiceState); useEffect(() => { if (state.ok) { toast.success(isEdit ? "Hizmet güncellendi." : "Hizmet eklendi."); onOpenChange(false); } else if (state.error) { toast.error(state.error); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [state]); return ( {isEdit ? "Hizmeti düzenle" : "Yeni hizmet"} {customers.length === 0 ? "Hizmet eklemek için önce en az bir müşteri tanımlamalısınız." : "Müşteriye sunduğunuz hizmeti tanımlayın."}
{isEdit && service && }
{state.fieldErrors?.customerId && (

{state.fieldErrors.customerId}

)}
{state.fieldErrors?.name && (

{state.fieldErrors.name}

)}