diff --git a/src/app/dashboard/customers/[id]/edit-customer-form.tsx b/src/app/dashboard/customers/[id]/edit-customer-form.tsx new file mode 100644 index 0000000..2cf6f26 --- /dev/null +++ b/src/app/dashboard/customers/[id]/edit-customer-form.tsx @@ -0,0 +1,211 @@ +'use client' + +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import * as z from "zod" +import { Button } from "@/components/ui/button" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { Textarea } from "@/components/ui/textarea" +import { updateCustomer, deleteCustomer } from "./actions" +import { useRouter } from "next/navigation" +import { useState } from "react" +import { toast } from "sonner" +import { Trash2 } from "lucide-react" + +const formSchema = z.object({ + full_name: z.string().min(2, "İsim en az 2 karakter olmalıdır."), + phone: z.string().optional(), + email: z.string().email("Geçerli bir e-posta adresi giriniz.").optional().or(z.literal("")), + city: z.string().optional(), + district: z.string().optional(), + address: z.string().optional(), + notes: z.string().optional(), +}) + +interface EditCustomerFormProps { + customer: { + id: string + full_name: string + phone?: string | null + email?: string | null + city?: string | null + district?: string | null + address?: string | null + notes?: string | null + } +} + +export function EditCustomerForm({ customer }: EditCustomerFormProps) { + const router = useRouter() + const [loading, setLoading] = useState(false) + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + full_name: customer.full_name, + phone: customer.phone || "", + email: customer.email || "", + city: customer.city || "", + district: customer.district || "", + address: customer.address || "", + notes: customer.notes || "", + }, + }) + + async function onSubmit(values: z.infer) { + setLoading(true) + try { + await updateCustomer(customer.id, values) + toast.success("Müşteri başarıyla güncellendi") + router.push('/dashboard/customers') + router.refresh() + } catch (error) { + toast.error("Bir hata oluştu") + } finally { + setLoading(false) + } + } + + async function handleDelete() { + if (!confirm("Bu müşteriyi silmek istediğinize emin misiniz?")) return + + setLoading(true) + try { + await deleteCustomer(customer.id) + toast.success("Müşteri silindi") + router.push('/dashboard/customers') + router.refresh() + } catch (error) { + toast.error("Silme işlemi başarısız") + setLoading(false) + } + } + + return ( +
+ +
+ ( + + İsim Soyisim + + + + + + )} + /> + ( + + Telefon + + + + + + )} + /> +
+ + ( + + E-posta + + + + + + )} + /> + +
+ ( + + İl + + + + + + )} + /> + ( + + İlçe + + + + + + )} + /> +
+ + ( + + Adres + +