unit(D8): optimistic grid editor + toast (assign/clear reverts on failure)

This commit is contained in:
Tiel Coder
2026-11-18 18:44:22 +00:00
parent 71b993e663
commit 137a135a5e
5 changed files with 242 additions and 6 deletions
@@ -0,0 +1,98 @@
"use client";
import { useRef, useState } from "react";
import { ScheduleGrid, type VolunteerOption } from "@/components/app/schedule-grid";
import { Toast } from "@/components/app/toast";
import type { GridAssignment, GridWeek } from "@/db/grid";
/** Deep-copy `week` with `slotId`'s assignments replaced (an optimistic edit). */
function withSlotAssignments(
week: GridWeek,
slotId: string,
assignments: GridAssignment[],
): GridWeek {
return {
...week,
instances: week.instances.map((inst) => ({
...inst,
slots: inst.slots.map((slot) =>
slot.id === slotId ? { ...slot, assignments } : slot,
),
})),
};
}
/**
* Optimistic grid editor (unit D8).
*
* Wraps the read-only grid for coordinators/admins. Each assign/clear is
* applied to state at once (the cell updates immediately), then POSTed to
* /api/assignments. On a non-2xx the cell reverts to the last confirmed week
* and an error toast shows the server's message. Volunteers keep the read-only
* grid (the schedule page only mounts this for `schedule.publish`), so the
* optimistic layer is the only place this state lives.
*/
export function ScheduleGridEditor({
week,
volunteers,
}: {
week: GridWeek;
volunteers: VolunteerOption[];
}) {
const [display, setDisplay] = useState<GridWeek>(week);
const committed = useRef<GridWeek>(week);
const [toast, setToast] = useState<{ message: string; kind: "error" | "success" } | null>(null);
const post = async (payload: {
action: "assign" | "clear";
slotId: string;
userId?: string;
}) => {
const res = await fetch("/api/assignments", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(payload),
});
let error: string | undefined;
try {
const data = (await res.json()) as { error?: string };
error = data.error;
} catch {
// non-JSON body — fall through to the default message
}
if (!res.ok) {
setDisplay(committed.current);
setToast({ message: error ?? "Could not save. Please try again.", kind: "error" });
} else {
committed.current = display;
setToast({ message: payload.action === "clear" ? "Assignment cleared" : "Assignment proposed", kind: "success" });
}
};
const onAssign = async (slotId: string, userId: string) => {
const volunteer = volunteers.find((v) => v.id === userId);
if (!volunteer) return;
const optimistic = withSlotAssignments(display, slotId, [
{ userId, displayName: volunteer.displayName, status: "proposed" },
]);
setDisplay(optimistic);
await post({ action: "assign", slotId, userId });
};
const onClear = async (slotId: string) => {
setDisplay(withSlotAssignments(display, slotId, []));
await post({ action: "clear", slotId });
};
return (
<>
<ScheduleGrid
week={display}
volunteers={volunteers}
onAssign={onAssign}
onClear={onClear}
/>
<Toast message={toast?.message ?? null} kind={toast?.kind ?? null} />
</>
);
}