diff --git a/src/app/(dashboard)/customers/components/customer-form-sheet.tsx b/src/app/(dashboard)/customers/components/customer-form-sheet.tsx new file mode 100644 index 0000000..530e5d9 --- /dev/null +++ b/src/app/(dashboard)/customers/components/customer-form-sheet.tsx @@ -0,0 +1,187 @@ +"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 { Textarea } from "@/components/ui/textarea"; +import { + createCustomerAction, + updateCustomerAction, +} from "@/lib/appwrite/customer-actions"; +import { initialCustomerState } from "@/lib/appwrite/customer-types"; +import type { CustomerRow } from "./types"; + +type Props = { + open: boolean; + onOpenChange: (v: boolean) => void; + customer?: CustomerRow | null; +}; + +export function CustomerFormSheet({ open, onOpenChange, customer }: Props) { + const isEdit = Boolean(customer); + const action = isEdit ? updateCustomerAction : createCustomerAction; + const [state, formAction, isPending] = useActionState(action, initialCustomerState); + + useEffect(() => { + if (state.ok) { + toast.success(isEdit ? "Müşteri güncellendi." : "Müşteri eklendi."); + onOpenChange(false); + } else if (state.error) { + toast.error(state.error); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [state]); + + return ( + + + + {isEdit ? "Müşteriyi düzenle" : "Yeni müşteri"} + + {isEdit + ? "Müşteri bilgilerini güncelleyin." + : "Yeni bir müşteri ekleyin. * işaretli alanlar zorunludur."} + + + +
+ {isEdit && customer && } + +
+
+ + + {state.fieldErrors?.name && ( +

{state.fieldErrors.name}

+ )} +
+ +
+
+ + + {state.fieldErrors?.email && ( +

{state.fieldErrors.email}

+ )} +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ +
+ +