"use client"; import { useState, useTransition } from "react"; import { usePathname, useRouter } from "next/navigation"; import { Add01Icon, Briefcase01Icon } from "@hugeicons/core-free-icons"; import type { CaseTypeValue } from "@/lib/constants"; import { Icon } from "@/components/shared/icon"; import { Button } from "@/components/ui/button"; import { Drawer, DrawerBody, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; type CaseCreateFormProps = { customerId: string; }; const initialState = { caseName: "", type: "CIVIL" as CaseTypeValue, subType: "", opponentFullName: "", description: "", }; export function CaseCreateForm({ customerId }: CaseCreateFormProps) { const router = useRouter(); const pathname = usePathname(); const [open, setOpen] = useState(false); const [isPending, startTransition] = useTransition(); const [status, setStatus] = useState<{ type: "success" | "error"; message: string; } | null>(null); const [formState, setFormState] = useState(initialState); function updateField( key: Key, value: (typeof initialState)[Key], ) { setFormState((current) => ({ ...current, [key]: value, })); } function handleSubmit(event: React.FormEvent) { event.preventDefault(); setStatus(null); startTransition(async () => { const response = await fetch(`/api/customers/${customerId}/cases`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(formState), }); const payload = (await response.json().catch(() => null)) as | { message?: string } | null; if (!response.ok) { setStatus({ type: "error", message: payload?.message || "تعذر إنشاء القضية.", }); return; } setFormState(initialState); setStatus({ type: "success", message: "تم إنشاء القضية بنجاح.", }); setOpen(false); router.replace(`${pathname}?tab=cases`); router.refresh(); }); } return (
قضية جديدة اربط قضية جديدة بالعميل الحالي وسجل بياناتها الأساسية من البداية.
updateField("caseName", event.target.value)} required />
updateField("subType", event.target.value)} />
updateField("opponentFullName", event.target.value) } required />