🧩 failures stop reading as empty data
Browse files at this commit391e2382bca889b2d99ea466725dcfa1bac32950Parent: e0d37b1- fix
- 🧩 failures stop reading as empty data
9 changed files +264 -36
- src/components/ui/data-table.test.tsx +22 -1
- src/components/ui/data-table.tsx +18 -4
- src/i18n/messages.ts +9 -0
- src/lib/board-resources.test.tsx +36 -0
- src/lib/board-resources.ts +30 -0
- src/pages/calendar-page.test.tsx +62 -0
- src/pages/calendar-page.tsx +36 -14
- src/pages/dashboard-page.test.tsx +23 -2
- src/pages/dashboard-page.tsx +28 -15
src/components/ui/data-table.test.tsxmodified+22 -1
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { fireEvent, render, screen } from "@solidjs/testing-library"; | |
| 1 | +import { fireEvent, render, screen, within } from "@solidjs/testing-library"; | |
| 2 | 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | 3 | import { DataTable } from "@/components/ui/data-table"; |
| 4 | 4 | import { DataSection } from "@/components/ui/data-section"; |
| @@ -149,3 +149,24 @@ test("the header action toolbar of every shared host can shrink below its max-co | ||
| 149 | 149 | expect(toolbar.classList).not.toContain("shrink-0"); |
| 150 | 150 | } |
| 151 | 151 | }); |
| 152 | + | |
| 153 | +test("a search that matches nothing says so and clears back to the list", () => { | |
| 154 | + render(() => ( | |
| 155 | + <PreferencesProvider> | |
| 156 | + <DataTable | |
| 157 | + columns={columns} | |
| 158 | + data={[{ name: "Ada" }]} | |
| 159 | + empty="No people yet." | |
| 160 | + enableColumnVisibility={false} | |
| 161 | + searchPredicate={(row, query) => row.name.toLowerCase().includes(query.toLowerCase())} | |
| 162 | + /> | |
| 163 | + </PreferencesProvider> | |
| 164 | + )); | |
| 165 | + | |
| 166 | + fireEvent.input(screen.getByRole("textbox"), { target: { value: "zzz" } }); | |
| 167 | + expect(screen.queryByText("No people yet.")).toBeNull(); | |
| 168 | + expect(screen.getByText(/Nothing matches|eşleşen kayıt yok/)).toBeTruthy(); | |
| 169 | + | |
| 170 | + fireEvent.click(within(screen.getByRole("table")).getByRole("button", { name: /Clear search|Aramayı temizle/ })); | |
| 171 | + expect(screen.getByText("Ada")).toBeTruthy(); | |
| 172 | +}); |
src/components/ui/data-table.tsxmodified+18 -4
| @@ -332,10 +332,24 @@ export function DataTable<TData, TValue = unknown>(props: DataTableProps<TData, | ||
| 332 | 332 | fallback={ |
| 333 | 333 | <TableRow> |
| 334 | 334 | <TableCell colSpan={colSpan()} class="py-8 text-center text-muted-foreground"> |
| 335 | - <div class="flex flex-col items-center gap-3"> | |
| 336 | - <Illustration name={searchValue().trim() ? "no-results" : (props.emptyIllustration ?? "empty")} class="h-20 w-32" /> | |
| 337 | - <span>{props.empty ?? t("common.noResults")}</span> | |
| 338 | - </div> | |
| 335 | + {/* A search that matches nothing is not an empty list: say so and offer the way back. */} | |
| 336 | + <Show | |
| 337 | + when={searchFieldValue().trim()} | |
| 338 | + fallback={ | |
| 339 | + <div class="flex flex-col items-center gap-3"> | |
| 340 | + <Illustration name={props.emptyIllustration ?? "empty"} class="h-20 w-32" /> | |
| 341 | + <span>{props.empty ?? t("common.noResults")}</span> | |
| 342 | + </div> | |
| 343 | + } | |
| 344 | + > | |
| 345 | + <div class="flex flex-col items-center gap-3"> | |
| 346 | + <Illustration name="no-results" class="h-20 w-32" /> | |
| 347 | + <span>{t("common.noMatches")}</span> | |
| 348 | + <Button type="button" size="sm" variant="outline" onClick={() => handleSearch("")}> | |
| 349 | + {t("common.clearSearch")} | |
| 350 | + </Button> | |
| 351 | + </div> | |
| 352 | + </Show> | |
| 339 | 353 | </TableCell> |
| 340 | 354 | </TableRow> |
| 341 | 355 | } |
src/i18n/messages.tsmodified+9 -0
| @@ -606,6 +606,7 @@ export type MessageKey = | ||
| 606 | 606 | | "common.next" |
| 607 | 607 | | "common.pageOf" |
| 608 | 608 | | "common.noResults" |
| 609 | + | "common.noMatches" | |
| 609 | 610 | | "common.creator" |
| 610 | 611 | | "common.searchPlaceholder" |
| 611 | 612 | | "common.all" |
| @@ -615,6 +616,7 @@ export type MessageKey = | ||
| 615 | 616 | | "common.deleted" |
| 616 | 617 | | "common.saved" |
| 617 | 618 | | "common.loadAll" |
| 619 | + | "dashboard.partialError" | |
| 618 | 620 | | "common.breadcrumb" |
| 619 | 621 | | "common.truncatedNotice" |
| 620 | 622 | | "common.createItem" |
| @@ -1978,6 +1980,7 @@ export type MessageKey = | ||
| 1978 | 1980 | | "messages.emptyTrashSummary" |
| 1979 | 1981 | | "messages.emptyTrashPageSummary" |
| 1980 | 1982 | | "calendar.title" |
| 1983 | + | "calendar.feedsFailed" | |
| 1981 | 1984 | | "calendar.today" |
| 1982 | 1985 | | "calendar.noEvents" |
| 1983 | 1986 | | "calendar.events" |
| @@ -2738,6 +2741,7 @@ const en: Dict = { | ||
| 2738 | 2741 | "common.next": "Next", |
| 2739 | 2742 | "common.pageOf": "{page} / {total}", |
| 2740 | 2743 | "common.noResults": "No results.", |
| 2744 | + "common.noMatches": "Nothing matches your search.", | |
| 2741 | 2745 | "common.creator": "Created by", |
| 2742 | 2746 | "common.searchPlaceholder": "Search…", |
| 2743 | 2747 | "common.all": "All", |
| @@ -2747,6 +2751,7 @@ const en: Dict = { | ||
| 2747 | 2751 | "common.deleted": "Deleted.", |
| 2748 | 2752 | "common.saved": "Saved successfully.", |
| 2749 | 2753 | "common.loadAll": "Load all", |
| 2754 | + "dashboard.partialError": "Some panels could not load and show no data.", | |
| 2750 | 2755 | "common.breadcrumb": "Breadcrumb", |
| 2751 | 2756 | "common.truncatedNotice": "Showing the first {shown} of {total} records.", |
| 2752 | 2757 | "common.createItem": "Create new {item}", |
| @@ -3195,6 +3200,7 @@ const en: Dict = { | ||
| 3195 | 3200 | "events.helpBody": |
| 3196 | 3201 | "Teachers and managers record attendance for students only. Managers can edit any event.", |
| 3197 | 3202 | "calendar.title": "Calendar", |
| 3203 | + "calendar.feedsFailed": "Could not load: {feeds}. The calendar is missing those items.", | |
| 3198 | 3204 | "calendar.today": "Today", |
| 3199 | 3205 | "calendar.noEvents": "Nothing scheduled on this day.", |
| 3200 | 3206 | "calendar.events": "Events", |
| @@ -4872,6 +4878,7 @@ const tr: Dict = { | ||
| 4872 | 4878 | "common.next": "Sonraki", |
| 4873 | 4879 | "common.pageOf": "{page} / {total}", |
| 4874 | 4880 | "common.noResults": "Sonuç yok.", |
| 4881 | + "common.noMatches": "Aramanızla eşleşen kayıt yok.", | |
| 4875 | 4882 | "common.creator": "Oluşturan", |
| 4876 | 4883 | "common.searchPlaceholder": "Ara…", |
| 4877 | 4884 | "common.all": "Tümü", |
| @@ -4881,6 +4888,7 @@ const tr: Dict = { | ||
| 4881 | 4888 | "common.deleted": "Silindi.", |
| 4882 | 4889 | "common.saved": "Başarıyla kaydedildi.", |
| 4883 | 4890 | "common.loadAll": "Tümünü yükle", |
| 4891 | + "dashboard.partialError": "Bazı bölümler yüklenemedi ve veri göstermiyor.", | |
| 4884 | 4892 | "common.breadcrumb": "Sayfa konumu", |
| 4885 | 4893 | "common.truncatedNotice": "{total} kayıttan ilk {shown} tanesi gösteriliyor.", |
| 4886 | 4894 | "common.createItem": "Yeni {item} oluştur", |
| @@ -5329,6 +5337,7 @@ const tr: Dict = { | ||
| 5329 | 5337 | "events.helpBody": |
| 5330 | 5338 | "Öğretmenler ve yöneticiler yalnız öğrenciler için yoklama kaydeder. Yöneticiler her etkinliği düzenleyebilir.", |
| 5331 | 5339 | "calendar.title": "Takvim", |
| 5340 | + "calendar.feedsFailed": "Yüklenemedi: {feeds}. Takvimde bu kayıtlar eksik.", | |
| 5332 | 5341 | "calendar.today": "Bugün", |
| 5333 | 5342 | "calendar.noEvents": "Bu güne ait bir şey yok.", |
| 5334 | 5343 | "calendar.events": "Etkinlikler", |
src/lib/board-resources.test.tsxadded+36 -0
| @@ -0,0 +1,36 @@ | ||
| 1 | +import { createRoot } from "solid-js"; | |
| 2 | +import { expect, test, vi } from "vitest"; | |
| 3 | +import { createBoardResources } from "@/lib/board-resources"; | |
| 4 | + | |
| 5 | +const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); | |
| 6 | + | |
| 7 | +test("a failed source reads as undefined and only it is retried", async () => { | |
| 8 | + const good = vi.fn(async () => "ok"); | |
| 9 | + let fail = true; | |
| 10 | + const flaky = vi.fn(async () => { | |
| 11 | + if (fail) throw new Error("boom"); | |
| 12 | + return "recovered"; | |
| 13 | + }); | |
| 14 | + | |
| 15 | + await createRoot(async (dispose) => { | |
| 16 | + const board = createBoardResources(); | |
| 17 | + const [a] = board.createResource(good); | |
| 18 | + const [b] = board.createResource(flaky); | |
| 19 | + await tick(); | |
| 20 | + | |
| 21 | + expect(a()).toBe("ok"); | |
| 22 | + expect(b()).toBeUndefined(); | |
| 23 | + expect(b.latest).toBeUndefined(); | |
| 24 | + expect(b.error).toBeInstanceOf(Error); | |
| 25 | + | |
| 26 | + fail = false; | |
| 27 | + board.retryFailed(); | |
| 28 | + await tick(); | |
| 29 | + | |
| 30 | + expect(b()).toBe("recovered"); | |
| 31 | + expect(b.error).toBeUndefined(); | |
| 32 | + expect(good).toHaveBeenCalledTimes(1); | |
| 33 | + expect(flaky).toHaveBeenCalledTimes(2); | |
| 34 | + dispose(); | |
| 35 | + }); | |
| 36 | +}); |
src/lib/board-resources.tsadded+30 -0
| @@ -0,0 +1,30 @@ | ||
| 1 | +import { createResource } from "@/lib/create-resource"; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Resources for a board of independent panels (the dashboard). | |
| 5 | + * | |
| 6 | + * A plain resource rethrows its error on read, so one failed request took | |
| 7 | + * down every panel with it. Here a failed source reads as `undefined` — its | |
| 8 | + * panel shows its own empty/"—" state while `.error` stays available for the | |
| 9 | + * board's notice — and `retryFailed` refetches only the sources that failed. | |
| 10 | + */ | |
| 11 | +export function createBoardResources() { | |
| 12 | + const sources: { failed: () => boolean; refetch: () => unknown }[] = []; | |
| 13 | + | |
| 14 | + const resource = ((...args: unknown[]) => { | |
| 15 | + const [res, actions] = (createResource as unknown as (...a: unknown[]) => [Record<string, unknown> & (() => unknown), { refetch: () => unknown }])(...args); | |
| 16 | + const read = () => (res.error ? undefined : res()); | |
| 17 | + for (const key of ["state", "loading", "error"] as const) { | |
| 18 | + Object.defineProperty(read, key, { get: () => res[key] }); | |
| 19 | + } | |
| 20 | + Object.defineProperty(read, "latest", { get: () => (res.error ? undefined : res.latest) }); | |
| 21 | + sources.push({ failed: () => !!res.error, refetch: actions.refetch }); | |
| 22 | + return [read, actions]; | |
| 23 | + }) as typeof createResource; | |
| 24 | + | |
| 25 | + const retryFailed = () => { | |
| 26 | + for (const source of sources) if (source.failed()) void source.refetch(); | |
| 27 | + }; | |
| 28 | + | |
| 29 | + return { createResource: resource, retryFailed }; | |
| 30 | +} |
src/pages/calendar-page.test.tsxadded+62 -0
| @@ -0,0 +1,62 @@ | ||
| 1 | +import { cleanup, fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library"; | |
| 2 | +import type { JSX } from "solid-js"; | |
| 3 | +import CalendarPage from "@/pages/calendar-page"; | |
| 4 | +import { PreferencesProvider } from "@/stores/preferences-context"; | |
| 5 | + | |
| 6 | +const { failExams, getExams } = vi.hoisted(() => ({ failExams: { times: 0 }, getExams: vi.fn() })); | |
| 7 | + | |
| 8 | +vi.mock("@tanstack/solid-router", () => ({ | |
| 9 | + Link: (props: { to: string; children: JSX.Element; class?: string }) => <a href={props.to} class={props.class}>{props.children}</a>, | |
| 10 | + Navigate: () => null, | |
| 11 | + useNavigate: () => vi.fn(), | |
| 12 | +})); | |
| 13 | +vi.mock("@/stores/auth-context", () => ({ | |
| 14 | + useAuth: () => ({ | |
| 15 | + user: () => ({ id: "u-1", username: "teacher", role: "teacher" }), | |
| 16 | + loading: () => false, | |
| 17 | + error: () => undefined, | |
| 18 | + }), | |
| 19 | +})); | |
| 20 | +const page = <T,>(items: T[]) => ({ items, total: items.length, limit: 100, offset: 0 }); | |
| 21 | +vi.mock("@/api/time", () => ({ getTime: vi.fn(async () => ({ now: Date.now() })) })); | |
| 22 | +vi.mock("@/api/events", () => ({ getEvents: vi.fn(async () => page([])) })); | |
| 23 | +vi.mock("@/api/exams", () => ({ getExams })); | |
| 24 | +vi.mock("@/api/homework", () => ({ getHomework: vi.fn(async () => page([])) })); | |
| 25 | +vi.mock("@/api/appointments", () => ({ getAppointments: vi.fn(async () => page([])) })); | |
| 26 | +vi.mock("@/api/courses", () => ({ getCourses: vi.fn(async () => page([])) })); | |
| 27 | +vi.mock("@/api/reports", () => ({ getMyCourses: vi.fn(async () => page([])) })); | |
| 28 | +vi.mock("@/api/instances", () => ({ | |
| 29 | + getMyInstances: vi.fn(async () => page([])), | |
| 30 | + getInstanceSessions: vi.fn(async () => page([])), | |
| 31 | +})); | |
| 32 | + | |
| 33 | +beforeEach(() => { | |
| 34 | + getExams.mockImplementation(async () => { | |
| 35 | + if (failExams.times > 0) { | |
| 36 | + failExams.times -= 1; | |
| 37 | + throw new Error("exams are down"); | |
| 38 | + } | |
| 39 | + return page([]); | |
| 40 | + }); | |
| 41 | +}); | |
| 42 | +afterEach(() => { | |
| 43 | + cleanup(); | |
| 44 | + vi.clearAllMocks(); | |
| 45 | +}); | |
| 46 | + | |
| 47 | +test("a failed feed is named above the grid instead of reading as an empty calendar", async () => { | |
| 48 | + failExams.times = 1; | |
| 49 | + render(() => ( | |
| 50 | + <PreferencesProvider> | |
| 51 | + <CalendarPage /> | |
| 52 | + </PreferencesProvider> | |
| 53 | + )); | |
| 54 | + | |
| 55 | + const notice = await screen.findByRole("alert"); | |
| 56 | + expect(notice.textContent).toMatch(/Exams|Sınavlar/); | |
| 57 | + expect(screen.getByRole("heading", { level: 1 })).toBeTruthy(); | |
| 58 | + | |
| 59 | + fireEvent.click(within(notice).getByRole("button")); | |
| 60 | + await waitFor(() => expect(screen.queryByRole("alert")).toBeNull()); | |
| 61 | + expect(getExams).toHaveBeenCalledTimes(2); | |
| 62 | +}); |
src/pages/calendar-page.tsxmodified+36 -14
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { For, Show, Suspense, createMemo, createSignal } from "solid-js"; |
| 2 | -import { createResource } from "@/lib/create-resource"; | |
| 2 | +import { createBoardResources } from "@/lib/board-resources"; | |
| 3 | 3 | import { getEvents } from "@/api/events"; |
| 4 | 4 | import { getExams } from "@/api/exams"; |
| 5 | 5 | import { getHomework } from "@/api/homework"; |
| @@ -149,14 +149,17 @@ function CalendarContent() { | ||
| 149 | 149 | const [selected, setSelected] = createSignal(dateKey(nowDate())); |
| 150 | 150 | const [view, setView] = createSignal<CalendarView>("month"); |
| 151 | 151 | |
| 152 | - const [events] = createResource(async () => (await getEvents({ limit: 100 })).items, { initialValue: [] }); | |
| 153 | - const [exams] = createResource(async () => (await getExams({ limit: 100 })).items, { initialValue: [] }); | |
| 154 | - const [appointments] = createResource(async () => (await getAppointments({ limit: 100 })).items, { initialValue: [] }); | |
| 155 | - const [homework] = createResource(async () => (await getHomework({ limit: 100 })).items, { initialValue: [] }); | |
| 152 | + // Each feed stands alone: a failed one reads as empty and is named in the | |
| 153 | + // notice above the grid, instead of blanking the whole calendar. | |
| 154 | + const board = createBoardResources(); | |
| 155 | + const [events] = board.createResource(async () => (await getEvents({ limit: 100 })).items); | |
| 156 | + const [exams] = board.createResource(async () => (await getExams({ limit: 100 })).items); | |
| 157 | + const [appointments] = board.createResource(async () => (await getAppointments({ limit: 100 })).items); | |
| 158 | + const [homework] = board.createResource(async () => (await getHomework({ limit: 100 })).items); | |
| 156 | 159 | |
| 157 | 160 | // Lessons and study/club meetings both come from course sessions; the course's |
| 158 | 161 | // own `kind` is what separates them. |
| 159 | - const [sessions] = createResource( | |
| 162 | + const [sessions] = board.createResource( | |
| 160 | 163 | () => auth.user()?.role ?? null, |
| 161 | 164 | async (role) => { |
| 162 | 165 | // Sessions hang off the instance (şube × ders) now; the catalog course is |
| @@ -181,14 +184,22 @@ function CalendarContent() { | ||
| 181 | 184 | ); |
| 182 | 185 | return pages.flat(); |
| 183 | 186 | }, |
| 184 | - { initialValue: [] }, | |
| 185 | 187 | ); |
| 188 | + const feeds = [ | |
| 189 | + { resource: sessions, labelKey: "calendar.lessons" }, | |
| 190 | + { resource: exams, labelKey: "calendar.exams" }, | |
| 191 | + { resource: homework, labelKey: "calendar.homework" }, | |
| 192 | + { resource: events, labelKey: "calendar.events" }, | |
| 193 | + { resource: appointments, labelKey: "calendar.appointments" }, | |
| 194 | + ] as const; | |
| 195 | + const loadingFeeds = () => feeds.some((feed) => feed.resource.loading); | |
| 196 | + const failedFeeds = () => feeds.filter((feed) => feed.resource.error).map((feed) => t(feed.labelKey)); | |
| 186 | 197 | |
| 187 | 198 | const counterpart = (a: Appointment) => appointmentCounterpart(a, auth.user()?.id); |
| 188 | 199 | |
| 189 | 200 | const items = createMemo<CalendarItem[]>(() => { |
| 190 | 201 | const rows: CalendarItem[] = []; |
| 191 | - for (const { session, course } of sessions()) { | |
| 202 | + for (const { session, course } of sessions() ?? []) { | |
| 192 | 203 | rows.push({ |
| 193 | 204 | id: session.id, |
| 194 | 205 | kind: course.kind === "course" ? "lesson" : "study", |
| @@ -198,18 +209,18 @@ function CalendarContent() { | ||
| 198 | 209 | href: `/courses/${course.id}`, |
| 199 | 210 | }); |
| 200 | 211 | } |
| 201 | - for (const exam of exams()) { | |
| 212 | + for (const exam of exams() ?? []) { | |
| 202 | 213 | if (!exam.starts_at || exam.draft) continue; |
| 203 | 214 | rows.push({ id: exam.id, kind: "exam", title: exam.title, at: exam.starts_at, endsAt: exam.ends_at, href: `/exams/${exam.id}` }); |
| 204 | 215 | } |
| 205 | - for (const hw of homework()) { | |
| 216 | + for (const hw of homework() ?? []) { | |
| 206 | 217 | rows.push({ id: hw.id, kind: "homework", title: hw.title, at: hw.due_at, endsAt: null, href: `/homework/${hw.id}` }); |
| 207 | 218 | } |
| 208 | - for (const event of events()) { | |
| 219 | + for (const event of events() ?? []) { | |
| 209 | 220 | if (!event.starts_at) continue; |
| 210 | 221 | rows.push({ id: event.id, kind: "event", title: event.title, at: event.starts_at, endsAt: event.ends_at, href: `/events/${event.id}` }); |
| 211 | 222 | } |
| 212 | - for (const appointment of appointments()) { | |
| 223 | + for (const appointment of appointments() ?? []) { | |
| 213 | 224 | if (!appointment.starts_at || (appointment.status !== "pending" && appointment.status !== "approved")) continue; |
| 214 | 225 | rows.push({ |
| 215 | 226 | id: appointment.id, |
| @@ -333,11 +344,11 @@ function CalendarContent() { | ||
| 333 | 344 | <div class="min-w-0"> |
| 334 | 345 | <h1 class="truncate text-lg font-semibold tracking-tight text-foreground sm:text-xl">{t("calendar.title")}</h1> |
| 335 | 346 | <div class="mt-1.5 flex items-center gap-1 sm:gap-2"> |
| 336 | - <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={goPrev}> | |
| 347 | + <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={goPrev} aria-label={t("common.prev")}> | |
| 337 | 348 | <IconChevronLeft class="h-4 w-4" /> |
| 338 | 349 | </Button> |
| 339 | 350 | <span class="truncate text-sm font-semibold tracking-tight sm:text-base">{rangeLabel()}</span> |
| 340 | - <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={goNext}> | |
| 351 | + <Button type="button" variant="ghost" size="sm" class="h-8 w-8 rounded-lg p-0" onClick={goNext} aria-label={t("common.next")}> | |
| 341 | 352 | <IconChevronRight class="h-4 w-4" /> |
| 342 | 353 | </Button> |
| 343 | 354 | </div> |
| @@ -380,6 +391,17 @@ function CalendarContent() { | ||
| 380 | 391 | </For> |
| 381 | 392 | </div> |
| 382 | 393 | |
| 394 | + <Show when={failedFeeds().length > 0}> | |
| 395 | + <div role="alert" class="flex flex-wrap items-center justify-between gap-3 rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive"> | |
| 396 | + <span class="min-w-0 flex-1">{t("calendar.feedsFailed", { feeds: failedFeeds().join(", ") })}</span> | |
| 397 | + <Button type="button" size="sm" variant="outline" class="shrink-0 border-destructive/40" onClick={() => board.retryFailed()}> | |
| 398 | + {t("common.tryAgain")} | |
| 399 | + </Button> | |
| 400 | + </div> | |
| 401 | + </Show> | |
| 402 | + <Show when={loadingFeeds()}> | |
| 403 | + <p role="status" class="text-xs text-muted-foreground">{t("common.loading")}</p> | |
| 404 | + </Show> | |
| 383 | 405 | <Suspense fallback={<PageSpinner />}> |
| 384 | 406 | <div class={cn("grid min-h-0 flex-1 gap-4", view() === "month" && "xl:grid-cols-[minmax(0,1fr)_20rem]")}> |
| 385 | 407 | <Show when={view() === "week"}> |
src/pages/dashboard-page.test.tsxmodified+23 -2
| @@ -1,15 +1,16 @@ | ||
| 1 | -import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; | |
| 1 | +import { cleanup, fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library"; | |
| 2 | 2 | import type { JSX } from "solid-js"; |
| 3 | 3 | import DashboardPage from "@/pages/dashboard-page"; |
| 4 | 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | 5 | |
| 6 | -const { navigate, authUser, schoolModules, calls, refuseExams } = vi.hoisted(() => ({ | |
| 6 | +const { navigate, authUser, schoolModules, calls, refuseExams, failExams } = vi.hoisted(() => ({ | |
| 7 | 7 | navigate: vi.fn(), |
| 8 | 8 | authUser: { authenticated: true, role: "student" }, |
| 9 | 9 | // null = module state unknown (fail open, everything shown). |
| 10 | 10 | schoolModules: { enabled: null as string[] | null }, |
| 11 | 11 | calls: [] as string[], |
| 12 | 12 | refuseExams: { on: false }, |
| 13 | + failExams: { times: 0 }, | |
| 13 | 14 | })); |
| 14 | 15 | |
| 15 | 16 | vi.mock("@tanstack/solid-router", () => ({ |
| @@ -100,6 +101,11 @@ vi.mock("@/api/exams", async () => { | ||
| 100 | 101 | getExams: async () => { |
| 101 | 102 | // A builder switched exams off after the modules list was read. |
| 102 | 103 | if (refuseExams.on) throw new ApiError(403, "forbidden", null, "exams"); |
| 104 | + // A plain server failure, not a module refusal. | |
| 105 | + if (failExams.times > 0) { | |
| 106 | + failExams.times -= 1; | |
| 107 | + throw new Error("exams are down"); | |
| 108 | + } | |
| 103 | 109 | return page([ |
| 104 | 110 | { |
| 105 | 111 | id: "exam-1", |
| @@ -193,6 +199,7 @@ afterEach(() => { | ||
| 193 | 199 | schoolModules.enabled = null; |
| 194 | 200 | calls.length = 0; |
| 195 | 201 | refuseExams.on = false; |
| 202 | + failExams.times = 0; | |
| 196 | 203 | }); |
| 197 | 204 | |
| 198 | 205 | function renderDashboard(role: string) { |
| @@ -302,3 +309,17 @@ test("a module refusal mid-session reads as no data instead of breaking the boar | ||
| 302 | 309 | expect(await screen.findByRole("heading", { name: "Upcoming deadlines" })).toBeTruthy(); |
| 303 | 310 | expect(screen.queryByText(/permission|switched off/i)).toBeNull(); |
| 304 | 311 | }); |
| 312 | + | |
| 313 | +test("a failed source leaves the other panels up and can be retried", async () => { | |
| 314 | + failExams.times = 1; | |
| 315 | + renderDashboard("manager"); | |
| 316 | + | |
| 317 | + const notice = await screen.findByRole("alert"); | |
| 318 | + expect(notice.textContent).toMatch(/could not load|yüklenemedi/); | |
| 319 | + expect(screen.getByRole("heading", { name: "Upcoming deadlines" })).toBeTruthy(); | |
| 320 | + expect(await screen.findByText("Event deadline")).toBeTruthy(); | |
| 321 | + | |
| 322 | + fireEvent.click(within(notice).getByRole("button")); | |
| 323 | + expect(await screen.findByText("Exam deadline")).toBeTruthy(); | |
| 324 | + await waitFor(() => expect(screen.queryByRole("alert")).toBeNull()); | |
| 325 | +}); |
src/pages/dashboard-page.tsxmodified+28 -15
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { For, Show, Suspense, createEffect, createMemo, createSignal, type Component } from "solid-js"; |
| 2 | -import { createResource } from "@/lib/create-resource"; | |
| 2 | +import { createBoardResources } from "@/lib/board-resources"; | |
| 3 | 3 | import { useNavigate } from "@tanstack/solid-router"; |
| 4 | 4 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 5 | 5 | import type { Role } from "@/api/client"; |
| @@ -19,6 +19,7 @@ import { getMyAttendance, getMyCourses, getMyMarks, getUserAttendance } from "@/ | ||
| 19 | 19 | import { getTime } from "@/api/time/getTime"; |
| 20 | 20 | import { getUsers } from "@/api/users"; |
| 21 | 21 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 22 | +import { Button } from "@/components/ui/button"; | |
| 22 | 23 | import { EmptyInline } from "@/components/ui/empty-inline"; |
| 23 | 24 | import { Select } from "@/components/ui/select"; |
| 24 | 25 | import { Badge } from "@/components/ui/badge"; |
| @@ -118,6 +119,10 @@ export default function DashboardPage() { | ||
| 118 | 119 | } |
| 119 | 120 | |
| 120 | 121 | function DashboardContent() { |
| 122 | + // Every panel reads its own source; one failed request must not take the | |
| 123 | + // others down with it (see createBoardResources). | |
| 124 | + const board = createBoardResources(); | |
| 125 | + const createResource = board.createResource; | |
| 121 | 126 | const auth = useAuth(); |
| 122 | 127 | const t = useT(); |
| 123 | 128 | const navigate = useNavigate(); |
| @@ -142,7 +147,7 @@ function DashboardContent() { | ||
| 142 | 147 | () => role() === "parent" || !on("courses") ? null : role(), |
| 143 | 148 | (currentRole) => quiet(currentRole === "student" ? getMyCourses() : getCourses()), |
| 144 | 149 | ); |
| 145 | - const courseCount = () => (courses()?.items ?? []).filter((course) => course.kind === "course").length; | |
| 150 | + const courseCount = () => (courses.error ? "—" : String((courses()?.items ?? []).filter((course) => course.kind === "course").length)); | |
| 146 | 151 | // `/events` has no role gate — a parent-teacher conference is a real PAR-01 |
| 147 | 152 | // "Yaklaşan" item, so parent reads this too (unlike `exams`/`homework` |
| 148 | 153 | // below, which really are course-scoped and out of a parent's reach). |
| @@ -397,37 +402,40 @@ function DashboardContent() { | ||
| 397 | 402 | return total ? Math.round((present / total) * 100) : null; |
| 398 | 403 | }); |
| 399 | 404 | |
| 405 | + /** A stat over a list total; a source that failed shows "—", never a made-up 0. */ | |
| 406 | + const countOf = (source: { (): { total: number } | null | undefined; error: unknown }) => | |
| 407 | + source.error ? "—" : String(source()?.total ?? 0); | |
| 400 | 408 | const allStats = createMemo<StatCardData[]>(() => { |
| 401 | 409 | const r = role(); |
| 402 | 410 | if (r === "student") { |
| 403 | 411 | return [ |
| 404 | - { labelKey: "dashboard.stats.courses", module: "courses", value: String(courseCount()), Icon: IconBook }, | |
| 405 | - { labelKey: "dashboard.stats.exams", module: "exams", value: String(exams()?.total ?? 0), Icon: IconExam }, | |
| 412 | + { labelKey: "dashboard.stats.courses", module: "courses", value: courseCount(), Icon: IconBook }, | |
| 413 | + { labelKey: "dashboard.stats.exams", module: "exams", value: countOf(exams), Icon: IconExam }, | |
| 406 | 414 | { labelKey: "dashboard.stats.average", module: "marks", value: marks()?.overall_average == null ? "—" : marks()!.overall_average!.toFixed(1), Icon: IconChart }, |
| 407 | 415 | { labelKey: "dashboard.stats.attendance", module: "attendance", value: attendanceRate() == null ? "—" : `${attendanceRate()}%`, Icon: IconClipboardCheck }, |
| 408 | 416 | ]; |
| 409 | 417 | } |
| 410 | 418 | if (r === "teacher") { |
| 411 | 419 | return [ |
| 412 | - { labelKey: "dashboard.stats.courses", module: "courses", value: String(courseCount()), Icon: IconBook }, | |
| 420 | + { labelKey: "dashboard.stats.courses", module: "courses", value: courseCount(), Icon: IconBook }, | |
| 413 | 421 | { labelKey: "dashboard.stats.students", module: "courses", value: teacherStudentCount() == null ? "—" : String(teacherStudentCount()), Icon: IconUsers }, |
| 414 | - { labelKey: "dashboard.stats.exams", module: "exams", value: String(exams()?.total ?? 0), Icon: IconExam }, | |
| 415 | - { labelKey: "dashboard.stats.homework", module: "homework", value: String(homework()?.total ?? 0), Icon: IconHomework }, | |
| 422 | + { labelKey: "dashboard.stats.exams", module: "exams", value: countOf(exams), Icon: IconExam }, | |
| 423 | + { labelKey: "dashboard.stats.homework", module: "homework", value: countOf(homework), Icon: IconHomework }, | |
| 416 | 424 | ]; |
| 417 | 425 | } |
| 418 | 426 | if (r === "parent") { |
| 419 | 427 | return [ |
| 420 | - { labelKey: "dashboard.stats.children", value: String(children()?.total ?? 0), Icon: IconUsers }, | |
| 421 | - { labelKey: "dashboard.stats.appointments", module: "appointments", value: String(appointments()?.total ?? 0), Icon: IconCalendarDays }, | |
| 422 | - { labelKey: "dashboard.stats.meals", module: "meals", value: String(menus()?.total ?? 0), Icon: IconUtensils }, | |
| 428 | + { labelKey: "dashboard.stats.children", value: countOf(children), Icon: IconUsers }, | |
| 429 | + { labelKey: "dashboard.stats.appointments", module: "appointments", value: countOf(appointments), Icon: IconCalendarDays }, | |
| 430 | + { labelKey: "dashboard.stats.meals", module: "meals", value: countOf(menus), Icon: IconUtensils }, | |
| 423 | 431 | { labelKey: "dashboard.stats.childAttendance", module: "attendance", value: childAttendanceBreakdown()?.rate == null ? "—" : `${childAttendanceBreakdown()!.rate}%`, Icon: IconClipboardCheck }, |
| 424 | 432 | ]; |
| 425 | 433 | } |
| 426 | 434 | return [ |
| 427 | - { labelKey: "dashboard.stats.courses", module: "courses", value: String(courseCount()), Icon: IconBook }, | |
| 428 | - { labelKey: "dashboard.stats.exams", module: "exams", value: String(exams()?.total ?? 0), Icon: IconExam }, | |
| 429 | - { labelKey: "dashboard.stats.events", module: "events", value: String(events()?.total ?? 0), Icon: IconCalendarDays }, | |
| 430 | - { labelKey: "dashboard.stats.meals", module: "meals", value: String(menus()?.total ?? 0), Icon: IconUtensils }, | |
| 435 | + { labelKey: "dashboard.stats.courses", module: "courses", value: courseCount(), Icon: IconBook }, | |
| 436 | + { labelKey: "dashboard.stats.exams", module: "exams", value: countOf(exams), Icon: IconExam }, | |
| 437 | + { labelKey: "dashboard.stats.events", module: "events", value: countOf(events), Icon: IconCalendarDays }, | |
| 438 | + { labelKey: "dashboard.stats.meals", module: "meals", value: countOf(menus), Icon: IconUtensils }, | |
| 431 | 439 | ]; |
| 432 | 440 | }); |
| 433 | 441 | const stats = () => allStats().filter((stat) => !stat.module || on(stat.module)); |
| @@ -716,7 +724,12 @@ function DashboardContent() { | ||
| 716 | 724 | |
| 717 | 725 | <Suspense fallback={<PageSpinner />}> |
| 718 | 726 | <Show when={error()}> |
| 719 | - <p class="rounded-xl border border-destructive/30 bg-destructive/5 px-4 py-3 text-sm text-destructive">{error()}</p> | |
| 727 | + <div role="alert" class="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-destructive/30 bg-destructive/5 px-4 py-3 text-sm text-destructive"> | |
| 728 | + <span class="min-w-0 flex-1">{t("dashboard.partialError")} {error()}</span> | |
| 729 | + <Button type="button" size="sm" variant="outline" class="shrink-0 border-destructive/40" onClick={() => board.retryFailed()}> | |
| 730 | + {t("common.tryAgain")} | |
| 731 | + </Button> | |
| 732 | + </div> | |
| 720 | 733 | </Show> |
| 721 | 734 | |
| 722 | 735 | <section class="space-y-3" aria-labelledby="highlights-heading"> |