Spaces:
Runtime error
Runtime error
File size: 6,533 Bytes
ad79323 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | "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 extends keyof typeof initialState>(
key: Key,
value: (typeof initialState)[Key],
) {
setFormState((current) => ({
...current,
[key]: value,
}));
}
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
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 (
<Drawer open={open} onOpenChange={setOpen}>
<DrawerTrigger asChild>
<Button>
<span>قضية جديدة</span>
<Icon icon={Briefcase01Icon} size={18} />
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<div className="mb-2 flex h-11 w-11 items-center justify-center rounded-2xl bg-[var(--accent-soft)] text-[var(--accent)]">
<Icon icon={Add01Icon} size={20} />
</div>
<DrawerTitle>قضية جديدة</DrawerTitle>
<DrawerDescription>
اربط قضية جديدة بالعميل الحالي وسجل بياناتها الأساسية من البداية.
</DrawerDescription>
</DrawerHeader>
<DrawerBody>
<form className="space-y-4" id="create-case-form" onSubmit={handleSubmit}>
<div className="space-y-2">
<Label htmlFor="case-name">اسم القضية</Label>
<Input
id="case-name"
placeholder="مثال: دعوى نفقة"
value={formState.caseName}
onChange={(event) => updateField("caseName", event.target.value)}
required
/>
</div>
<div className="space-y-2">
<Label>نوع القضية</Label>
<Select
value={formState.type}
onValueChange={(value) => updateField("type", value as CaseTypeValue)}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="CIVIL">مدنيه</SelectItem>
<SelectItem value="CRIMINAL">جنائيه</SelectItem>
<SelectItem value="PERSONAL_STATUS">أحوال</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="case-subType">النوع الفرعي</Label>
<Input
id="case-subType"
placeholder="اختياري"
value={formState.subType}
onChange={(event) => updateField("subType", event.target.value)}
/>
</div>
<div className="space-y-2">
<Label htmlFor="case-opponent">اسم الخصم بالكامل</Label>
<Input
id="case-opponent"
placeholder="اكتب اسم الخصم"
value={formState.opponentFullName}
onChange={(event) =>
updateField("opponentFullName", event.target.value)
}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="case-description">وصف مختصر</Label>
<Textarea
id="case-description"
placeholder="اختياري"
value={formState.description}
onChange={(event) => updateField("description", event.target.value)}
/>
</div>
{status ? (
<p
className={
status.type === "success"
? "rounded-2xl bg-[var(--accent-soft)] px-4 py-3 text-sm font-medium text-[var(--accent)]"
: "rounded-2xl bg-[color:rgba(185,28,28,0.08)] px-4 py-3 text-sm font-medium text-[var(--destructive)]"
}
>
{status.message}
</p>
) : null}
</form>
</DrawerBody>
<DrawerFooter>
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
إلغاء
</Button>
<Button form="create-case-form" disabled={isPending} type="submit">
<span>{isPending ? "جارٍ الإنشاء..." : "إنشاء القضية"}</span>
<Icon icon={Briefcase01Icon} size={18} />
</Button>
</DrawerFooter>
</DrawerContent>
</Drawer>
);
}
|