d0a696167f
- src/db/grid.ts: key role map by ID (was slug, causing all cells to show '—') - src/components/app/schedule-grid-editor.tsx: revert only failed cell, not entire grid - src/db/audit.ts: fix import path ./db → @/db - src/db/seed.ts: add resetAndSeed(), assign Audio only (leave Livestream/Slides open) - src/auth/config.ts: carry id/email on JWT for audit actor - e2e/dev.ts: shared signInAs + resetDb helpers - e2e/schedule-grid.spec.ts: use shared helpers, reset DB beforeAll - e2e/assign-mutations.spec.ts: E2E for assign/clear with optimistic update + rollback - src/app/api/dev/reset/route.ts: admin-only DB reset endpoint - playwright.config.ts: workers: 1 (serial, suites share one Postgres)
105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
"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) {
|
|
// Revert only the cell that failed, leaving other optimistic changes intact
|
|
const committedSlot = committed.current.instances
|
|
.flatMap((i) => i.slots)
|
|
.find((s) => s.id === payload.slotId);
|
|
setDisplay((prev) =>
|
|
withSlotAssignments(prev, payload.slotId, committedSlot?.assignments ?? []),
|
|
);
|
|
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} />
|
|
</>
|
|
);
|
|
}
|