'use client' import { useState } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Customer, CustomerInsert } from "@/types/customer" import { addCustomer, updateCustomer } from "@/lib/customers/actions" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Plus, Loader2 } from "lucide-react" import { toast } from "sonner" const formSchema = z.object({ full_name: z.string().min(2, "Ad soyad en az 2 karakter olmalıdır"), email: z.string().email("Geçersiz e-posta adresi").or(z.literal("")).optional(), phone: z.string().optional(), address: z.string().optional(), notes: z.string().optional(), }) interface CustomerFormProps { customer?: Customer trigger?: React.ReactNode } export function CustomerForm({ customer, trigger }: CustomerFormProps) { const [open, setOpen] = useState(false) const [loading, setLoading] = useState(false) const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { full_name: customer?.full_name || "", email: customer?.email || "", phone: customer?.phone || "", address: customer?.address || "", notes: customer?.notes || "", }, }) async function onSubmit(values: z.infer) { setLoading(true) try { const customerData = { ...values, email: values.email || null, phone: values.phone || null, address: values.address || null, notes: values.notes || null, } as CustomerInsert let result if (customer) { result = await updateCustomer(customer.id, customerData) } else { result = await addCustomer(customerData) } if (result.success) { toast.success(customer ? "Müşteri güncellendi" : "Müşteri eklendi") setOpen(false) form.reset() } else { toast.error(result.error) } } catch { toast.error("Bir hata oluştu") } finally { setLoading(false) } } return ( {trigger || ( )} {customer ? "Müşteriyi Düzenle" : "Yeni Müşteri Ekle"} Müşteri bilgilerini aşağıdan yönetebilirsiniz.
( Ad Soyad )} /> ( E-Posta )} /> ( Telefon )} /> ( Adres