🧑🏫 rebuild class browsing
Browse files at this commite7c5b53e7c858a6daeea06a6a0754bcb35483ea7Parent: 53173ce- fix
- 🧑🏫 rebuild class browsing
6 changed files +396 -294
- src/components/courses/course-card.tsx +57 -0
- src/components/ui/data-toolbar.tsx +7 -4
- src/components/ui/select.tsx +5 -3
- src/pages/course-detail-page.tsx +58 -79
- src/pages/courses-page.test.tsx +114 -0
- src/pages/courses-page.tsx +155 -208
src/components/courses/course-card.tsxadded+57 -0
| @@ -0,0 +1,57 @@ | |||
| 1 | +import { Show } from "solid-js"; | ||
| 2 | +import { Link } from "@tanstack/solid-router"; | ||
| 3 | +import type { Course } from "@/api/client"; | ||
| 4 | +import { Badge } from "@/components/ui/badge"; | ||
| 5 | +import { IconBook, IconChevronRight, IconUsers } from "@/components/ui/icons"; | ||
| 6 | +import { personLabel } from "@/lib/person"; | ||
| 7 | + | ||
| 8 | +export function CourseCard(props: { | ||
| 9 | + course: Course; | ||
| 10 | + term: string; | ||
| 11 | + enrolled?: boolean; | ||
| 12 | + labels: { | ||
| 13 | + capacity: string; | ||
| 14 | + enrolled: string; | ||
| 15 | + kind: string; | ||
| 16 | + }; | ||
| 17 | +}) { | ||
| 18 | + const teachers = () => [props.course.creator, ...(props.course.teachers ?? [])] | ||
| 19 | + .filter((person, index, all) => all.findIndex((row) => row.id === person.id) === index); | ||
| 20 | + | ||
| 21 | + return ( | ||
| 22 | + <Link | ||
| 23 | + to="/courses/$id" | ||
| 24 | + params={{ id: props.course.id }} | ||
| 25 | + class="group grid gap-4 rounded-xl border border-border bg-card px-4 py-4 outline-hidden transition-colors hover:border-primary/35 hover:bg-muted/20 focus-visible:ring-2 focus-visible:ring-ring sm:grid-cols-[minmax(0,1fr)_auto] sm:items-center" | ||
| 26 | + > | ||
| 27 | + <div class="flex min-w-0 items-start gap-3"> | ||
| 28 | + <span class="mt-0.5 flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-muted text-muted-foreground"> | ||
| 29 | + <IconBook class="h-4 w-4" /> | ||
| 30 | + </span> | ||
| 31 | + <div class="min-w-0"> | ||
| 32 | + <div class="flex flex-wrap items-center gap-2"> | ||
| 33 | + <h2 class="truncate font-display text-base font-semibold tracking-tight">{props.course.title}</h2> | ||
| 34 | + <Badge variant="outline" class="rounded-md text-[10px] font-medium">{props.labels.kind}</Badge> | ||
| 35 | + <Show when={props.enrolled}><Badge variant="secondary" class="rounded-md text-[10px]">{props.labels.enrolled}</Badge></Show> | ||
| 36 | + </div> | ||
| 37 | + <p class="mt-1 line-clamp-1 text-sm text-muted-foreground">{props.course.description || "—"}</p> | ||
| 38 | + <div class="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground"> | ||
| 39 | + <span>{props.term}</span> | ||
| 40 | + <span class="flex min-w-0 items-center gap-1.5"> | ||
| 41 | + <IconUsers class="h-3.5 w-3.5 shrink-0" /> | ||
| 42 | + <span class="truncate">{teachers().map(personLabel).join(", ")}</span> | ||
| 43 | + </span> | ||
| 44 | + </div> | ||
| 45 | + </div> | ||
| 46 | + </div> | ||
| 47 | + | ||
| 48 | + <div class="flex items-center justify-between gap-4 border-t border-border/60 pt-3 sm:border-l sm:border-t-0 sm:pl-5 sm:pt-0"> | ||
| 49 | + <div class="text-left sm:text-right"> | ||
| 50 | + <p class="text-[10px] font-medium uppercase tracking-wider text-muted-foreground">{props.labels.capacity}</p> | ||
| 51 | + <p class="mt-0.5 font-semibold tabular-nums">{props.course.capacity ?? "—"}</p> | ||
| 52 | + </div> | ||
| 53 | + <IconChevronRight class="h-4 w-4 text-muted-foreground transition-transform group-hover:translate-x-0.5 group-hover:text-foreground" /> | ||
| 54 | + </div> | ||
| 55 | + </Link> | ||
| 56 | + ); | ||
| 57 | +} | ||
src/components/ui/data-toolbar.tsxmodified+7 -4
| @@ -10,14 +10,15 @@ export function DataToolbar(props: { | |||
| 10 | onSearchInput?: (value: string) => void; | 10 | onSearchInput?: (value: string) => void; |
| 11 | filters?: JSX.Element; | 11 | filters?: JSX.Element; |
| 12 | actions?: JSX.Element; | 12 | actions?: JSX.Element; |
| 13 | + inline?: boolean; | ||
| 13 | class?: string; | 14 | class?: string; |
| 14 | }) { | 15 | }) { |
| 15 | const hasSearch = () => props.searchValue !== undefined || props.searchPlaceholder !== undefined || props.onSearchInput !== undefined; | 16 | const hasSearch = () => props.searchValue !== undefined || props.searchPlaceholder !== undefined || props.onSearchInput !== undefined; |
| 16 | return ( | 17 | return ( |
| 17 | - <div class={cn("flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between", props.class)}> | 18 | + <div class={cn("flex gap-3", props.inline ? "flex-row items-center" : "flex-col sm:flex-row sm:items-center sm:justify-between", props.class)}> |
| 18 | - <div class="flex min-w-0 flex-1 flex-col gap-2 sm:flex-row sm:flex-wrap sm:items-center"> | 19 | + <div class={cn("flex min-w-0 flex-1 gap-2", props.inline ? "flex-row flex-nowrap items-center" : "flex-col sm:flex-row sm:flex-wrap sm:items-center")}> |
| 19 | <Show when={hasSearch()}> | 20 | <Show when={hasSearch()}> |
| 20 | - <div class="relative w-full sm:max-w-72"> | 21 | + <div class={cn("relative w-full sm:max-w-72", props.inline && "min-w-0 flex-1")}> |
| 21 | <IconSearch class="pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" /> | 22 | <IconSearch class="pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" /> |
| 22 | <Input | 23 | <Input |
| 23 | class="h-11 w-full rounded-xl bg-background pl-8 text-[13px]" | 24 | class="h-11 w-full rounded-xl bg-background pl-8 text-[13px]" |
| @@ -29,7 +30,9 @@ export function DataToolbar(props: { | |||
| 29 | </Show> | 30 | </Show> |
| 30 | {props.filters} | 31 | {props.filters} |
| 31 | </div> | 32 | </div> |
| 32 | - <div class="flex shrink-0 items-center gap-2 sm:justify-end">{props.actions}</div> | 33 | + <Show when={props.actions}> |
| 34 | + <div class="flex shrink-0 items-center gap-2 sm:justify-end">{props.actions}</div> | ||
| 35 | + </Show> | ||
| 33 | </div> | 36 | </div> |
| 34 | ); | 37 | ); |
| 35 | } | 38 | } |
src/components/ui/select.tsxmodified+5 -3
| @@ -80,12 +80,14 @@ export function DropdownSelect<T extends string | number = string>(props: Dropdo | |||
| 80 | ); | 80 | ); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| 83 | -export type SelectProps = ComponentProps<"select">; | 83 | +export type SelectProps = ComponentProps<"select"> & { |
| 84 | + wrapperClass?: string; | ||
| 85 | +}; | ||
| 84 | 86 | ||
| 85 | export function Select(props: SelectProps) { | 87 | export function Select(props: SelectProps) { |
| 86 | - const [local, rest] = splitProps(props, ["class"]); | 88 | + const [local, rest] = splitProps(props, ["class", "wrapperClass"]); |
| 87 | return ( | 89 | return ( |
| 88 | - <div class="relative w-full"> | 90 | + <div class={cn("relative w-full", local.wrapperClass)}> |
| 89 | <select | 91 | <select |
| 90 | class={cn( | 92 | class={cn( |
| 91 | "flex h-11 w-full appearance-none rounded-xl border border-black/8 dark:border-white/12 bg-card pl-3.5 pr-9 py-2 text-xs font-medium text-foreground transition-all duration-150", | 93 | "flex h-11 w-full appearance-none rounded-xl border border-black/8 dark:border-white/12 bg-card pl-3.5 pr-9 py-2 text-xs font-medium text-foreground transition-all duration-150", |
src/pages/course-detail-page.tsxmodified+58 -79
| @@ -69,7 +69,7 @@ function CourseDetailContent() { | |||
| 69 | return params().id; | 69 | return params().id; |
| 70 | }); | 70 | }); |
| 71 | 71 | ||
| 72 | - const [courseTab, setCourseTab] = createSignal("subjects"); | 72 | + const [courseTab, setCourseTab] = createSignal("overview"); |
| 73 | const [course, { refetch: refetchCourse }] = createResource(id, (courseId) => getCourseById(courseId)); | 73 | const [course, { refetch: refetchCourse }] = createResource(id, (courseId) => getCourseById(courseId)); |
| 74 | const [terms] = createResource(async () => (await getTerms({ limit: 100 })).items); | 74 | const [terms] = createResource(async () => (await getTerms({ limit: 100 })).items); |
| 75 | const [settings] = createResource(() => getSettings()); | 75 | const [settings] = createResource(() => getSettings()); |
| @@ -147,7 +147,7 @@ function CourseDetailContent() { | |||
| 147 | }; | 147 | }; |
| 148 | const courseKindLabel = (value: CourseKind | undefined) => | 148 | const courseKindLabel = (value: CourseKind | undefined) => |
| 149 | value === "study" ? t("courses.kind.study") : value === "club" ? t("courses.kind.club") : t("courses.kind.course"); | 149 | value === "study" ? t("courses.kind.study") : value === "club" ? t("courses.kind.club") : t("courses.kind.course"); |
| 150 | - const courseListPath = (value: CourseKind | undefined) => value === "study" ? "/studies" : value === "club" ? "/clubs" : "/courses"; | 150 | + const courseListSearch = (value: CourseKind | undefined) => value ? { kind: value } as never : {} as never; |
| 151 | 151 | ||
| 152 | const examCount = createMemo(() => exams()?.length ?? 0); | 152 | const examCount = createMemo(() => exams()?.length ?? 0); |
| 153 | const rosterCount = createMemo(() => roster()?.length ?? 0); | 153 | const rosterCount = createMemo(() => roster()?.length ?? 0); |
| @@ -283,7 +283,7 @@ function CourseDetailContent() { | |||
| 283 | description={c().description || "—"} | 283 | description={c().description || "—"} |
| 284 | actions={ | 284 | actions={ |
| 285 | <div class="detail-action-group"> | 285 | <div class="detail-action-group"> |
| 286 | - <Link to={courseListPath(c().kind)}> | 286 | + <Link to="/courses" search={courseListSearch(c().kind)}> |
| 287 | <Button variant="ghost" size="sm" class="w-full rounded-xl sm:w-auto"> | 287 | <Button variant="ghost" size="sm" class="w-full rounded-xl sm:w-auto"> |
| 288 | <IconChevronLeft class="h-4 w-4" /> | 288 | <IconChevronLeft class="h-4 w-4" /> |
| 289 | {t("common.back")} | 289 | {t("common.back")} |
| @@ -331,7 +331,7 @@ function CourseDetailContent() { | |||
| 331 | onConfirm={async () => { | 331 | onConfirm={async () => { |
| 332 | await wrap(async () => { | 332 | await wrap(async () => { |
| 333 | await deleteCourseById(id()); | 333 | await deleteCourseById(id()); |
| 334 | - void navigate({ to: courseListPath(c().kind) }); | 334 | + void navigate({ to: "/courses", search: courseListSearch(c().kind) }); |
| 335 | }); | 335 | }); |
| 336 | }} | 336 | }} |
| 337 | /> | 337 | /> |
| @@ -557,75 +557,53 @@ function CourseDetailContent() { | |||
| 557 | 557 | ||
| 558 | <Tabs value={courseTab()} onChange={setCourseTab} class="space-y-3"> | 558 | <Tabs value={courseTab()} onChange={setCourseTab} class="space-y-3"> |
| 559 | <TabsList> | 559 | <TabsList> |
| 560 | - <Show when={hasCourseManagementRights()}> | 560 | + <TabsTrigger value="overview">{t("courses.overview")}</TabsTrigger> |
| 561 | - <TabsTrigger value="teachers">{t("courses.teachers")}</TabsTrigger> | 561 | + <TabsTrigger value="work">{t("courses.work")}</TabsTrigger> |
| 562 | - </Show> | ||
| 563 | - <TabsTrigger value="subjects">{t("subjects.title")}</TabsTrigger> | ||
| 564 | - <TabsTrigger value="exams">{t("courses.exams")}</TabsTrigger> | ||
| 565 | - <TabsTrigger value="homework">{t("homework.title")}</TabsTrigger> | ||
| 566 | <TabsTrigger value="sessions">{t("sessions.title")}</TabsTrigger> | 562 | <TabsTrigger value="sessions">{t("sessions.title")}</TabsTrigger> |
| 567 | - <Show when={hasCourseManagementRights()}> | 563 | + <TabsTrigger value="people">{t("courses.people")}</TabsTrigger> |
| 568 | - <TabsTrigger value="roster">{t("courses.roster")}</TabsTrigger> | ||
| 569 | - </Show> | ||
| 570 | </TabsList> | 564 | </TabsList> |
| 571 | 565 | ||
| 572 | - <Show when={hasCourseManagementRights()}> | 566 | + <TabsContent value="overview" class="space-y-4"> |
| 573 | - <TabsContent value="teachers" forceMount class="space-y-4"> | 567 | + <div class="data-shell p-4"> |
| 574 | - <div class="tab-panel-header"> | 568 | + <h2 class="font-display text-base font-semibold">{t("courses.overview")}</h2> |
| 575 | - <p class="text-sm text-muted-foreground">{countDescription((c().teachers ?? []).length, t("courses.teachers"))}</p> | 569 | + <p class="mt-2 whitespace-pre-wrap text-sm text-muted-foreground">{c().description || "—"}</p> |
| 576 | - <Show when={canStaffCourse()}> | ||
| 577 | - <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowTeacherForm(true)}> | ||
| 578 | - <IconPlus class="h-4 w-4" /> | ||
| 579 | - {t("courses.assignTeacher")} | ||
| 580 | - </Button> | ||
| 581 | - </Show> | ||
| 582 | - </div> | ||
| 583 | - <CourseTeachersPanel courseId={id()} teachers={c().teachers ?? []} canStaff={canStaffCourse()} assignOpen={showTeacherForm()} onAssignOpenChange={setShowTeacherForm} onCourseUpdated={refetchCourse} /> | ||
| 584 | - </TabsContent> | ||
| 585 | - </Show> | ||
| 586 | - | ||
| 587 | - <TabsContent value="subjects" forceMount class="space-y-4"> | ||
| 588 | - <div class="tab-panel-header"> | ||
| 589 | - <p class="text-sm text-muted-foreground">{countDescription(subjectCount(), t("subjects.item"))}</p> | ||
| 590 | - <Show when={canManage()}> | ||
| 591 | - <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowSubjectForm(true)}> | ||
| 592 | - <IconPlus class="h-4 w-4" /> | ||
| 593 | - {t("subjects.add")} | ||
| 594 | - </Button> | ||
| 595 | - </Show> | ||
| 596 | </div> | 570 | </div> |
| 597 | - <CourseSubjectsPanel courseId={id()} canManage={canManage()} active createOpen={showSubjectForm()} onCreateOpenChange={setShowSubjectForm} onCountChange={setSubjectCount} /> | 571 | + <div class="data-shell p-4"> |
| 598 | - </TabsContent> | 572 | + <h2 class="font-display text-base font-semibold">{t("courses.upcoming")}</h2> |
| 599 | - | 573 | + <Show |
| 600 | - <TabsContent value="exams" forceMount class="space-y-4"> | 574 | + when={(exams() ?? []).filter((exam) => (exam.ends_at ?? exam.starts_at ?? 0) > Date.now()).sort((a, b) => (a.starts_at ?? a.ends_at ?? 0) - (b.starts_at ?? b.ends_at ?? 0))[0]} |
| 601 | - <div class="tab-panel-header"> | 575 | + fallback={<p class="mt-2 text-sm text-muted-foreground">{t("courses.noUpcoming")}</p>} |
| 602 | - <p class="text-sm text-muted-foreground">{countDescription(examCount(), t("courses.examItem"))}</p> | 576 | + > |
| 603 | - <Show when={canManage()}> | 577 | + {(exam) => <ExamLink examId={exam().id} class="mt-2 block text-sm font-medium hover:underline">{exam().title}</ExamLink>} |
| 604 | - <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowExamForm(true)}> | ||
| 605 | - <IconPlus class="h-4 w-4" /> | ||
| 606 | - {t("courses.addExam")} | ||
| 607 | - </Button> | ||
| 608 | </Show> | 578 | </Show> |
| 609 | </div> | 579 | </div> |
| 610 | - <Suspense fallback={<DataTableSkeleton />}> | ||
| 611 | - <DataTable columns={examColumns()} data={exams() ?? []} filterColumn="title" enablePagination pageSize={10} empty={t("exams.empty")} onRowClick={(exam) => void navigate({ to: "/exams/$id", params: { id: exam.id } })} /> | ||
| 612 | - </Suspense> | ||
| 613 | </TabsContent> | 580 | </TabsContent> |
| 614 | 581 | ||
| 615 | - <TabsContent value="homework" forceMount class="space-y-4"> | 582 | + <TabsContent value="work" class="space-y-5"> |
| 616 | - <div class="tab-panel-header"> | 583 | + <div class="space-y-4"> |
| 617 | - <p class="text-sm text-muted-foreground">{countDescription(homeworkCount(), t("homework.item"))}</p> | 584 | + <div class="tab-panel-header"> |
| 618 | - <Show when={canManage()}> | 585 | + <p class="text-sm font-medium">{t("subjects.title")} · {countDescription(subjectCount(), t("subjects.item"))}</p> |
| 619 | - <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowHomeworkForm(true)}> | 586 | + <Show when={canManage()}><Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowSubjectForm(true)}><IconPlus class="h-4 w-4" />{t("subjects.add")}</Button></Show> |
| 620 | - <IconPlus class="h-4 w-4" /> | 587 | + </div> |
| 621 | - {t("homework.add")} | 588 | + <CourseSubjectsPanel courseId={id()} canManage={canManage()} active={courseTab() === "work"} createOpen={showSubjectForm()} onCreateOpenChange={setShowSubjectForm} onCountChange={setSubjectCount} /> |
| 622 | - </Button> | 589 | + </div> |
| 623 | - </Show> | 590 | + <div class="space-y-4 border-t border-border/60 pt-5"> |
| 591 | + <div class="tab-panel-header"> | ||
| 592 | + <p class="text-sm font-medium">{t("courses.exams")} · {countDescription(examCount(), t("courses.examItem"))}</p> | ||
| 593 | + <Show when={canManage()}><Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowExamForm(true)}><IconPlus class="h-4 w-4" />{t("courses.addExam")}</Button></Show> | ||
| 594 | + </div> | ||
| 595 | + <Suspense fallback={<DataTableSkeleton />}><DataTable columns={examColumns()} data={exams() ?? []} filterColumn="title" enablePagination pageSize={10} empty={t("exams.empty")} onRowClick={(exam) => void navigate({ to: "/exams/$id", params: { id: exam.id } })} /></Suspense> | ||
| 596 | + </div> | ||
| 597 | + <div class="space-y-4 border-t border-border/60 pt-5"> | ||
| 598 | + <div class="tab-panel-header"> | ||
| 599 | + <p class="text-sm font-medium">{t("homework.title")} · {countDescription(homeworkCount(), t("homework.item"))}</p> | ||
| 600 | + <Show when={canManage()}><Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowHomeworkForm(true)}><IconPlus class="h-4 w-4" />{t("homework.add")}</Button></Show> | ||
| 601 | + </div> | ||
| 602 | + <CourseHomeworkPanel courseId={id()} canManage={canManage()} active={courseTab() === "work"} createOpen={showHomeworkForm()} onCreateOpenChange={setShowHomeworkForm} onCountChange={setHomeworkCount} /> | ||
| 624 | </div> | 603 | </div> |
| 625 | - <CourseHomeworkPanel courseId={id()} canManage={canManage()} active={courseTab() === "homework"} createOpen={showHomeworkForm()} onCreateOpenChange={setShowHomeworkForm} onCountChange={setHomeworkCount} /> | ||
| 626 | </TabsContent> | 604 | </TabsContent> |
| 627 | 605 | ||
| 628 | - <TabsContent value="sessions" forceMount class="space-y-4"> | 606 | + <TabsContent value="sessions" class="space-y-4"> |
| 629 | <div class="tab-panel-header"> | 607 | <div class="tab-panel-header"> |
| 630 | <p class="text-sm text-muted-foreground">{countDescription(sessionCount(), t("sessions.item"))}</p> | 608 | <p class="text-sm text-muted-foreground">{countDescription(sessionCount(), t("sessions.item"))}</p> |
| 631 | <Show when={canManage()}> | 609 | <Show when={canManage()}> |
| @@ -635,27 +613,28 @@ function CourseDetailContent() { | |||
| 635 | </Button> | 613 | </Button> |
| 636 | </Show> | 614 | </Show> |
| 637 | </div> | 615 | </div> |
| 638 | - <CourseSessionsPanel courseId={id()} roster={roster() ?? []} canManage={canManage()} active createOpen={showSessionForm()} onCreateOpenChange={setShowSessionForm} onCountChange={setSessionCount} /> | 616 | + <CourseSessionsPanel courseId={id()} roster={roster() ?? []} canManage={canManage()} active={courseTab() === "sessions"} createOpen={showSessionForm()} onCreateOpenChange={setShowSessionForm} onCountChange={setSessionCount} /> |
| 639 | </TabsContent> | 617 | </TabsContent> |
| 640 | 618 | ||
| 641 | - <Show when={hasCourseManagementRights()}> | 619 | + <TabsContent value="people" class="space-y-5"> |
| 642 | - <TabsContent value="roster" forceMount class="space-y-4"> | 620 | + <div class="space-y-4"> |
| 643 | <div class="tab-panel-header"> | 621 | <div class="tab-panel-header"> |
| 644 | - <p class="text-sm text-muted-foreground">{countDescription(rosterCount(), t("courses.rosterItem"))}</p> | 622 | + <p class="text-sm font-medium">{t("courses.teachers")} · {countDescription((c().teachers ?? []).length + 1, t("courses.teachers"))}</p> |
| 645 | - <Show when={canManage()}> | 623 | + <Show when={canStaffCourse()}><Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowTeacherForm(true)}><IconPlus class="h-4 w-4" />{t("courses.assignTeacher")}</Button></Show> |
| 646 | - <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowEnrollPanel(true)}> | ||
| 647 | - <IconPlus class="h-4 w-4" /> | ||
| 648 | - {t("courses.enroll")} | ||
| 649 | - </Button> | ||
| 650 | - </Show> | ||
| 651 | </div> | 624 | </div> |
| 652 | - <Suspense fallback={<DataTableSkeleton />}> | 625 | + <p class="rounded-xl border border-border/60 bg-muted/20 px-3 py-2 text-sm">{t("common.creator")}: {c().creator.display_name || c().creator.username}</p> |
| 653 | - <Show when={(roster() ?? []).length > 0} fallback={<EmptyState kind="courses" title={t("exams.emptyRoster")} />}> | 626 | + <CourseTeachersPanel courseId={id()} teachers={c().teachers ?? []} canStaff={canStaffCourse()} assignOpen={showTeacherForm()} onAssignOpenChange={setShowTeacherForm} onCourseUpdated={refetchCourse} /> |
| 654 | - <DataTable columns={rosterColumns()} data={roster() ?? []} filterColumn="username" enablePagination pageSize={10} /> | 627 | + </div> |
| 655 | - </Show> | 628 | + <Show when={hasCourseManagementRights()}> |
| 656 | - </Suspense> | 629 | + <div class="space-y-4 border-t border-border/60 pt-5"> |
| 657 | - </TabsContent> | 630 | + <div class="tab-panel-header"> |
| 658 | - </Show> | 631 | + <p class="text-sm font-medium">{t("courses.roster")} · {countDescription(rosterCount(), t("courses.rosterItem"))}</p> |
| 632 | + <Show when={canManage()}><Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setShowEnrollPanel(true)}><IconPlus class="h-4 w-4" />{t("courses.enroll")}</Button></Show> | ||
| 633 | + </div> | ||
| 634 | + <Suspense fallback={<DataTableSkeleton />}><Show when={(roster() ?? []).length > 0} fallback={<EmptyState kind="courses" title={t("exams.emptyRoster")} />}><DataTable columns={rosterColumns()} data={roster() ?? []} filterColumn="username" enablePagination pageSize={10} /></Show></Suspense> | ||
| 635 | + </div> | ||
| 636 | + </Show> | ||
| 637 | + </TabsContent> | ||
| 659 | </Tabs> | 638 | </Tabs> |
| 660 | </div> | 639 | </div> |
| 661 | </Show> | 640 | </Show> |
src/pages/courses-page.test.tsxadded+114 -0
| @@ -0,0 +1,114 @@ | |||
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | ||
| 2 | +import type { JSX } from "solid-js"; | ||
| 3 | +import CoursesPage from "@/pages/courses-page"; | ||
| 4 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 5 | + | ||
| 6 | +const { getCourses, getMyCourses, navigate } = vi.hoisted(() => ({ | ||
| 7 | + getCourses: vi.fn(), | ||
| 8 | + getMyCourses: vi.fn(), | ||
| 9 | + navigate: vi.fn(), | ||
| 10 | +})); | ||
| 11 | + | ||
| 12 | +vi.mock("@tanstack/solid-router", () => ({ | ||
| 13 | + Link: (props: { to: string; children: JSX.Element; class?: string }) => <a href={props.to} class={props.class}>{props.children}</a>, | ||
| 14 | + Navigate: () => null, | ||
| 15 | + useNavigate: () => navigate, | ||
| 16 | + useSearch: () => () => ({ kind: undefined, action: undefined }), | ||
| 17 | +})); | ||
| 18 | +vi.mock("@/stores/auth-context", () => ({ | ||
| 19 | + useAuth: () => ({ | ||
| 20 | + user: () => ({ id: "student-1", username: "student", role: "student" }), | ||
| 21 | + loading: () => false, | ||
| 22 | + error: () => undefined, | ||
| 23 | + }), | ||
| 24 | +})); | ||
| 25 | +vi.mock("@/api/courses", () => ({ | ||
| 26 | + getCourses, | ||
| 27 | + postCourse: vi.fn(), | ||
| 28 | + postCourseTeacher: vi.fn(), | ||
| 29 | +})); | ||
| 30 | +vi.mock("@/api/reports", () => ({ getMyCourses })); | ||
| 31 | +vi.mock("@/api/terms", () => ({ | ||
| 32 | + getTerms: async () => ({ items: [{ id: "term-1", name: "Fall" }], total: 1, limit: 100, offset: 0 }), | ||
| 33 | +})); | ||
| 34 | +vi.mock("@/api/limits", () => ({ getLimits: vi.fn() })); | ||
| 35 | + | ||
| 36 | +afterEach(() => { | ||
| 37 | + cleanup(); | ||
| 38 | + vi.clearAllMocks(); | ||
| 39 | +}); | ||
| 40 | + | ||
| 41 | +test("student class directory uses only enrolled-course data", async () => { | ||
| 42 | + getMyCourses.mockResolvedValue({ | ||
| 43 | + items: [ | ||
| 44 | + { | ||
| 45 | + id: "course-1", | ||
| 46 | + title: "Algebra", | ||
| 47 | + description: "Core mathematics", | ||
| 48 | + kind: "course", | ||
| 49 | + term: "term-1", | ||
| 50 | + capacity: 24, | ||
| 51 | + creator: { id: "teacher-1", username: "teacher", display_name: "Ada Teacher" }, | ||
| 52 | + teachers: [], | ||
| 53 | + }, | ||
| 54 | + { | ||
| 55 | + id: "study-1", | ||
| 56 | + title: "Study Lab", | ||
| 57 | + description: "Weekly review", | ||
| 58 | + kind: "study", | ||
| 59 | + term: null, | ||
| 60 | + capacity: 12, | ||
| 61 | + creator: { id: "teacher-1", username: "teacher", display_name: "Ada Teacher" }, | ||
| 62 | + teachers: [], | ||
| 63 | + }, | ||
| 64 | + { | ||
| 65 | + id: "club-1", | ||
| 66 | + title: "Robotics", | ||
| 67 | + description: "Build robots", | ||
| 68 | + kind: "club", | ||
| 69 | + term: "term-1", | ||
| 70 | + capacity: 18, | ||
| 71 | + creator: { id: "teacher-2", username: "mentor", display_name: "Club Mentor" }, | ||
| 72 | + teachers: [], | ||
| 73 | + }, | ||
| 74 | + ], | ||
| 75 | + total: 3, | ||
| 76 | + limit: 3, | ||
| 77 | + offset: 0, | ||
| 78 | + }); | ||
| 79 | + | ||
| 80 | + render(() => <PreferencesProvider><CoursesPage /></PreferencesProvider>); | ||
| 81 | + | ||
| 82 | + expect(await screen.findByRole("link", { name: /Algebra/ })).toBeTruthy(); | ||
| 83 | + expect(screen.getAllByText("Enrolled")).toHaveLength(3); | ||
| 84 | + expect(getMyCourses).toHaveBeenCalledWith(); | ||
| 85 | + expect(getCourses).not.toHaveBeenCalled(); | ||
| 86 | + | ||
| 87 | + fireEvent.click(screen.getByRole("tab", { name: "Study" })); | ||
| 88 | + expect(await screen.findByRole("link", { name: /Study Lab/ })).toBeTruthy(); | ||
| 89 | + expect(screen.queryByRole("link", { name: /Algebra/ })).toBeNull(); | ||
| 90 | + expect(getMyCourses).toHaveBeenCalledTimes(1); | ||
| 91 | + expect(navigate).toHaveBeenCalledWith({ | ||
| 92 | + to: "/courses", | ||
| 93 | + search: { action: undefined, kind: "study" }, | ||
| 94 | + replace: true, | ||
| 95 | + }); | ||
| 96 | + | ||
| 97 | + fireEvent.click(screen.getByRole("tab", { name: "Club" })); | ||
| 98 | + expect(await screen.findByRole("link", { name: /Robotics/ })).toBeTruthy(); | ||
| 99 | + expect(screen.queryByRole("link", { name: /Study Lab/ })).toBeNull(); | ||
| 100 | + | ||
| 101 | + fireEvent.click(screen.getByRole("tab", { name: "Classes" })); | ||
| 102 | + expect(await screen.findByRole("link", { name: /Algebra/ })).toBeTruthy(); | ||
| 103 | + expect(screen.queryByRole("link", { name: /Robotics/ })).toBeNull(); | ||
| 104 | + | ||
| 105 | + fireEvent.click(screen.getByRole("tab", { name: "All" })); | ||
| 106 | + fireEvent.input(screen.getByPlaceholderText("Search…"), { target: { value: "robot" } }); | ||
| 107 | + await waitFor(() => expect(screen.getByRole("link", { name: /Robotics/ })).toBeTruthy()); | ||
| 108 | + expect(screen.queryByRole("link", { name: /Algebra/ })).toBeNull(); | ||
| 109 | + | ||
| 110 | + fireEvent.input(screen.getByPlaceholderText("Search…"), { target: { value: "" } }); | ||
| 111 | + fireEvent.change(screen.getByRole("combobox", { name: "Term" }), { target: { value: "unassigned" } }); | ||
| 112 | + expect(await screen.findByRole("link", { name: /Study Lab/ })).toBeTruthy(); | ||
| 113 | + expect(screen.queryByRole("link", { name: /Robotics/ })).toBeNull(); | ||
| 114 | +}); | ||
src/pages/courses-page.tsxmodified+155 -208
| @@ -1,170 +1,113 @@ | |||
| 1 | import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; | 1 | import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; |
| 2 | -import type { ColumnDef } from "@tanstack/solid-table"; | 2 | +import { useNavigate, useSearch } from "@tanstack/solid-router"; |
| 3 | -import { useLocation, useNavigate } from "@tanstack/solid-router"; | ||
| 4 | import { getCourses, postCourse, postCourseTeacher } from "@/api/courses"; | 3 | import { getCourses, postCourse, postCourseTeacher } from "@/api/courses"; |
| 5 | import { getMyCourses } from "@/api/reports"; | 4 | import { getMyCourses } from "@/api/reports"; |
| 6 | import { getTerms } from "@/api/terms"; | 5 | import { getTerms } from "@/api/terms"; |
| 7 | -import { formatApiError } from "@/api/client"; | 6 | +import { getLimits } from "@/api/limits"; |
| 8 | -import type { Course, CourseKind } from "@/api/client"; | 7 | +import { formatApiError, type CourseKind } from "@/api/client"; |
| 9 | -import { Alert } from "@/components/ui/alert"; | 8 | +import { CourseCard } from "@/components/courses/course-card"; |
| 10 | import { RouteGuard } from "@/components/layout/route-guard"; | 9 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 11 | -import { Badge } from "@/components/ui/badge"; | 10 | +import { Alert } from "@/components/ui/alert"; |
| 12 | import { Button } from "@/components/ui/button"; | 11 | import { Button } from "@/components/ui/button"; |
| 13 | -import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; | 12 | +import { DataToolbar } from "@/components/ui/data-toolbar"; |
| 14 | -import { IconEye, IconPlus } from "@/components/ui/icons"; | 13 | +import { EmptyState } from "@/components/ui/empty-state"; |
| 14 | +import { ErrorAlert } from "@/components/ui/error-alert"; | ||
| 15 | +import { IconPlus } from "@/components/ui/icons"; | ||
| 15 | import { Input } from "@/components/ui/input"; | 16 | import { Input } from "@/components/ui/input"; |
| 16 | import { Label } from "@/components/ui/label"; | 17 | import { Label } from "@/components/ui/label"; |
| 17 | -import { DropdownSelect, Select } from "@/components/ui/select"; | 18 | +import { PageSpinner } from "@/components/ui/page-spinner"; |
| 19 | +import { PaginationControls } from "@/components/ui/pagination-controls"; | ||
| 20 | +import { Select } from "@/components/ui/select"; | ||
| 18 | import { SidePanel } from "@/components/ui/side-panel"; | 21 | import { SidePanel } from "@/components/ui/side-panel"; |
| 19 | -import { TableRowActions } from "@/components/ui/table-row-actions"; | 22 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 20 | import { Textarea } from "@/components/ui/textarea"; | 23 | import { Textarea } from "@/components/ui/textarea"; |
| 21 | import { UserSearchSelect } from "@/components/users/user-search-select"; | 24 | import { UserSearchSelect } from "@/components/users/user-search-select"; |
| 22 | import { createFlash } from "@/lib/flash"; | 25 | import { createFlash } from "@/lib/flash"; |
| 23 | import { personLabel } from "@/lib/person"; | 26 | import { personLabel } from "@/lib/person"; |
| 27 | +import { hasMinRole } from "@/lib/roles"; | ||
| 24 | import { useAuth } from "@/stores/auth-context"; | 28 | import { useAuth } from "@/stores/auth-context"; |
| 25 | import { useT } from "@/stores/preferences-context"; | 29 | import { useT } from "@/stores/preferences-context"; |
| 26 | -import { hasMinRole } from "@/lib/roles"; | ||
| 27 | 30 | ||
| 28 | -const COURSE_PAGE_SIZE = 12; | 31 | +const PAGE_SIZE = 12; |
| 29 | 32 | ||
| 30 | export default function CoursesPage() { | 33 | export default function CoursesPage() { |
| 31 | - return ( | 34 | + return <RouteGuard><CoursesContent /></RouteGuard>; |
| 32 | - <RouteGuard> | ||
| 33 | - <CoursesContent /> | ||
| 34 | - </RouteGuard> | ||
| 35 | - ); | ||
| 36 | } | 35 | } |
| 37 | 36 | ||
| 38 | function CoursesContent() { | 37 | function CoursesContent() { |
| 39 | const auth = useAuth(); | 38 | const auth = useAuth(); |
| 40 | const navigate = useNavigate(); | 39 | const navigate = useNavigate(); |
| 41 | - const location = useLocation(); | 40 | + const routeSearch = useSearch({ from: "/courses" }); |
| 42 | const t = useT(); | 41 | const t = useT(); |
| 43 | - const [showForm, setShowForm] = createSignal(location().searchStr.includes("action=new")); | 42 | + const [pageKind, setPageKind] = createSignal<CourseKind | undefined>(routeSearch().kind); |
| 44 | - createEffect(() => { | 43 | + const createKind = (): CourseKind => pageKind() ?? "course"; |
| 45 | - if (location().searchStr.includes("action=new")) { | 44 | + const pageLabel = () => t("nav.classes"); |
| 46 | - setShowForm(true); | 45 | + const kindLabel = () => createKind() === "study" ? t("courses.kind.study") : createKind() === "club" ? t("courses.kind.club") : t("courses.kind.course"); |
| 47 | - } | 46 | + const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); |
| 48 | - }); | 47 | + const [showForm, setShowForm] = createSignal(routeSearch().action === "new"); |
| 49 | const [title, setTitle] = createSignal(""); | 48 | const [title, setTitle] = createSignal(""); |
| 50 | const [description, setDescription] = createSignal(""); | 49 | const [description, setDescription] = createSignal(""); |
| 51 | const [termId, setTermId] = createSignal(""); | 50 | const [termId, setTermId] = createSignal(""); |
| 52 | const [capacity, setCapacity] = createSignal(""); | 51 | const [capacity, setCapacity] = createSignal(""); |
| 53 | const [teacherId, setTeacherId] = createSignal(""); | 52 | const [teacherId, setTeacherId] = createSignal(""); |
| 53 | + const [termFilter, setTermFilter] = createSignal("all"); | ||
| 54 | + const [page, setPage] = createSignal(0); | ||
| 55 | + const [search, setSearch] = createSignal(""); | ||
| 54 | const [error, setError] = createSignal(""); | 56 | const [error, setError] = createSignal(""); |
| 55 | - const [flash, setFlash] = createFlash(); | ||
| 56 | const [pending, setPending] = createSignal(false); | 57 | const [pending, setPending] = createSignal(false); |
| 57 | - const [termFilter, setTermFilter] = createSignal("all"); | 58 | + const [flash, setFlash] = createFlash(); |
| 58 | 59 | ||
| 59 | - const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); | 60 | + createEffect(() => { |
| 60 | - const isStudent = () => auth.user()?.role === "student"; | 61 | + routeSearch().action === "new" && setShowForm(true); |
| 62 | + }); | ||
| 63 | + createEffect(() => { | ||
| 64 | + setPageKind(routeSearch().kind); | ||
| 65 | + }); | ||
| 66 | + createEffect(() => { | ||
| 67 | + pageKind(); termFilter(); search(); setPage(0); | ||
| 68 | + }); | ||
| 61 | 69 | ||
| 62 | const [terms] = createResource(async () => (await getTerms({ limit: 100 })).items); | 70 | const [terms] = createResource(async () => (await getTerms({ limit: 100 })).items); |
| 63 | - | 71 | + const [limits, { refetch: refetchLimits }] = createResource(() => canCreate() ? getLimits() : null); |
| 64 | - const termName = (id: string | null | undefined) => terms()?.find((term) => term.id === id)?.name ?? t("terms.unassigned"); | ||
| 65 | - const courseKindLabel = (value: CourseKind | undefined) => | ||
| 66 | - value === "study" ? t("courses.kind.study") : value === "club" ? t("courses.kind.club") : t("courses.kind.course"); | ||
| 67 | - const pageKind = (): CourseKind => location().pathname.startsWith("/studies") ? "study" : location().pathname.startsWith("/clubs") ? "club" : "course"; | ||
| 68 | - const pageLabel = () => courseKindLabel(pageKind()); | ||
| 69 | - const filterCourses = (items: Course[]) => { | ||
| 70 | - const selectedTerm = termFilter(); | ||
| 71 | - return items.filter((course) => { | ||
| 72 | - if (selectedTerm === "unassigned" && course.term) return false; | ||
| 73 | - if (selectedTerm !== "all" && selectedTerm !== "unassigned" && course.term !== selectedTerm) return false; | ||
| 74 | - return course.kind === pageKind(); | ||
| 75 | - }); | ||
| 76 | - }; | ||
| 77 | - const searchCourse = (course: Course, query: string) => | ||
| 78 | - [course.title, course.description, personLabel(course.creator), course.creator.username, termName(course.term), courseKindLabel(course.kind)] | ||
| 79 | - .join(" ") | ||
| 80 | - .toLocaleLowerCase() | ||
| 81 | - .includes(query.toLocaleLowerCase()); | ||
| 82 | - | ||
| 83 | const [list, { refetch }] = createResource( | 72 | const [list, { refetch }] = createResource( |
| 84 | () => auth.user()?.role ?? null, | 73 | () => auth.user()?.role ?? null, |
| 85 | - async (role) => (role === "student" ? (await getMyCourses({ limit: 100 })).items : (await getCourses({ limit: 100 })).items), | 74 | + async (role) => role === "student" ? getMyCourses() : getCourses(), |
| 86 | ); | 75 | ); |
| 76 | + const listData = () => list.latest ?? list(); | ||
| 77 | + const termName = (id: string | null) => terms.latest?.find((term) => term.id === id)?.name ?? (id || t("terms.unassigned")); | ||
| 78 | + const filteredCourses = createMemo(() => { | ||
| 79 | + const query = search().trim().toLocaleLowerCase(); | ||
| 80 | + return (listData()?.items ?? []).filter((course) => { | ||
| 81 | + if (pageKind() && course.kind !== pageKind()) return false; | ||
| 82 | + if (termFilter() === "unassigned" && course.term) return false; | ||
| 83 | + if (termFilter() !== "all" && termFilter() !== "unassigned" && course.term !== termFilter()) return false; | ||
| 84 | + if (!query) return true; | ||
| 85 | + return [ | ||
| 86 | + course.title, | ||
| 87 | + course.description, | ||
| 88 | + personLabel(course.creator), | ||
| 89 | + ...(course.teachers ?? []).map(personLabel), | ||
| 90 | + termName(course.term), | ||
| 91 | + ].join(" ").toLocaleLowerCase().includes(query); | ||
| 92 | + }); | ||
| 93 | + }); | ||
| 94 | + const totalPages = createMemo(() => Math.max(1, Math.ceil(filteredCourses().length / PAGE_SIZE))); | ||
| 95 | + const visibleCourses = createMemo(() => filteredCourses().slice(page() * PAGE_SIZE, (page() + 1) * PAGE_SIZE)); | ||
| 87 | 96 | ||
| 88 | - const rows = () => filterCourses(list() ?? []); | 97 | + const createCourse = async (event: SubmitEvent) => { |
| 89 | - const columns = createMemo<ColumnDef<Course>[]>(() => [ | 98 | + event.preventDefault(); |
| 90 | - { | ||
| 91 | - accessorKey: "title", | ||
| 92 | - header: pageLabel(), | ||
| 93 | - cell: (cell) => ( | ||
| 94 | - <div class="min-w-0 space-y-1"> | ||
| 95 | - <p class="truncate font-medium">{cell.row.original.title}</p> | ||
| 96 | - <Show when={isStudent()}> | ||
| 97 | - <Badge variant="secondary" class="rounded-full">{t("courses.enrolled")}</Badge> | ||
| 98 | - </Show> | ||
| 99 | - </div> | ||
| 100 | - ), | ||
| 101 | - }, | ||
| 102 | - { | ||
| 103 | - accessorKey: "description", | ||
| 104 | - header: t("form.description"), | ||
| 105 | - meta: { cellClass: "truncate text-muted-foreground" }, | ||
| 106 | - cell: (cell) => cell.row.original.description || "—", | ||
| 107 | - }, | ||
| 108 | - { | ||
| 109 | - id: "term", | ||
| 110 | - accessorFn: (course) => termName(course.term), | ||
| 111 | - header: t("terms.term"), | ||
| 112 | - cell: (cell) => <Badge variant="outline" class="mono max-w-full rounded-full text-[11px]"><span class="truncate">{termName(cell.row.original.term)}</span></Badge>, | ||
| 113 | - }, | ||
| 114 | - { | ||
| 115 | - id: "creator", | ||
| 116 | - accessorFn: (course) => personLabel(course.creator), | ||
| 117 | - header: t("common.creator"), | ||
| 118 | - meta: { cellClass: "truncate text-muted-foreground" }, | ||
| 119 | - cell: (cell) => personLabel(cell.row.original.creator), | ||
| 120 | - }, | ||
| 121 | - { | ||
| 122 | - id: "capacity", | ||
| 123 | - accessorFn: (course) => course.capacity, | ||
| 124 | - header: t("courses.capacity"), | ||
| 125 | - meta: { headerClass: "text-right", cellClass: "text-right tabular-nums text-muted-foreground" }, | ||
| 126 | - cell: (cell) => cell.row.original.capacity ?? "—", | ||
| 127 | - }, | ||
| 128 | - { | ||
| 129 | - id: "actions", | ||
| 130 | - header: t("common.actions"), | ||
| 131 | - meta: { headerClass: "text-center", cellClass: "px-1 text-center" }, | ||
| 132 | - cell: (cell) => ( | ||
| 133 | - <TableRowActions | ||
| 134 | - label={t("common.actions")} | ||
| 135 | - actions={[{ | ||
| 136 | - label: t("common.view"), | ||
| 137 | - icon: <IconEye class="h-4 w-4" />, | ||
| 138 | - onSelect: () => void navigate({ to: "/courses/$id", params: { id: cell.row.original.id } }), | ||
| 139 | - }]} | ||
| 140 | - /> | ||
| 141 | - ), | ||
| 142 | - }, | ||
| 143 | - ]); | ||
| 144 | - | ||
| 145 | - const onCreate = async (e: SubmitEvent) => { | ||
| 146 | - e.preventDefault(); | ||
| 147 | setError(""); | 99 | setError(""); |
| 148 | setPending(true); | 100 | setPending(true); |
| 149 | try { | 101 | try { |
| 150 | - const cap = capacity().trim(); | 102 | + const course = await postCourse({ |
| 151 | - const newCourse = await postCourse({ | ||
| 152 | title: title().trim(), | 103 | title: title().trim(), |
| 153 | description: description().trim() || undefined, | 104 | description: description().trim() || undefined, |
| 154 | - kind: pageKind(), | 105 | + kind: createKind(), |
| 155 | term_id: termId() || null, | 106 | term_id: termId() || null, |
| 156 | - capacity: cap ? Number(cap) : null, | 107 | + capacity: capacity().trim() ? Number(capacity()) : null, |
| 157 | }); | 108 | }); |
| 158 | - const tid = teacherId(); | 109 | + if (teacherId() && hasMinRole(auth.user()?.role, "manager")) await postCourseTeacher(course.id, teacherId()); |
| 159 | - if (tid && hasMinRole(auth.user()?.role, "manager")) { | 110 | + setTitle(""); setDescription(""); setTermId(""); setCapacity(""); setTeacherId(""); setShowForm(false); |
| 160 | - await postCourseTeacher(newCourse.id, tid); | ||
| 161 | - } | ||
| 162 | - setTitle(""); | ||
| 163 | - setDescription(""); | ||
| 164 | - setTermId(""); | ||
| 165 | - setCapacity(""); | ||
| 166 | - setTeacherId(""); | ||
| 167 | - setShowForm(false); | ||
| 168 | await refetch(); | 111 | await refetch(); |
| 169 | setFlash(t("common.created")); | 112 | setFlash(t("common.created")); |
| 170 | } catch (err) { | 113 | } catch (err) { |
| @@ -175,100 +118,104 @@ function CoursesContent() { | |||
| 175 | }; | 118 | }; |
| 176 | 119 | ||
| 177 | return ( | 120 | return ( |
| 178 | - <div class="space-y-6"> | 121 | + <div class="space-y-5"> |
| 179 | - <SidePanel open={canCreate() && showForm()} onOpenChange={setShowForm} title={t("common.createItem", { item: pageLabel() })} description={t("courses.subtitle", { item: pageLabel() })}> | 122 | + <SidePanel open={canCreate() && showForm()} onOpenChange={setShowForm} title={t("common.createItem", { item: kindLabel() })} description={t("courses.subtitle", { item: kindLabel() })}> |
| 180 | - <form class="space-y-4" onSubmit={onCreate}> | 123 | + <form class="space-y-4" onSubmit={createCourse}> |
| 181 | - <div class="space-y-3 rounded-2xl border border-sky-500/15 bg-sky-500/3 p-4 shadow-xs"> | 124 | + <Show when={limits.error}><ErrorAlert message={formatApiError(limits.error)} onRetry={() => void refetchLimits()} /></Show> |
| 182 | - <div class="space-y-1.5"> | 125 | + <div class="space-y-3"> |
| 183 | - <Label for="course-title">{t("form.title")}</Label> | 126 | + <div class="space-y-1.5"><Label for="course-title">{t("form.title")}</Label><Input id="course-title" required maxlength={limits.latest?.course.max_title_len} value={title()} onInput={(e) => setTitle(e.currentTarget.value)} /></div> |
| 184 | - <Input id="course-title" required maxlength={200} value={title()} onInput={(e) => setTitle(e.currentTarget.value)} /> | 127 | + <div class="space-y-1.5"><Label for="course-description">{t("form.description")}</Label><Textarea id="course-description" maxlength={limits.latest?.course.max_description_len} rows={3} value={description()} onInput={(e) => setDescription(e.currentTarget.value)} /></div> |
| 185 | - </div> | 128 | + <div class="space-y-1.5"><Label for="course-term">{t("terms.term")}</Label><Select id="course-term" value={termId()} onChange={(e) => setTermId(e.currentTarget.value)}><option value="">{t("terms.unassigned")}</option><For each={terms.latest ?? []}>{(term) => <option value={term.id}>{term.name}</option>}</For></Select></div> |
| 186 | - <div class="space-y-1.5"> | 129 | + <div class="space-y-1.5"><Label for="course-capacity">{t("courses.capacity")}</Label><Input id="course-capacity" type="number" min={1} value={capacity()} onInput={(e) => setCapacity(e.currentTarget.value)} /></div> |
| 187 | - <Label for="course-desc">{t("form.description")}</Label> | 130 | + <Show when={hasMinRole(auth.user()?.role, "manager")}><UserSearchSelect id="course-teacher" role="teacher" value={teacherId()} onChange={setTeacherId} placeholder={t("courses.assignTeacher")} label={t("courses.teachers")} /></Show> |
| 188 | - <Textarea id="course-desc" maxlength={2000} rows={3} value={description()} onInput={(e) => setDescription(e.currentTarget.value)} /> | ||
| 189 | - </div> | ||
| 190 | - </div> | ||
| 191 | - <div class="space-y-3 rounded-2xl border border-violet-500/15 bg-violet-500/3 p-4 shadow-xs"> | ||
| 192 | - <div class="space-y-1.5"> | ||
| 193 | - <Label for="course-term">{t("terms.term")}</Label> | ||
| 194 | - <Select id="course-term" value={termId()} onChange={(e) => setTermId(e.currentTarget.value)}> | ||
| 195 | - <option value="">{t("terms.unassigned")}</option> | ||
| 196 | - <For each={terms() ?? []}>{(term) => <option value={term.id}>{term.name}</option>}</For> | ||
| 197 | - </Select> | ||
| 198 | - </div> | ||
| 199 | - <div class="space-y-1.5"> | ||
| 200 | - <Label for="course-capacity">{t("courses.capacity")}</Label> | ||
| 201 | - <Input id="course-capacity" type="number" min={1} value={capacity()} placeholder={t("courses.capacityOptional")} onInput={(e) => setCapacity(e.currentTarget.value)} /> | ||
| 202 | - </div> | ||
| 203 | - <Show when={hasMinRole(auth.user()?.role, "manager")}> | ||
| 204 | - <div class="space-y-1.5"> | ||
| 205 | - <UserSearchSelect | ||
| 206 | - id="course-create-teacher" | ||
| 207 | - role="teacher" | ||
| 208 | - value={teacherId()} | ||
| 209 | - onChange={setTeacherId} | ||
| 210 | - placeholder={t("courses.assignTeacher")} | ||
| 211 | - label={t("courses.teachers")} | ||
| 212 | - /> | ||
| 213 | - </div> | ||
| 214 | - </Show> | ||
| 215 | - </div> | ||
| 216 | - {error() && <p class="text-sm text-destructive">{error()}</p>} | ||
| 217 | - <div class="sticky bottom-0 -mx-5 flex flex-wrap gap-2 border-t border-border bg-background px-5 pb-6 pt-4 sm:-mx-6 sm:px-6 sm:pb-6"> | ||
| 218 | - <Button type="submit" class="flex-1 rounded-xl sm:flex-none" disabled={pending()}> | ||
| 219 | - {t("common.create")} | ||
| 220 | - </Button> | ||
| 221 | - <Button type="button" variant="outline" class="flex-1 rounded-xl sm:flex-none" onClick={() => setShowForm(false)}> | ||
| 222 | - {t("common.cancel")} | ||
| 223 | - </Button> | ||
| 224 | </div> | 131 | </div> |
| 132 | + <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> | ||
| 133 | + <div class="flex gap-2 border-t pt-4"><Button type="submit" disabled={pending()}>{t("common.create")}</Button><Button type="button" variant="outline" onClick={() => setShowForm(false)}>{t("common.cancel")}</Button></div> | ||
| 225 | </form> | 134 | </form> |
| 226 | </SidePanel> | 135 | </SidePanel> |
| 227 | 136 | ||
| 228 | - <section class="data-shell space-y-4 border-sky-500/15 bg-sky-500/2.5 p-4"> | 137 | + <header class="flex flex-wrap items-end justify-between gap-4 border-b border-border pb-5"> |
| 229 | - <Show when={flash()}> | 138 | + <div class="space-y-1"> |
| 230 | - <Alert variant="success">{flash()}</Alert> | 139 | + <h1 class="font-display text-2xl font-semibold tracking-tight">{pageLabel()}</h1> |
| 140 | + <p class="text-sm text-muted-foreground">{t("courses.pageSubtitle")}</p> | ||
| 141 | + </div> | ||
| 142 | + <Show when={canCreate()}> | ||
| 143 | + <Button size="sm" class="min-w-30 rounded-lg" onClick={() => setShowForm(true)}> | ||
| 144 | + <IconPlus class="h-4 w-4" /> | ||
| 145 | + {t("common.createItem", { item: kindLabel() })} | ||
| 146 | + </Button> | ||
| 231 | </Show> | 147 | </Show> |
| 232 | - <Suspense fallback={<DataTableSkeleton columns={5} rows={8} />}> | 148 | + </header> |
| 233 | - <Show when={list.error}> | 149 | + |
| 234 | - <Alert variant="destructive">{formatApiError(list.error)}</Alert> | 150 | + <Show when={flash()}><Alert variant="success">{flash()}</Alert></Show> |
| 235 | - </Show> | 151 | + |
| 236 | - <div class="space-y-4"> | 152 | + <Tabs |
| 237 | - <DataTable | 153 | + value={pageKind() ?? "all"} |
| 238 | - title={pageLabel()} | 154 | + onChange={(value) => { |
| 239 | - description={t("courses.subtitle", { item: pageLabel() })} | 155 | + const kind = value === "course" || value === "study" || value === "club" ? value : undefined; |
| 240 | - actions={ | 156 | + setPageKind(kind); |
| 241 | - canCreate() ? ( | 157 | + void navigate({ |
| 242 | - <Button type="button" size="sm" class="min-w-30" onClick={() => setShowForm(true)}> | 158 | + to: "/courses", |
| 243 | - <IconPlus class="h-4 w-4" /> | 159 | + search: { action: undefined, kind }, |
| 244 | - {t("common.createItem", { item: pageLabel() })} | 160 | + replace: true, |
| 245 | - </Button> | 161 | + }); |
| 246 | - ) : undefined | 162 | + }} |
| 247 | - } | 163 | + > |
| 248 | - columns={columns()} | 164 | + <TabsList aria-label={t("nav.classes")}> |
| 249 | - data={rows()} | 165 | + <TabsTrigger value="all">{t("common.all")}</TabsTrigger> |
| 250 | - tableClass="table-fixed min-w-176" | 166 | + <TabsTrigger value="course">{t("courses.kind.course")}</TabsTrigger> |
| 251 | - searchPredicate={searchCourse} | 167 | + <TabsTrigger value="study">{t("courses.kind.study")}</TabsTrigger> |
| 252 | - enablePagination | 168 | + <TabsTrigger value="club">{t("courses.kind.club")}</TabsTrigger> |
| 253 | - pageSize={COURSE_PAGE_SIZE} | 169 | + </TabsList> |
| 254 | - empty={t("courses.empty", { item: pageLabel() })} | 170 | + |
| 255 | - onRowClick={(course) => void navigate({ to: "/courses/$id", params: { id: course.id } })} | 171 | + <TabsContent value={pageKind() ?? "all"} class="mt-4 space-y-4 border-0 bg-transparent p-0 shadow-none"> |
| 172 | + <section class="rounded-2xl border border-border bg-card p-3 shadow-xs" aria-label={t("common.search")}> | ||
| 173 | + <DataToolbar | ||
| 174 | + inline | ||
| 175 | + searchValue={search()} | ||
| 176 | + searchPlaceholder={t("common.searchPlaceholder")} | ||
| 177 | + onSearchInput={setSearch} | ||
| 256 | filters={ | 178 | filters={ |
| 257 | - <DropdownSelect | 179 | + <Select wrapperClass="w-40 shrink-0 sm:w-52" class="h-11 rounded-xl" aria-label={t("terms.term")} value={termFilter()} onChange={(e) => setTermFilter(e.currentTarget.value)}> |
| 258 | - labelPrefix={t("terms.term")} | 180 | + <option value="all">{t("common.all")}</option> |
| 259 | - value={termFilter()} | 181 | + <option value="unassigned">{t("terms.unassigned")}</option> |
| 260 | - onChange={(val) => setTermFilter(val)} | 182 | + <For each={terms.latest ?? []}>{(term) => <option value={term.id}>{term.name}</option>}</For> |
| 261 | - options={[ | 183 | + </Select> |
| 262 | - { value: "all", label: t("common.all") }, | ||
| 263 | - { value: "unassigned", label: t("terms.unassigned") }, | ||
| 264 | - ...(terms() ?? []).map((term) => ({ value: term.id, label: term.name })), | ||
| 265 | - ]} | ||
| 266 | - /> | ||
| 267 | } | 184 | } |
| 268 | /> | 185 | /> |
| 269 | - </div> | 186 | + </section> |
| 270 | - </Suspense> | 187 | + |
| 271 | - </section> | 188 | + <Suspense fallback={<PageSpinner />}> |
| 189 | + <Show | ||
| 190 | + when={!list.error} | ||
| 191 | + fallback={<ErrorAlert message={formatApiError(list.error)} onRetry={() => void refetch()} />} | ||
| 192 | + > | ||
| 193 | + <Show | ||
| 194 | + when={filteredCourses().length > 0} | ||
| 195 | + fallback={<EmptyState kind="courses" title={t("courses.empty", { item: kindLabel() })} />} | ||
| 196 | + > | ||
| 197 | + <div class={list.loading ? "space-y-2 opacity-60 transition-opacity" : "space-y-2 transition-opacity"}> | ||
| 198 | + <For each={visibleCourses()}> | ||
| 199 | + {(course) => ( | ||
| 200 | + <CourseCard | ||
| 201 | + course={course} | ||
| 202 | + term={termName(course.term)} | ||
| 203 | + enrolled={auth.user()?.role === "student"} | ||
| 204 | + labels={{ | ||
| 205 | + capacity: t("courses.capacity"), | ||
| 206 | + enrolled: t("courses.enrolled"), | ||
| 207 | + kind: course.kind === "study" ? t("courses.kind.study") : course.kind === "club" ? t("courses.kind.club") : t("courses.kind.course"), | ||
| 208 | + }} | ||
| 209 | + /> | ||
| 210 | + )} | ||
| 211 | + </For> | ||
| 212 | + </div> | ||
| 213 | + <PaginationControls page={page()} totalPages={totalPages()} onPageChange={setPage} /> | ||
| 214 | + </Show> | ||
| 215 | + </Show> | ||
| 216 | + </Suspense> | ||
| 217 | + </TabsContent> | ||
| 218 | + </Tabs> | ||
| 272 | </div> | 219 | </div> |
| 273 | ); | 220 | ); |
| 274 | } | 221 | } |