"use client" import { useState } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { toast } from "sonner" import { useRouter } from "next/navigation" import { createCategory, updateCategory, deleteCategory } from "@/app/(dashboard)/dashboard/categories/actions" import { Trash } from "lucide-react" import { AlertModal } from "@/components/modals/alert-modal" const formSchema = z.object({ name: z.string().min(2, "Kategori adı en az 2 karakter olmalıdır."), description: z.string().optional(), image_url: z.string().optional(), }) type CategoryFormValues = z.infer interface CategoryFormProps { initialData?: { id: string name: string description?: string image_url?: string } | null } export function CategoryForm({ initialData }: CategoryFormProps) { const router = useRouter() const [open, setOpen] = useState(false) const [loading, setLoading] = useState(false) const title = initialData ? "Kategoriyi Düzenle" : "Yeni Kategori" const description = initialData ? "Kategori detaylarını düzenleyin." : "Yeni bir kategori ekleyin." const toastMessage = initialData ? "Kategori güncellendi." : "Kategori oluşturuldu." const action = initialData ? "Kaydet" : "Oluştur" const form = useForm({ resolver: zodResolver(formSchema), defaultValues: initialData || { name: "", description: "", image_url: "", }, }) const onSubmit = async (data: CategoryFormValues) => { setLoading(true) try { if (initialData) { const result = await updateCategory(initialData.id, data) if ((result as any).error) { toast.error((result as any).error) } else { toast.success(toastMessage) router.push(`/dashboard/categories`) router.refresh() } } else { const result = await createCategory(data) if ((result as any).error) { toast.error((result as any).error) } else { toast.success(toastMessage) router.push(`/dashboard/categories`) router.refresh() } } } catch (error) { toast.error("Bir hata oluştu.") } finally { setLoading(false) } } const onDelete = async () => { setLoading(true) try { const result = await deleteCategory(initialData!.id) if ((result as any).error) { toast.error((result as any).error) } else { toast.success("Kategori silindi.") router.push(`/dashboard/categories`) router.refresh() } } catch (error) { toast.error("Silme işlemi başarısız.") } finally { setLoading(false) setOpen(false) } } return ( <> setOpen(false)} onConfirm={onDelete} loading={loading} />

{title}

{description}

{initialData && ( )}
( Başlık )} /> ( Görsel URL (Opsiyonel) )} />
( Açıklama