phase-5: contracts, warranties, insurance, demo equipment, and department visits
This commit is contained in:
@@ -0,0 +1,703 @@
|
||||
import { useState, useEffect, useMemo } from "react";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import type { ColumnDef } from "@tanstack/react-table";
|
||||
import {
|
||||
Plus,
|
||||
Paperclip,
|
||||
Pencil,
|
||||
Trash2,
|
||||
CornerDownLeft,
|
||||
Search,
|
||||
AlertCircle,
|
||||
} from "lucide-react";
|
||||
import { demoEquipmentApi, type DemoEquipmentItem } from "@/api/phase5";
|
||||
import { departmentsApi, locationsApi, vendorsApi } from "@/api/masters";
|
||||
import { ApiError } from "@/api/client";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
import { can } from "@/lib/permissions";
|
||||
import { formatDate } from "@/lib/format";
|
||||
import { DataTable } from "@/components/DataTable";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Modal } from "@/components/ui/dialog";
|
||||
import { DemoStatusBadge } from "@/components/StatusBadge";
|
||||
import { Attachments } from "@/components/Attachments";
|
||||
|
||||
type ViewTab = "active" | "returned" | "all";
|
||||
|
||||
const inputCls =
|
||||
"w-full px-3 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500";
|
||||
const labelCls = "block text-sm font-medium text-gray-700 mb-1";
|
||||
|
||||
export function DemoEquipmentPage() {
|
||||
const qc = useQueryClient();
|
||||
const role = useAuthStore((s) => s.user?.role);
|
||||
const isDeptUser = role === "department_user";
|
||||
const canManage = can(role, "manage_demo_equipment");
|
||||
|
||||
const [viewTab, setViewTab] = useState<ViewTab>("active");
|
||||
const [q, setQ] = useState("");
|
||||
const [supplierId, setSupplierId] = useState("");
|
||||
const [departmentId, setDepartmentId] = useState("");
|
||||
const [dateFrom, setDateFrom] = useState("");
|
||||
const [dateTo, setDateTo] = useState("");
|
||||
|
||||
const [formOpen, setFormOpen] = useState(false);
|
||||
const [editing, setEditing] = useState<DemoEquipmentItem | null>(null);
|
||||
const [returnTarget, setReturnTarget] = useState<DemoEquipmentItem | null>(null);
|
||||
const [attachTarget, setAttachTarget] = useState<DemoEquipmentItem | null>(null);
|
||||
|
||||
const deptsQuery = useQuery({
|
||||
queryKey: ["departments", "active-only"],
|
||||
queryFn: () => departmentsApi.list(false),
|
||||
enabled: !isDeptUser,
|
||||
});
|
||||
|
||||
const locationsQuery = useQuery({
|
||||
queryKey: ["locations", "active-only"],
|
||||
queryFn: () => locationsApi.list(false),
|
||||
});
|
||||
|
||||
const vendorsQuery = useQuery({
|
||||
queryKey: ["vendors", "active-only"],
|
||||
queryFn: () => vendorsApi.list(false),
|
||||
});
|
||||
|
||||
const demoQuery = useQuery({
|
||||
queryKey: [
|
||||
"demo-equipment",
|
||||
viewTab,
|
||||
q,
|
||||
supplierId,
|
||||
departmentId,
|
||||
dateFrom,
|
||||
dateTo,
|
||||
],
|
||||
queryFn: () =>
|
||||
demoEquipmentApi.list({
|
||||
q: q.trim() || undefined,
|
||||
supplier_id: supplierId ? Number(supplierId) : undefined,
|
||||
department_id: !isDeptUser && departmentId ? Number(departmentId) : undefined,
|
||||
status: viewTab === "returned" ? "returned" : undefined,
|
||||
date_from: dateFrom || undefined,
|
||||
date_to: dateTo || undefined,
|
||||
page_size: 200,
|
||||
}),
|
||||
});
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: (id: number) => demoEquipmentApi.remove(id),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ["demo-equipment"] }),
|
||||
});
|
||||
|
||||
const items = useMemo(() => {
|
||||
const raw = demoQuery.data?.items ?? [];
|
||||
if (viewTab === "active") {
|
||||
return raw.filter((item) => item.status !== "returned");
|
||||
}
|
||||
if (viewTab === "returned") {
|
||||
return raw.filter((item) => item.status === "returned");
|
||||
}
|
||||
return raw;
|
||||
}, [demoQuery.data?.items, viewTab]);
|
||||
|
||||
const departments = deptsQuery.data?.items ?? [];
|
||||
const locations = locationsQuery.data?.items ?? [];
|
||||
const allVendors = vendorsQuery.data?.items ?? [];
|
||||
const suppliers = allVendors.filter((v) => v.vendor_type === "supplier");
|
||||
const supplierOptions = suppliers.length ? suppliers : allVendors;
|
||||
|
||||
const columns: ColumnDef<DemoEquipmentItem, unknown>[] = [
|
||||
{
|
||||
id: "equipment",
|
||||
accessorFn: (r) => `${r.name} ${r.equipment_type ?? ""}`,
|
||||
header: "Equipment",
|
||||
cell: ({ row }) => (
|
||||
<div>
|
||||
<div className="font-medium text-gray-900">{row.original.name}</div>
|
||||
{row.original.equipment_type && (
|
||||
<div className="text-xs text-gray-500">{row.original.equipment_type}</div>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: "supplier_name",
|
||||
header: "Supplier",
|
||||
cell: ({ getValue }) => (getValue() as string | null) || "—",
|
||||
},
|
||||
{
|
||||
id: "dept_loc",
|
||||
accessorFn: (r) => `${r.department_name ?? ""} ${r.location_name ?? ""}`,
|
||||
header: "Department / Location",
|
||||
cell: ({ row }) => (
|
||||
<div>
|
||||
<div className="text-gray-800">{row.original.department_name || "—"}</div>
|
||||
{row.original.location_name && (
|
||||
<div className="text-xs text-gray-500">{row.original.location_name}</div>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: "start_date",
|
||||
header: "Start Date",
|
||||
cell: ({ getValue }) => (
|
||||
<span className="whitespace-nowrap">{formatDate(getValue() as string)}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: "expected_return_date",
|
||||
header: "Expected Return",
|
||||
cell: ({ row }) => (
|
||||
<span
|
||||
className={`whitespace-nowrap ${
|
||||
row.original.status === "overdue_return" ? "font-semibold text-red-700" : ""
|
||||
}`}
|
||||
>
|
||||
{formatDate(row.original.expected_return_date)}
|
||||
</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: "actual_return_date",
|
||||
header: "Actual Return",
|
||||
cell: ({ getValue }) => (
|
||||
<span className="whitespace-nowrap">{formatDate(getValue() as string | null)}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
id: "status",
|
||||
accessorKey: "status",
|
||||
header: "Status",
|
||||
cell: ({ row }) => (
|
||||
<DemoStatusBadge
|
||||
status={row.original.status}
|
||||
daysRemaining={row.original.days_remaining}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
id: "attachments",
|
||||
header: "Docs",
|
||||
enableSorting: false,
|
||||
enableColumnFilter: false,
|
||||
cell: ({ row }) => (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => setAttachTarget(row.original)}
|
||||
title="View or upload demo equipment documents"
|
||||
>
|
||||
<Paperclip className="h-3.5 w-3.5 mr-1" />
|
||||
<span className="text-xs">{row.original.attachment_count}</span>
|
||||
</Button>
|
||||
),
|
||||
},
|
||||
...(canManage
|
||||
? [
|
||||
{
|
||||
id: "actions",
|
||||
header: "Actions",
|
||||
enableSorting: false,
|
||||
enableColumnFilter: false,
|
||||
cell: ({ row }: { row: { original: DemoEquipmentItem } }) => (
|
||||
<div className="flex items-center gap-1">
|
||||
{row.original.status !== "returned" && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => setReturnTarget(row.original)}
|
||||
>
|
||||
<CornerDownLeft className="h-3.5 w-3.5 mr-1" />
|
||||
Return
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
setEditing(row.original);
|
||||
setFormOpen(true);
|
||||
}}
|
||||
aria-label={`Edit demo equipment ${row.original.name}`}
|
||||
>
|
||||
<Pencil className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="text-red-600 hover:text-red-700"
|
||||
onClick={() => {
|
||||
if (window.confirm(`Delete demo entry "${row.original.name}"?`)) {
|
||||
deleteMutation.mutate(row.original.id);
|
||||
}
|
||||
}}
|
||||
aria-label={`Delete demo equipment ${row.original.name}`}
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
),
|
||||
} satisfies ColumnDef<DemoEquipmentItem, unknown>,
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-gray-900">Demo Equipment Register</h1>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Monitor supplier trial equipment, expected return dates, and overdue alerts
|
||||
</p>
|
||||
</div>
|
||||
{canManage && (
|
||||
<Button
|
||||
onClick={() => {
|
||||
setEditing(null);
|
||||
setFormOpen(true);
|
||||
}}
|
||||
>
|
||||
<Plus className="h-4 w-4 mr-1.5" />
|
||||
Log Demo Equipment
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* View Tabs */}
|
||||
<div className="flex gap-1 border-b border-gray-200">
|
||||
{(
|
||||
[
|
||||
{ key: "active", label: "Active & Overdue" },
|
||||
{ key: "returned", label: "Returned History" },
|
||||
{ key: "all", label: "All" },
|
||||
] as { key: ViewTab; label: string }[]
|
||||
).map((t) => (
|
||||
<button
|
||||
key={t.key}
|
||||
type="button"
|
||||
onClick={() => setViewTab(t.key)}
|
||||
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
|
||||
viewTab === t.key
|
||||
? "border-[#1e3a8a] text-[#1e3a8a]"
|
||||
: "border-transparent text-gray-500 hover:text-gray-800"
|
||||
}`}
|
||||
>
|
||||
{t.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Filters */}
|
||||
<div className="bg-white border border-gray-200 rounded-lg p-3.5 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-6 gap-2.5">
|
||||
<div className="relative lg:col-span-2">
|
||||
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
placeholder="Search equipment name, type, remarks..."
|
||||
className="w-full pl-8 pr-3 py-1.5 text-xs border border-gray-200 rounded-md"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<select
|
||||
value={supplierId}
|
||||
onChange={(e) => setSupplierId(e.target.value)}
|
||||
aria-label="Filter by Supplier"
|
||||
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md bg-white"
|
||||
>
|
||||
<option value="">All Suppliers</option>
|
||||
{supplierOptions.map((s) => (
|
||||
<option key={s.id} value={String(s.id)}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{!isDeptUser && (
|
||||
<select
|
||||
value={departmentId}
|
||||
onChange={(e) => setDepartmentId(e.target.value)}
|
||||
aria-label="Filter by Department"
|
||||
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md bg-white"
|
||||
>
|
||||
<option value="">All Departments</option>
|
||||
{departments.map((d) => (
|
||||
<option key={d.id} value={String(d.id)}>
|
||||
{d.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
|
||||
<input
|
||||
type="date"
|
||||
aria-label="Start Date From"
|
||||
value={dateFrom}
|
||||
onChange={(e) => setDateFrom(e.target.value)}
|
||||
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
aria-label="Start Date To"
|
||||
value={dateTo}
|
||||
onChange={(e) => setDateTo(e.target.value)}
|
||||
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DataTable
|
||||
data={items}
|
||||
columns={columns}
|
||||
loading={demoQuery.isLoading}
|
||||
globalFilterPlaceholder="Quick filter demo equipment..."
|
||||
exportFilename="demo-equipment"
|
||||
emptyMessage="No demo equipment entries found."
|
||||
/>
|
||||
|
||||
<DemoFormModal
|
||||
open={formOpen}
|
||||
onClose={() => setFormOpen(false)}
|
||||
editing={editing}
|
||||
suppliers={supplierOptions}
|
||||
departments={departments}
|
||||
locations={locations}
|
||||
/>
|
||||
|
||||
<ReturnDemoModal
|
||||
open={Boolean(returnTarget)}
|
||||
onClose={() => setReturnTarget(null)}
|
||||
item={returnTarget}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={Boolean(attachTarget)}
|
||||
onOpenChange={(o) => !o && setAttachTarget(null)}
|
||||
title={`Documents — ${attachTarget?.name ?? ""}`}
|
||||
className="max-w-xl"
|
||||
>
|
||||
{attachTarget && (
|
||||
<Attachments
|
||||
entityType="demo_equipment"
|
||||
entityId={attachTarget.id}
|
||||
canUpload={canManage}
|
||||
canDelete={canManage}
|
||||
/>
|
||||
)}
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DemoFormModal({
|
||||
open,
|
||||
onClose,
|
||||
editing,
|
||||
suppliers,
|
||||
departments,
|
||||
locations,
|
||||
}: {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
editing: DemoEquipmentItem | null;
|
||||
suppliers: Array<{ id: number; name: string }>;
|
||||
departments: Array<{ id: number; name: string }>;
|
||||
locations: Array<{ id: number; department_id: number; name: string }>;
|
||||
}) {
|
||||
const qc = useQueryClient();
|
||||
const [name, setName] = useState("");
|
||||
const [equipmentType, setEquipmentType] = useState("");
|
||||
const [supplierId, setSupplierId] = useState("");
|
||||
const [departmentId, setDepartmentId] = useState("");
|
||||
const [locationId, setLocationId] = useState("");
|
||||
const [startDate, setStartDate] = useState("");
|
||||
const [expectedReturnDate, setExpectedReturnDate] = useState("");
|
||||
const [remarks, setRemarks] = useState("");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setError(null);
|
||||
if (editing) {
|
||||
setName(editing.name);
|
||||
setEquipmentType(editing.equipment_type ?? "");
|
||||
setSupplierId(String(editing.supplier_id));
|
||||
setDepartmentId(String(editing.department_id));
|
||||
setLocationId(editing.location_id ? String(editing.location_id) : "");
|
||||
setStartDate(editing.start_date);
|
||||
setExpectedReturnDate(editing.expected_return_date);
|
||||
setRemarks(editing.remarks ?? "");
|
||||
} else {
|
||||
const today = new Date();
|
||||
const plus14 = new Date(today);
|
||||
plus14.setDate(plus14.getDate() + 14);
|
||||
setName("");
|
||||
setEquipmentType("");
|
||||
setSupplierId("");
|
||||
setDepartmentId("");
|
||||
setLocationId("");
|
||||
setStartDate(today.toISOString().slice(0, 10));
|
||||
setExpectedReturnDate(plus14.toISOString().slice(0, 10));
|
||||
setRemarks("");
|
||||
}
|
||||
}, [open, editing]);
|
||||
|
||||
const deptLocations = locations.filter(
|
||||
(l) => !departmentId || l.department_id === Number(departmentId)
|
||||
);
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
try {
|
||||
const payload = {
|
||||
name: name.trim(),
|
||||
equipment_type: equipmentType.trim(),
|
||||
supplier_id: Number(supplierId),
|
||||
department_id: Number(departmentId),
|
||||
location_id: locationId ? Number(locationId) : null,
|
||||
start_date: startDate,
|
||||
expected_return_date: expectedReturnDate,
|
||||
remarks: remarks.trim() || null,
|
||||
};
|
||||
if (editing) {
|
||||
await demoEquipmentApi.update(editing.id, payload);
|
||||
} else {
|
||||
await demoEquipmentApi.create(payload);
|
||||
}
|
||||
await qc.invalidateQueries({ queryKey: ["demo-equipment"] });
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(err instanceof ApiError ? err.detail : "Failed to save demo equipment.");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onOpenChange={(o) => !o && onClose()}
|
||||
title={editing ? `Edit Demo Equipment — ${editing.name}` : "Log Demo Equipment"}
|
||||
className="max-w-xl"
|
||||
>
|
||||
{error && (
|
||||
<div
|
||||
role="alert"
|
||||
className="mb-3 flex items-start gap-2 rounded-md bg-red-50 border border-red-200 px-3 py-2 text-sm text-red-700"
|
||||
>
|
||||
<AlertCircle className="h-4 w-4 shrink-0 mt-0.5" />
|
||||
<span>{error}</span>
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className={labelCls}>Equipment Name *</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="e.g. Portable Ultrasound Demo Unit"
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Equipment Type / Model *</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
value={equipmentType}
|
||||
onChange={(e) => setEquipmentType(e.target.value)}
|
||||
placeholder="e.g. Diagnostic Imaging"
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Supplier *</label>
|
||||
<select
|
||||
required
|
||||
value={supplierId}
|
||||
onChange={(e) => setSupplierId(e.target.value)}
|
||||
className={inputCls}
|
||||
>
|
||||
<option value="">Select supplier...</option>
|
||||
{suppliers.map((s) => (
|
||||
<option key={s.id} value={String(s.id)}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Department *</label>
|
||||
<select
|
||||
required
|
||||
value={departmentId}
|
||||
onChange={(e) => {
|
||||
setDepartmentId(e.target.value);
|
||||
setLocationId("");
|
||||
}}
|
||||
className={inputCls}
|
||||
>
|
||||
<option value="">Select department...</option>
|
||||
{departments.map((d) => (
|
||||
<option key={d.id} value={String(d.id)}>
|
||||
{d.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Location</label>
|
||||
<select
|
||||
value={locationId}
|
||||
onChange={(e) => setLocationId(e.target.value)}
|
||||
className={inputCls}
|
||||
>
|
||||
<option value="">Select location...</option>
|
||||
{deptLocations.map((l) => (
|
||||
<option key={l.id} value={String(l.id)}>
|
||||
{l.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div />
|
||||
<div>
|
||||
<label className={labelCls}>Start Date *</label>
|
||||
<input
|
||||
type="date"
|
||||
required
|
||||
value={startDate}
|
||||
onChange={(e) => setStartDate(e.target.value)}
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Expected Return Date *</label>
|
||||
<input
|
||||
type="date"
|
||||
required
|
||||
value={expectedReturnDate}
|
||||
onChange={(e) => setExpectedReturnDate(e.target.value)}
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Remarks</label>
|
||||
<textarea
|
||||
rows={2}
|
||||
value={remarks}
|
||||
onChange={(e) => setRemarks(e.target.value)}
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-end gap-2 pt-2">
|
||||
<Button type="button" variant="outline" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" loading={submitting}>
|
||||
{editing ? "Save Changes" : "Log Demo Equipment"}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
function ReturnDemoModal({
|
||||
open,
|
||||
onClose,
|
||||
item,
|
||||
}: {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
item: DemoEquipmentItem | null;
|
||||
}) {
|
||||
const qc = useQueryClient();
|
||||
const [actualReturnDate, setActualReturnDate] = useState("");
|
||||
const [remarks, setRemarks] = useState("");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setActualReturnDate(new Date().toISOString().slice(0, 10));
|
||||
setRemarks(item?.remarks ?? "");
|
||||
setError(null);
|
||||
}, [open, item]);
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!item) return;
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
try {
|
||||
await demoEquipmentApi.markReturned(item.id, {
|
||||
actual_return_date: actualReturnDate,
|
||||
remarks: remarks.trim() || null,
|
||||
});
|
||||
await qc.invalidateQueries({ queryKey: ["demo-equipment"] });
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(err instanceof ApiError ? err.detail : "Failed to mark returned.");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onOpenChange={(o) => !o && onClose()}
|
||||
title={`Mark Returned — ${item?.name ?? ""}`}
|
||||
>
|
||||
{error && (
|
||||
<div
|
||||
role="alert"
|
||||
className="mb-3 flex items-start gap-2 rounded-md bg-red-50 border border-red-200 px-3 py-2 text-sm text-red-700"
|
||||
>
|
||||
<AlertCircle className="h-4 w-4 shrink-0 mt-0.5" />
|
||||
<span>{error}</span>
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className={labelCls}>Actual Return Date *</label>
|
||||
<input
|
||||
type="date"
|
||||
required
|
||||
value={actualReturnDate}
|
||||
onChange={(e) => setActualReturnDate(e.target.value)}
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelCls}>Return Remarks</label>
|
||||
<textarea
|
||||
rows={2}
|
||||
value={remarks}
|
||||
onChange={(e) => setRemarks(e.target.value)}
|
||||
placeholder="Condition, handover person, gate pass reference..."
|
||||
className={inputCls}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-end gap-2 pt-2">
|
||||
<Button type="button" variant="outline" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" variant="success" loading={submitting}>
|
||||
Confirm Return
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user