"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { BookEditIcon } from "@hugeicons/core-free-icons"; import type { CaseTypeValue } from "@/lib/constants"; import { Icon } from "@/components/shared/icon"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; 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 CaseEditDialogProps = { customerId: string; caseId: string; defaultValues: { caseName: string; type: CaseTypeValue; subType?: string | null; opponentFullName: string; description?: string | null; }; }; export function CaseEditDialog({ customerId, caseId, defaultValues, }: CaseEditDialogProps) { const router = useRouter(); const [open, setOpen] = useState(false); const [isPending, startTransition] = useTransition(); const [error, setError] = useState(null); const [formState, setFormState] = useState({ caseName: defaultValues.caseName, type: defaultValues.type, subType: defaultValues.subType ?? "", opponentFullName: defaultValues.opponentFullName, description: defaultValues.description ?? "", }); function updateField( key: Key, value: (typeof formState)[Key], ) { setFormState((current) => ({ ...current, [key]: value, })); } function handleSubmit(event: React.FormEvent) { event.preventDefault(); setError(null); startTransition(async () => { const response = await fetch( `/api/customers/${customerId}/cases/${caseId}`, { method: "PATCH", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ action: "update", ...formState, }), }, ); const payload = (await response.json().catch(() => null)) as | { message?: string } | null; if (!response.ok) { setError(payload?.message || "تعذر تعديل بيانات القضية."); return; } setOpen(false); router.refresh(); }); } return ( تعديل بيانات القضية حدّث نوع القضية أو بيانات الخصم أو الوصف المختصر وقت ما تحتاج.
updateField("caseName", event.target.value)} required />
updateField("subType", event.target.value)} />
updateField("opponentFullName", event.target.value) } required />