diff --git a/src/app/assignments/page.tsx b/src/app/assignments/page.tsx index e685c19..fe08ece 100644 --- a/src/app/assignments/page.tsx +++ b/src/app/assignments/page.tsx @@ -88,6 +88,14 @@ export default function AssignmentsPage() { const [returnErrors, setReturnErrors] = useState>({}); const [returningId, setReturningId] = useState(null); + // Per-row transfer mini-form state, keyed by assignment id — same shape as + // maintenance-records/page.tsx's close mini-form state (separate maps + // rather than one nested-object map, so each field stays a plain string + // setter). + const [transferEmployeeId, setTransferEmployeeId] = useState>({}); + const [transferErrors, setTransferErrors] = useState>({}); + const [transferringId, setTransferringId] = useState(null); + async function handleSubmit(event: FormEvent) { event.preventDefault(); setSubmitError(null); @@ -183,6 +191,61 @@ export default function AssignmentsPage() { } } + async function handleTransfer(id: string) { + setTransferringId(id); + setTransferErrors((previous) => { + const next = { ...previous }; + delete next[id]; + return next; + }); + + const employeeId = transferEmployeeId[id] ?? ""; + + try { + // PATCH /api/assignments/[id]'s body is `{employeeId}` for a transfer + // — see src/app/api/assignments/[id]/route.ts's dispatch (a body + // carrying employeeId is a transfer, everything else is a return). + // employeeId is sent even if unselected (empty string), which the + // server rejects with a clean 400 message surfaced below, same as any + // other validation failure. + const response = await fetch(`/api/assignments/${id}`, { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ employeeId }), + }); + + let body: unknown; + try { + body = await response.json(); + } catch { + body = null; + } + + if (!response.ok) { + const message = + body !== null && + typeof body === "object" && + "error" in body && + typeof (body as { error: unknown }).error === "string" + ? (body as { error: string }).error + : "Request failed"; + setTransferErrors((previous) => ({ ...previous, [id]: message })); + return; + } + + setTransferEmployeeId((previous) => { + const next = { ...previous }; + delete next[id]; + return next; + }); + refetch(); + } catch { + setTransferErrors((previous) => ({ ...previous, [id]: "Request failed" })); + } finally { + setTransferringId(null); + } + } + return (

Assignments

@@ -220,21 +283,63 @@ export default function AssignmentsPage() { {assignment.created_at} {assignment.returned_at === null ? ( -
- - {returnErrors[assignment.id] ? ( -

- {returnErrors[assignment.id]} -

- ) : null} +
+
+ + {returnErrors[assignment.id] ? ( +

+ {returnErrors[assignment.id]} +

+ ) : null} +
+ +
+ + + {transferErrors[assignment.id] ? ( +

+ {transferErrors[assignment.id]} +

+ ) : null} +
) : ( "-"