🧑🏫 lift lesson roll call into its own component
Browse files at this commit0a51c44fb33d2747ea4a05f9f89b623bf8ef3a45Parent: 1f449db- refactor
- 🧑🏫 lift lesson roll call into its own component
4 changed files +290 -113
- src/components/sessions/course-sessions-panel.tsx +47 -113
- src/components/sessions/session-roll-call.test.tsx +72 -0
- src/components/sessions/session-roll-call.tsx +169 -0
- src/pages/instance-detail-page.tsx +2 -0
src/components/sessions/course-sessions-panel.tsxmodified+47 -113
| @@ -2,27 +2,24 @@ import { For, Show, Suspense, createEffect, createMemo, createSignal } from "sol | |||
| 2 | import { createResource } from "@/lib/create-resource"; | 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import type { ColumnDef } from "@tanstack/solid-table"; | 3 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 4 | import { deleteSessionById } from "@/api/sessions"; | 4 | import { deleteSessionById } from "@/api/sessions"; |
| 5 | -import { deleteSessionAttendanceByUserId } from "@/api/sessions"; | ||
| 6 | import { getSessionAttendance } from "@/api/sessions"; | 5 | import { getSessionAttendance } from "@/api/sessions"; |
| 7 | import { getInstanceSessions, postInstanceSession } from "@/api/instances"; | 6 | import { getInstanceSessions, postInstanceSession } from "@/api/instances"; |
| 8 | import { getTime } from "@/api/time"; | 7 | import { getTime } from "@/api/time"; |
| 9 | import { patchSessionById } from "@/api/sessions"; | 8 | import { patchSessionById } from "@/api/sessions"; |
| 10 | -import { postSessionAttendance } from "@/api/sessions"; | ||
| 11 | import { formatApiError } from "@/api/client"; | 9 | import { formatApiError } from "@/api/client"; |
| 12 | -import type { AttendanceStatus, CourseSession, Enrollment, SessionAttendance } from "@/api/client"; | 10 | +import type { CourseSession, Enrollment, PersonRef } from "@/api/client"; |
| 13 | -import { AttendanceStatusPicker } from "@/components/events/attendance-status-picker"; | 11 | +import { SessionRollCall } from "@/components/sessions/session-roll-call"; |
| 14 | import { Alert } from "@/components/ui/alert"; | 12 | import { Alert } from "@/components/ui/alert"; |
| 15 | import { Button } from "@/components/ui/button"; | 13 | import { Button } from "@/components/ui/button"; |
| 16 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | 14 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 17 | import { DatePicker } from "@/components/ui/date-picker"; | 15 | import { DatePicker } from "@/components/ui/date-picker"; |
| 18 | import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; | 16 | import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; |
| 19 | import { DetailField } from "@/components/ui/detail-field"; | 17 | import { DetailField } from "@/components/ui/detail-field"; |
| 20 | -import { EmptyState } from "@/components/ui/empty-state"; | ||
| 21 | import { ErrorAlert } from "@/components/ui/error-alert"; | 18 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 22 | import { IconClipboardCheck, IconEdit, IconEye, IconTrash } from "@/components/ui/icons"; | 19 | import { IconClipboardCheck, IconEdit, IconEye, IconTrash } from "@/components/ui/icons"; |
| 23 | import { Input } from "@/components/ui/input"; | 20 | import { Input } from "@/components/ui/input"; |
| 24 | import { Label } from "@/components/ui/label"; | 21 | import { Label } from "@/components/ui/label"; |
| 25 | -import { PaginationControls } from "@/components/ui/pagination-controls"; | 22 | +import { Select } from "@/components/ui/select"; |
| 26 | import { SidePanel } from "@/components/ui/side-panel"; | 23 | import { SidePanel } from "@/components/ui/side-panel"; |
| 27 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 24 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 28 | import { createFlash } from "@/lib/flash"; | 25 | import { createFlash } from "@/lib/flash"; |
| @@ -58,13 +55,13 @@ function msToTimeInput(ms: number): string { | |||
| 58 | return `${pad(d.getHours())}:${pad(d.getMinutes())}`; | 55 | return `${pad(d.getHours())}:${pad(d.getMinutes())}`; |
| 59 | } | 56 | } |
| 60 | 57 | ||
| 61 | -const ROLL_CALL_PAGE_SIZE = 8; | ||
| 62 | - | ||
| 63 | export function CourseSessionsPanel(props: { | 58 | export function CourseSessionsPanel(props: { |
| 64 | /** The instance (class x course) these lessons belong to. */ | 59 | /** The instance (class x course) these lessons belong to. */ |
| 65 | instanceId: string; | 60 | instanceId: string; |
| 66 | roster: Enrollment[]; | 61 | roster: Enrollment[]; |
| 62 | + teachers: PersonRef[]; | ||
| 67 | canManage: boolean; | 63 | canManage: boolean; |
| 64 | + canManageStaff: boolean; | ||
| 68 | active: boolean; | 65 | active: boolean; |
| 69 | createOpen: boolean; | 66 | createOpen: boolean; |
| 70 | onCreateOpenChange: (open: boolean) => void; | 67 | onCreateOpenChange: (open: boolean) => void; |
| @@ -85,14 +82,25 @@ export function CourseSessionsPanel(props: { | |||
| 85 | const [startsTime, setStartsTime] = createSignal(""); | 82 | const [startsTime, setStartsTime] = createSignal(""); |
| 86 | const [endsDate, setEndsDate] = createSignal(""); | 83 | const [endsDate, setEndsDate] = createSignal(""); |
| 87 | const [endsTime, setEndsTime] = createSignal(""); | 84 | const [endsTime, setEndsTime] = createSignal(""); |
| 85 | + const [teacherId, setTeacherId] = createSignal(""); | ||
| 88 | const [error, setError] = createSignal(""); | 86 | const [error, setError] = createSignal(""); |
| 89 | const [flash, setFlash] = createFlash(); | 87 | const [flash, setFlash] = createFlash(); |
| 90 | const [pending, setPending] = createSignal(false); | 88 | const [pending, setPending] = createSignal(false); |
| 91 | const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() }))); | 89 | const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() }))); |
| 92 | const [detailAttendance] = createResource( | 90 | const [detailAttendance] = createResource( |
| 93 | - () => detailSession()?.id ?? null, | 91 | + () => (props.canManage ? detailSession()?.id ?? null : null), |
| 94 | async (sessionId) => (await getSessionAttendance(sessionId)).items, | 92 | async (sessionId) => (await getSessionAttendance(sessionId)).items, |
| 95 | ); | 93 | ); |
| 94 | + const teacherOptions = createMemo(() => { | ||
| 95 | + const current = editingSession()?.teacher; | ||
| 96 | + if (!current || props.teachers.some((teacher) => teacher.id === current.id)) return props.teachers; | ||
| 97 | + return [current, ...props.teachers]; | ||
| 98 | + }); | ||
| 99 | + const attendanceTargetCount = (session: CourseSession) => { | ||
| 100 | + const teacherIsStudent = props.roster.some((row) => row.user.id === session.teacher.id); | ||
| 101 | + const teacherHasRow = detailAttendance()?.some((row) => row.user.id === session.teacher.id) ?? false; | ||
| 102 | + return props.roster.length + (!teacherIsStudent && (props.canManageStaff || teacherHasRow) ? 1 : 0); | ||
| 103 | + }; | ||
| 96 | 104 | ||
| 97 | const resetForm = () => { | 105 | const resetForm = () => { |
| 98 | setTopic(""); | 106 | setTopic(""); |
| @@ -100,6 +108,7 @@ export function CourseSessionsPanel(props: { | |||
| 100 | setStartsTime(""); | 108 | setStartsTime(""); |
| 101 | setEndsDate(""); | 109 | setEndsDate(""); |
| 102 | setEndsTime(""); | 110 | setEndsTime(""); |
| 111 | + setTeacherId(""); | ||
| 103 | setError(""); | 112 | setError(""); |
| 104 | setEditingSession(null); | 113 | setEditingSession(null); |
| 105 | }; | 114 | }; |
| @@ -111,6 +120,7 @@ export function CourseSessionsPanel(props: { | |||
| 111 | setStartsTime(msToTimeInput(session.starts_at)); | 120 | setStartsTime(msToTimeInput(session.starts_at)); |
| 112 | setEndsDate(session.ends_at != null ? msToDateInput(session.ends_at) : ""); | 121 | setEndsDate(session.ends_at != null ? msToDateInput(session.ends_at) : ""); |
| 113 | setEndsTime(session.ends_at != null ? msToTimeInput(session.ends_at) : ""); | 122 | setEndsTime(session.ends_at != null ? msToTimeInput(session.ends_at) : ""); |
| 123 | + setTeacherId(session.teacher.id); | ||
| 114 | setError(""); | 124 | setError(""); |
| 115 | props.onCreateOpenChange(false); | 125 | props.onCreateOpenChange(false); |
| 116 | }; | 126 | }; |
| @@ -157,6 +167,7 @@ export function CourseSessionsPanel(props: { | |||
| 157 | if (current) { | 167 | if (current) { |
| 158 | await patchSessionById(current.id, { | 168 | await patchSessionById(current.id, { |
| 159 | topic: topic().trim() || "", | 169 | topic: topic().trim() || "", |
| 170 | + teacher_id: teacherId(), | ||
| 160 | starts_at, | 171 | starts_at, |
| 161 | ends_at, | 172 | ends_at, |
| 162 | }); | 173 | }); |
| @@ -165,6 +176,7 @@ export function CourseSessionsPanel(props: { | |||
| 165 | await postInstanceSession(props.instanceId, { | 176 | await postInstanceSession(props.instanceId, { |
| 166 | starts_at, | 177 | starts_at, |
| 167 | ...(topic().trim() ? { topic: topic().trim() } : {}), | 178 | ...(topic().trim() ? { topic: topic().trim() } : {}), |
| 179 | + ...(teacherId() ? { teacher_id: teacherId() } : {}), | ||
| 168 | ...(ends_at != null ? { ends_at } : {}), | 180 | ...(ends_at != null ? { ends_at } : {}), |
| 169 | }); | 181 | }); |
| 170 | setFlash(t("common.created")); | 182 | setFlash(t("common.created")); |
| @@ -274,6 +286,21 @@ export function CourseSessionsPanel(props: { | |||
| 274 | <Label for="session-topic">{t("sessions.topic")}</Label> | 286 | <Label for="session-topic">{t("sessions.topic")}</Label> |
| 275 | <Input id="session-topic" value={topic()} maxlength={200} onInput={(e) => setTopic(e.currentTarget.value)} /> | 287 | <Input id="session-topic" value={topic()} maxlength={200} onInput={(e) => setTopic(e.currentTarget.value)} /> |
| 276 | </div> | 288 | </div> |
| 289 | + <div class="space-y-1.5"> | ||
| 290 | + <Label for="session-teacher">{t("sessions.teacher")}</Label> | ||
| 291 | + <Select | ||
| 292 | + id="session-teacher" | ||
| 293 | + value={teacherId()} | ||
| 294 | + onChange={(e) => setTeacherId(e.currentTarget.value)} | ||
| 295 | + > | ||
| 296 | + <Show when={!editingSession()}> | ||
| 297 | + <option value="">{t("sessions.defaultTeacher")}</option> | ||
| 298 | + </Show> | ||
| 299 | + <For each={teacherOptions()}> | ||
| 300 | + {(teacher) => <option value={teacher.id}>{personLabel(teacher)}</option>} | ||
| 301 | + </For> | ||
| 302 | + </Select> | ||
| 303 | + </div> | ||
| 277 | <div class="grid gap-3"> | 304 | <div class="grid gap-3"> |
| 278 | <div class="space-y-1.5"> | 305 | <div class="space-y-1.5"> |
| 279 | <Label for="session-starts">{t("events.starts")}</Label> | 306 | <Label for="session-starts">{t("events.starts")}</Label> |
| @@ -322,7 +349,9 @@ export function CourseSessionsPanel(props: { | |||
| 322 | <DetailField label={t("sessions.teacher")} value={personLabel(session.teacher)} /> | 349 | <DetailField label={t("sessions.teacher")} value={personLabel(session.teacher)} /> |
| 323 | <DetailField label={t("events.starts")} value={formatDateTime(session.starts_at, locale())} /> | 350 | <DetailField label={t("events.starts")} value={formatDateTime(session.starts_at, locale())} /> |
| 324 | <DetailField label={t("events.ends")} value={formatDateTime(session.ends_at, locale())} /> | 351 | <DetailField label={t("events.ends")} value={formatDateTime(session.ends_at, locale())} /> |
| 325 | - <DetailField label={t("attendance.title")} value={`${detailAttendance()?.length ?? 0} / ${props.roster.length}`} mono /> | 352 | + <Show when={props.canManage}> |
| 353 | + <DetailField label={t("attendance.title")} value={`${detailAttendance()?.length ?? 0} / ${attendanceTargetCount(session)}`} mono /> | ||
| 354 | + </Show> | ||
| 326 | </div> | 355 | </div> |
| 327 | <Show when={props.canManage}> | 356 | <Show when={props.canManage}> |
| 328 | <div class="flex gap-2"> | 357 | <div class="flex gap-2"> |
| @@ -350,7 +379,14 @@ export function CourseSessionsPanel(props: { | |||
| 350 | description={selectedSession() ? `${selectedSession()!.topic || t("sessions.untitled")} · ${formatDateTime(selectedSession()!.starts_at, locale())}` : undefined} | 379 | description={selectedSession() ? `${selectedSession()!.topic || t("sessions.untitled")} · ${formatDateTime(selectedSession()!.starts_at, locale())}` : undefined} |
| 351 | > | 380 | > |
| 352 | <Show when={selectedSession()}> | 381 | <Show when={selectedSession()}> |
| 353 | - {(session) => <RollCall sessionId={session().id} roster={props.roster} />} | 382 | + {(session) => ( |
| 383 | + <SessionRollCall | ||
| 384 | + sessionId={session().id} | ||
| 385 | + roster={props.roster} | ||
| 386 | + teacher={session().teacher} | ||
| 387 | + canMarkTeacher={props.canManageStaff} | ||
| 388 | + /> | ||
| 389 | + )} | ||
| 354 | </Show> | 390 | </Show> |
| 355 | </SidePanel> | 391 | </SidePanel> |
| 356 | 392 | ||
| @@ -378,105 +414,3 @@ export function CourseSessionsPanel(props: { | |||
| 378 | </div> | 414 | </div> |
| 379 | ); | 415 | ); |
| 380 | } | 416 | } |
| 381 | - | ||
| 382 | -function RollCall(props: { sessionId: string; roster: Enrollment[] }) { | ||
| 383 | - const t = useT(); | ||
| 384 | - const [attendance, { refetch }] = createResource( | ||
| 385 | - () => props.sessionId, | ||
| 386 | - async (sessionId) => (await getSessionAttendance(sessionId)).items, | ||
| 387 | - ); | ||
| 388 | - const rows = createMemo(() => new Map((attendance() ?? []).map((row) => [row.user.id, row]))); | ||
| 389 | - const [local, setLocal] = createSignal<Record<string, AttendanceStatus>>({}); | ||
| 390 | - const [error, setError] = createSignal(""); | ||
| 391 | - const [flash, setFlash] = createFlash(); | ||
| 392 | - const [page, setPage] = createSignal(0); | ||
| 393 | - const totalPages = createMemo(() => Math.max(1, Math.ceil(props.roster.length / ROLL_CALL_PAGE_SIZE))); | ||
| 394 | - const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); | ||
| 395 | - const visibleRoster = createMemo(() => { | ||
| 396 | - const start = safePage() * ROLL_CALL_PAGE_SIZE; | ||
| 397 | - return props.roster.slice(start, start + ROLL_CALL_PAGE_SIZE); | ||
| 398 | - }); | ||
| 399 | - | ||
| 400 | - createEffect(() => { | ||
| 401 | - if (page() >= totalPages()) setPage(totalPages() - 1); | ||
| 402 | - }); | ||
| 403 | - | ||
| 404 | - const statusFor = (userId: string) => local()[userId] ?? rows().get(userId)?.status ?? "present"; | ||
| 405 | - const save = async (userId: string) => { | ||
| 406 | - setError(""); | ||
| 407 | - try { | ||
| 408 | - await postSessionAttendance(props.sessionId, { user_id: userId, status: statusFor(userId) }); | ||
| 409 | - await refetch(); | ||
| 410 | - setFlash(t("common.saved")); | ||
| 411 | - } catch (err) { | ||
| 412 | - setError(formatApiError(err)); | ||
| 413 | - } | ||
| 414 | - }; | ||
| 415 | - // Marking cannot undo itself — a wrong mark stays until the row is removed. | ||
| 416 | - // Roster rows are students, so the session's teacher may clear them. | ||
| 417 | - const clear = async (userId: string) => { | ||
| 418 | - setError(""); | ||
| 419 | - try { | ||
| 420 | - await deleteSessionAttendanceByUserId(props.sessionId, userId); | ||
| 421 | - setLocal((current) => { | ||
| 422 | - const next = { ...current }; | ||
| 423 | - delete next[userId]; | ||
| 424 | - return next; | ||
| 425 | - }); | ||
| 426 | - await refetch(); | ||
| 427 | - setFlash(t("common.deleted")); | ||
| 428 | - } catch (err) { | ||
| 429 | - setError(formatApiError(err)); | ||
| 430 | - } | ||
| 431 | - }; | ||
| 432 | - | ||
| 433 | - return ( | ||
| 434 | - <div class="space-y-3"> | ||
| 435 | - <Show when={flash()}> | ||
| 436 | - <Alert variant="success">{flash()}</Alert> | ||
| 437 | - </Show> | ||
| 438 | - {error() && <Alert variant="destructive">{error()}</Alert>} | ||
| 439 | - <Show | ||
| 440 | - when={props.roster.length > 0} | ||
| 441 | - fallback={<EmptyState kind="people" title={t("sessions.emptyRoster")} />} | ||
| 442 | - > | ||
| 443 | - <For each={visibleRoster()}> | ||
| 444 | - {(row) => { | ||
| 445 | - const saved = () => rows().get(row.user.id) as SessionAttendance | undefined; | ||
| 446 | - return ( | ||
| 447 | - <div class="space-y-2 rounded-lg border border-border/50 bg-card px-4 py-3"> | ||
| 448 | - <div class="min-w-0"> | ||
| 449 | - <p class="truncate font-medium">{personLabel(row.user)}</p> | ||
| 450 | - <p class="mono truncate text-xs text-muted-foreground">{row.user.id}</p> | ||
| 451 | - </div> | ||
| 452 | - <div class="flex items-center gap-2"> | ||
| 453 | - <div class="min-w-0 flex-1"> | ||
| 454 | - <AttendanceStatusPicker hideLabel hideDetail id={`session-${props.sessionId}-${row.user.id}`} value={statusFor(row.user.id)} onChange={(status) => setLocal((current) => ({ ...current, [row.user.id]: status }))} /> | ||
| 455 | - </div> | ||
| 456 | - <Button type="button" class="h-10 w-24 shrink-0 rounded-lg" variant={saved() ? "outline" : "default"} onClick={() => void save(row.user.id)}> | ||
| 457 | - {saved() ? t("common.update") : t("common.save")} | ||
| 458 | - </Button> | ||
| 459 | - <Show when={saved()}> | ||
| 460 | - <Button | ||
| 461 | - type="button" | ||
| 462 | - variant="ghost" | ||
| 463 | - size="icon" | ||
| 464 | - class="h-10 w-10 shrink-0 text-destructive hover:bg-destructive/10" | ||
| 465 | - aria-label={t("common.remove")} | ||
| 466 | - onClick={() => void clear(row.user.id)} | ||
| 467 | - > | ||
| 468 | - <IconTrash class="h-4 w-4" /> | ||
| 469 | - </Button> | ||
| 470 | - </Show> | ||
| 471 | - </div> | ||
| 472 | - </div> | ||
| 473 | - ); | ||
| 474 | - }} | ||
| 475 | - </For> | ||
| 476 | - </Show> | ||
| 477 | - <Show when={totalPages() > 1}> | ||
| 478 | - <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} /> | ||
| 479 | - </Show> | ||
| 480 | - </div> | ||
| 481 | - ); | ||
| 482 | -} | ||
src/components/sessions/session-roll-call.test.tsxadded+72 -0
| @@ -0,0 +1,72 @@ | |||
| 1 | +import { fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | ||
| 2 | +import { SessionRollCall } from "@/components/sessions/session-roll-call"; | ||
| 3 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 4 | + | ||
| 5 | +const { getSessionAttendance, postSessionAttendance } = vi.hoisted(() => ({ | ||
| 6 | + getSessionAttendance: vi.fn(async () => ({ items: [], total: 0, limit: null, offset: 0 })), | ||
| 7 | + postSessionAttendance: vi.fn(async (_sessionId: string, body: { user_id: string; status: string }) => ({ | ||
| 8 | + id: `attendance-${body.user_id}`, | ||
| 9 | + session: "session-1", | ||
| 10 | + class_course: "instance-1", | ||
| 11 | + user: { id: body.user_id, username: body.user_id, display_name: null }, | ||
| 12 | + status: body.status, | ||
| 13 | + marked_by: { id: "manager-1", username: "manager", display_name: null }, | ||
| 14 | + })), | ||
| 15 | +})); | ||
| 16 | + | ||
| 17 | +vi.mock("@/api/sessions", () => ({ | ||
| 18 | + getSessionAttendance, | ||
| 19 | + postSessionAttendance, | ||
| 20 | + deleteSessionAttendanceByUserId: vi.fn(async () => undefined), | ||
| 21 | +})); | ||
| 22 | + | ||
| 23 | +vi.mock("@/api/settings", () => ({ | ||
| 24 | + getSettings: async () => ({ attendance_statuses: ["present", "absent", "late", "excused"] }), | ||
| 25 | +})); | ||
| 26 | + | ||
| 27 | +const roster = [{ | ||
| 28 | + id: "enrollment-1", | ||
| 29 | + class_course: "instance-1", | ||
| 30 | + user: { id: "student-1", username: "ada", display_name: "Ada Öğrenci" }, | ||
| 31 | + enrolled_by: { id: "manager-1", username: "manager", display_name: null }, | ||
| 32 | + source: null, | ||
| 33 | +}]; | ||
| 34 | + | ||
| 35 | +const teacher = { id: "teacher-1", username: "teacher", display_name: "Deniz Öğretmen" }; | ||
| 36 | + | ||
| 37 | +afterEach(() => { | ||
| 38 | + vi.clearAllMocks(); | ||
| 39 | +}); | ||
| 40 | + | ||
| 41 | +test("lets manager-level users mark the session teacher alongside enrolled students", async () => { | ||
| 42 | + render(() => ( | ||
| 43 | + <PreferencesProvider> | ||
| 44 | + <SessionRollCall sessionId="session-1" roster={roster} teacher={teacher} canMarkTeacher /> | ||
| 45 | + </PreferencesProvider> | ||
| 46 | + )); | ||
| 47 | + | ||
| 48 | + expect(await screen.findByText("Ada Öğrenci")).toBeTruthy(); | ||
| 49 | + expect(screen.getByText("Deniz Öğretmen")).toBeTruthy(); | ||
| 50 | + | ||
| 51 | + const saveButtons = screen.getAllByRole("button", { name: "Save" }); | ||
| 52 | + fireEvent.click(saveButtons[1]!); | ||
| 53 | + | ||
| 54 | + await waitFor(() => { | ||
| 55 | + expect(postSessionAttendance).toHaveBeenCalledWith("session-1", { | ||
| 56 | + user_id: "teacher-1", | ||
| 57 | + status: "present", | ||
| 58 | + }); | ||
| 59 | + }); | ||
| 60 | +}); | ||
| 61 | + | ||
| 62 | +test("keeps the teacher out of roll call for users without staff-attendance rights", async () => { | ||
| 63 | + render(() => ( | ||
| 64 | + <PreferencesProvider> | ||
| 65 | + <SessionRollCall sessionId="session-1" roster={roster} teacher={teacher} canMarkTeacher={false} /> | ||
| 66 | + </PreferencesProvider> | ||
| 67 | + )); | ||
| 68 | + | ||
| 69 | + expect(await screen.findByText("Ada Öğrenci")).toBeTruthy(); | ||
| 70 | + expect(screen.queryByText("Deniz Öğretmen")).toBeNull(); | ||
| 71 | + expect(screen.getAllByRole("button", { name: "Save" })).toHaveLength(1); | ||
| 72 | +}); | ||
src/components/sessions/session-roll-call.tsxadded+169 -0
| @@ -0,0 +1,169 @@ | |||
| 1 | +import { For, Show, createEffect, createMemo, createSignal } from "solid-js"; | ||
| 2 | +import { createResource } from "@/lib/create-resource"; | ||
| 3 | +import { deleteSessionAttendanceByUserId, getSessionAttendance, postSessionAttendance } from "@/api/sessions"; | ||
| 4 | +import { formatApiError } from "@/api/client"; | ||
| 5 | +import type { AttendanceStatus, Enrollment, PersonRef, SessionAttendance } from "@/api/client"; | ||
| 6 | +import { AttendanceStatusPicker } from "@/components/events/attendance-status-picker"; | ||
| 7 | +import { Alert } from "@/components/ui/alert"; | ||
| 8 | +import { Badge } from "@/components/ui/badge"; | ||
| 9 | +import { Button } from "@/components/ui/button"; | ||
| 10 | +import { EmptyState } from "@/components/ui/empty-state"; | ||
| 11 | +import { IconTrash } from "@/components/ui/icons"; | ||
| 12 | +import { PaginationControls } from "@/components/ui/pagination-controls"; | ||
| 13 | +import { createFlash } from "@/lib/flash"; | ||
| 14 | +import { personLabel } from "@/lib/person"; | ||
| 15 | +import { useT } from "@/stores/preferences-context"; | ||
| 16 | + | ||
| 17 | +const PAGE_SIZE = 8; | ||
| 18 | + | ||
| 19 | +type RollCallTarget = { | ||
| 20 | + user: PersonRef; | ||
| 21 | + isTeacher: boolean; | ||
| 22 | +}; | ||
| 23 | + | ||
| 24 | +export function SessionRollCall(props: { | ||
| 25 | + sessionId: string; | ||
| 26 | + roster: Enrollment[]; | ||
| 27 | + teacher: PersonRef; | ||
| 28 | + canMarkTeacher: boolean; | ||
| 29 | +}) { | ||
| 30 | + const t = useT(); | ||
| 31 | + const [attendance, { refetch }] = createResource( | ||
| 32 | + () => props.sessionId, | ||
| 33 | + async (sessionId) => (await getSessionAttendance(sessionId)).items, | ||
| 34 | + ); | ||
| 35 | + const rows = createMemo(() => new Map((attendance() ?? []).map((row) => [row.user.id, row]))); | ||
| 36 | + const targets = createMemo<RollCallTarget[]>(() => [ | ||
| 37 | + ...props.roster.map((row) => ({ user: row.user, isTeacher: false })), | ||
| 38 | + ...(props.canMarkTeacher && !props.roster.some((row) => row.user.id === props.teacher.id) | ||
| 39 | + ? [{ user: props.teacher, isTeacher: true }] | ||
| 40 | + : []), | ||
| 41 | + ]); | ||
| 42 | + const [local, setLocal] = createSignal<Record<string, AttendanceStatus>>({}); | ||
| 43 | + const [pendingUserId, setPendingUserId] = createSignal<string | null>(null); | ||
| 44 | + const [error, setError] = createSignal(""); | ||
| 45 | + const [flash, setFlash] = createFlash(); | ||
| 46 | + const [page, setPage] = createSignal(0); | ||
| 47 | + const totalPages = createMemo(() => Math.max(1, Math.ceil(targets().length / PAGE_SIZE))); | ||
| 48 | + const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); | ||
| 49 | + const visibleTargets = createMemo(() => { | ||
| 50 | + const start = safePage() * PAGE_SIZE; | ||
| 51 | + return targets().slice(start, start + PAGE_SIZE); | ||
| 52 | + }); | ||
| 53 | + | ||
| 54 | + createEffect(() => { | ||
| 55 | + if (page() >= totalPages()) setPage(totalPages() - 1); | ||
| 56 | + }); | ||
| 57 | + | ||
| 58 | + const statusFor = (userId: string) => local()[userId] ?? rows().get(userId)?.status ?? "present"; | ||
| 59 | + const save = async (userId: string) => { | ||
| 60 | + if (pendingUserId()) return; | ||
| 61 | + setError(""); | ||
| 62 | + setPendingUserId(userId); | ||
| 63 | + try { | ||
| 64 | + await postSessionAttendance(props.sessionId, { user_id: userId, status: statusFor(userId) }); | ||
| 65 | + await refetch(); | ||
| 66 | + setFlash(t("common.saved")); | ||
| 67 | + } catch (err) { | ||
| 68 | + setError(formatApiError(err)); | ||
| 69 | + } finally { | ||
| 70 | + setPendingUserId(null); | ||
| 71 | + } | ||
| 72 | + }; | ||
| 73 | + | ||
| 74 | + const clear = async (userId: string) => { | ||
| 75 | + if (pendingUserId()) return; | ||
| 76 | + setError(""); | ||
| 77 | + setPendingUserId(userId); | ||
| 78 | + try { | ||
| 79 | + await deleteSessionAttendanceByUserId(props.sessionId, userId); | ||
| 80 | + setLocal((current) => { | ||
| 81 | + const next = { ...current }; | ||
| 82 | + delete next[userId]; | ||
| 83 | + return next; | ||
| 84 | + }); | ||
| 85 | + await refetch(); | ||
| 86 | + setFlash(t("common.deleted")); | ||
| 87 | + } catch (err) { | ||
| 88 | + setError(formatApiError(err)); | ||
| 89 | + } finally { | ||
| 90 | + setPendingUserId(null); | ||
| 91 | + } | ||
| 92 | + }; | ||
| 93 | + | ||
| 94 | + return ( | ||
| 95 | + <div class="space-y-3"> | ||
| 96 | + <Show when={flash()}> | ||
| 97 | + <Alert variant="success">{flash()}</Alert> | ||
| 98 | + </Show> | ||
| 99 | + <Show when={error()}> | ||
| 100 | + <Alert variant="destructive">{error()}</Alert> | ||
| 101 | + </Show> | ||
| 102 | + <Show | ||
| 103 | + when={targets().length > 0} | ||
| 104 | + fallback={<EmptyState kind="people" title={t("sessions.emptyRoster")} />} | ||
| 105 | + > | ||
| 106 | + <For each={visibleTargets()}> | ||
| 107 | + {(target) => { | ||
| 108 | + const saved = () => rows().get(target.user.id) as SessionAttendance | undefined; | ||
| 109 | + const pending = () => pendingUserId() === target.user.id; | ||
| 110 | + return ( | ||
| 111 | + <div class="space-y-2 rounded-lg border border-border/50 bg-card px-4 py-3"> | ||
| 112 | + <div class="flex min-w-0 items-center gap-2"> | ||
| 113 | + <div class="min-w-0 flex-1"> | ||
| 114 | + <p class="truncate font-medium">{personLabel(target.user)}</p> | ||
| 115 | + <Show when={target.user.display_name}> | ||
| 116 | + <p class="truncate text-xs text-muted-foreground">{target.user.username}</p> | ||
| 117 | + </Show> | ||
| 118 | + </div> | ||
| 119 | + <Show when={target.isTeacher}> | ||
| 120 | + <Badge variant="secondary" class="shrink-0 rounded-full">{t("sessions.teacher")}</Badge> | ||
| 121 | + </Show> | ||
| 122 | + </div> | ||
| 123 | + <div class="flex items-center gap-2"> | ||
| 124 | + <div class="min-w-0 flex-1"> | ||
| 125 | + <AttendanceStatusPicker | ||
| 126 | + hideLabel | ||
| 127 | + hideDetail | ||
| 128 | + id={`session-${props.sessionId}-${target.user.id}`} | ||
| 129 | + value={statusFor(target.user.id)} | ||
| 130 | + onChange={(status) => setLocal((current) => ({ ...current, [target.user.id]: status }))} | ||
| 131 | + /> | ||
| 132 | + </div> | ||
| 133 | + <Button | ||
| 134 | + type="button" | ||
| 135 | + class="h-10 w-24 shrink-0 rounded-lg" | ||
| 136 | + variant={saved() ? "outline" : "default"} | ||
| 137 | + disabled={pendingUserId() != null} | ||
| 138 | + onClick={() => void save(target.user.id)} | ||
| 139 | + > | ||
| 140 | + {saved() ? t("common.update") : t("common.save")} | ||
| 141 | + </Button> | ||
| 142 | + <Show when={saved()}> | ||
| 143 | + <Button | ||
| 144 | + type="button" | ||
| 145 | + variant="ghost" | ||
| 146 | + size="icon" | ||
| 147 | + class="h-10 w-10 shrink-0 text-destructive hover:bg-destructive/10" | ||
| 148 | + aria-label={t("common.remove")} | ||
| 149 | + disabled={pendingUserId() != null} | ||
| 150 | + onClick={() => void clear(target.user.id)} | ||
| 151 | + > | ||
| 152 | + <IconTrash class="h-4 w-4" /> | ||
| 153 | + </Button> | ||
| 154 | + </Show> | ||
| 155 | + <Show when={pending()}> | ||
| 156 | + <span class="sr-only" aria-live="polite">{t("common.loading")}</span> | ||
| 157 | + </Show> | ||
| 158 | + </div> | ||
| 159 | + </div> | ||
| 160 | + ); | ||
| 161 | + }} | ||
| 162 | + </For> | ||
| 163 | + </Show> | ||
| 164 | + <Show when={totalPages() > 1}> | ||
| 165 | + <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} /> | ||
| 166 | + </Show> | ||
| 167 | + </div> | ||
| 168 | + ); | ||
| 169 | +} | ||
src/pages/instance-detail-page.tsxmodified+2 -0
| @@ -568,7 +568,9 @@ function InstanceDetailContent() { | |||
| 568 | <CourseSessionsPanel | 568 | <CourseSessionsPanel |
| 569 | instanceId={id()} | 569 | instanceId={id()} |
| 570 | roster={roster() ?? []} | 570 | roster={roster() ?? []} |
| 571 | + teachers={inst().teachers} | ||
| 571 | canManage={canManage()} | 572 | canManage={canManage()} |
| 573 | + canManageStaff={canStaff()} | ||
| 572 | active={tab() === "sessions"} | 574 | active={tab() === "sessions"} |
| 573 | createOpen={showSessionForm()} | 575 | createOpen={showSessionForm()} |
| 574 | onCreateOpenChange={setShowSessionForm} | 576 | onCreateOpenChange={setShowSessionForm} |