🚚 events page replaces card grid with DataTable
Browse files at this commita97ceab434246cad468d02ce21ac4e91d57bf026Parent: 249dfed- refactor
- 🚚 events page replaces card grid with DataTable
1 changed file +112 -100
- src/pages/events-page.tsx +112 -100
src/pages/events-page.tsxmodified+112 -100
| @@ -1,29 +1,30 @@ | |||
| 1 | -import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; | 1 | +import { Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; |
| 2 | +import { Link, useNavigate } from "@tanstack/solid-router"; | ||
| 3 | +import type { ColumnDef } from "@tanstack/solid-table"; | ||
| 2 | import { getEvents } from "@/api/getEvents"; | 4 | import { getEvents } from "@/api/getEvents"; |
| 3 | import { postEvent } from "@/api/postEvent"; | 5 | import { postEvent } from "@/api/postEvent"; |
| 4 | import { formatApiError } from "@/api/client"; | 6 | import { formatApiError } from "@/api/client"; |
| 5 | import type { Event, EventAudience } from "@/api/types"; | 7 | import type { Event, EventAudience } from "@/api/types"; |
| 6 | import { Alert } from "@/components/ui/alert"; | 8 | import { Alert } from "@/components/ui/alert"; |
| 9 | +import { Badge } from "@/components/ui/badge"; | ||
| 7 | import { Button } from "@/components/ui/button"; | 10 | import { Button } from "@/components/ui/button"; |
| 8 | -import { EventCard } from "@/components/events/event-card"; | ||
| 9 | import { EventForm } from "@/components/events/event-form"; | 11 | import { EventForm } from "@/components/events/event-form"; |
| 10 | import { RouteGuard } from "@/components/layout/route-guard"; | 12 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 11 | import { PageHeader } from "@/components/layout/page-header"; | 13 | import { PageHeader } from "@/components/layout/page-header"; |
| 12 | -import { DataToolbar } from "@/components/ui/data-toolbar"; | 14 | +import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; |
| 13 | -import { EmptyState } from "@/components/ui/empty-state"; | 15 | +import { IconEye, IconPlus } from "@/components/ui/icons"; |
| 14 | -import { IconPlus } from "@/components/ui/icons"; | ||
| 15 | -import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 16 | -import { PaginationControls } from "@/components/ui/pagination-controls"; | ||
| 17 | import { Select } from "@/components/ui/select"; | 16 | import { Select } from "@/components/ui/select"; |
| 18 | import { SidePanel } from "@/components/ui/side-panel"; | 17 | import { SidePanel } from "@/components/ui/side-panel"; |
| 18 | +import { TableRowActions } from "@/components/ui/table-row-actions"; | ||
| 19 | +import { cn } from "@/lib/cn"; | ||
| 19 | import { createFlash } from "@/lib/flash"; | 20 | import { createFlash } from "@/lib/flash"; |
| 20 | import { formatDateTime } from "@/lib/format"; | 21 | import { formatDateTime } from "@/lib/format"; |
| 21 | -import { loadListPage, totalPages as pagesOf } from "@/lib/list-page"; | ||
| 22 | import { hasMinRole } from "@/lib/roles"; | 22 | import { hasMinRole } from "@/lib/roles"; |
| 23 | +import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; | ||
| 23 | import { useAuth } from "@/stores/auth-context"; | 24 | import { useAuth } from "@/stores/auth-context"; |
| 24 | import { usePreferences, useT } from "@/stores/preferences-context"; | 25 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 25 | 26 | ||
| 26 | -const EVENT_PAGE_SIZE = 9; | 27 | +const EVENT_PAGE_SIZE = 10; |
| 27 | 28 | ||
| 28 | export default function EventsPage() { | 29 | export default function EventsPage() { |
| 29 | return ( | 30 | return ( |
| @@ -35,48 +36,108 @@ export default function EventsPage() { | |||
| 35 | 36 | ||
| 36 | function EventsContent() { | 37 | function EventsContent() { |
| 37 | const auth = useAuth(); | 38 | const auth = useAuth(); |
| 39 | + const navigate = useNavigate(); | ||
| 38 | const { locale } = usePreferences(); | 40 | const { locale } = usePreferences(); |
| 39 | const t = useT(); | 41 | const t = useT(); |
| 40 | const [error, setError] = createSignal(""); | 42 | const [error, setError] = createSignal(""); |
| 41 | const [flash, setFlash] = createFlash(); | 43 | const [flash, setFlash] = createFlash(); |
| 42 | const [showForm, setShowForm] = createSignal(false); | 44 | const [showForm, setShowForm] = createSignal(false); |
| 43 | - const [query, setQuery] = createSignal(""); | ||
| 44 | const [timeFilter, setTimeFilter] = createSignal("all"); | 45 | const [timeFilter, setTimeFilter] = createSignal("all"); |
| 45 | - const [page, setPage] = createSignal(0); | ||
| 46 | const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); | 46 | const canCreate = () => hasMinRole(auth.user()?.role, "teacher"); |
| 47 | - const clientFilterActive = () => query().trim() !== "" || timeFilter() !== "all"; | ||
| 48 | 47 | ||
| 49 | const filterEvents = (items: Event[]) => { | 48 | const filterEvents = (items: Event[]) => { |
| 50 | - const needle = query().trim().toLocaleLowerCase(); | ||
| 51 | const scope = timeFilter(); | 49 | const scope = timeFilter(); |
| 52 | const now = Date.now(); | 50 | const now = Date.now(); |
| 53 | return items.filter((event) => { | 51 | return items.filter((event) => { |
| 54 | if (scope === "upcoming" && event.ends_at != null && event.ends_at < now) return false; | 52 | if (scope === "upcoming" && event.ends_at != null && event.ends_at < now) return false; |
| 55 | if (scope === "past" && (event.ends_at == null || event.ends_at >= now)) return false; | 53 | if (scope === "past" && (event.ends_at == null || event.ends_at >= now)) return false; |
| 56 | - if (!needle) return true; | 54 | + return true; |
| 57 | - return [event.title, event.description, formatDateTime(event.starts_at, locale()), formatDateTime(event.ends_at, locale())] | ||
| 58 | - .join(" ") | ||
| 59 | - .toLocaleLowerCase() | ||
| 60 | - .includes(needle); | ||
| 61 | }); | 55 | }); |
| 62 | }; | 56 | }; |
| 63 | 57 | ||
| 64 | - const [list, { refetch }] = createResource( | 58 | + const [list, { refetch }] = createResource(async () => (await getEvents()).items); |
| 65 | - () => [page(), clientFilterActive() ? "1" : "0", query(), timeFilter()].join("|"), | 59 | + const rows = () => filterEvents(list() ?? []); |
| 66 | - async () => | 60 | + const eventStatus = (event: Event) => { |
| 67 | - loadListPage({ | 61 | + const now = Date.now(); |
| 68 | - page: page(), | 62 | + if (event.ends_at != null && event.ends_at < now) return "past"; |
| 69 | - pageSize: EVENT_PAGE_SIZE, | 63 | + if (event.starts_at != null && event.starts_at > now) return "upcoming"; |
| 70 | - clientMode: clientFilterActive(), | 64 | + return "active"; |
| 71 | - fetch: getEvents, | 65 | + }; |
| 72 | - filter: filterEvents, | 66 | + const statusLabel = (status: string) => status === "past" ? t("events.past") : status === "upcoming" ? t("events.upcoming") : t("exams.active"); |
| 73 | - }), | 67 | + const audienceLabel = (audience: EventAudience) => { |
| 74 | - ); | 68 | + if (audience.kind === "school") return t("events.audience.school"); |
| 75 | - | 69 | + if (audience.kind === "role") return t("events.audience.role"); |
| 76 | - const total = () => list()?.total ?? 0; | 70 | + if (audience.kind === "course") return t("events.audience.course"); |
| 77 | - const pageItems = () => list()?.items ?? []; | 71 | + return t("events.audience.registration"); |
| 78 | - const totalPages = createMemo(() => pagesOf(total(), EVENT_PAGE_SIZE)); | 72 | + }; |
| 79 | - const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); | 73 | + const searchEvent = (event: Event, query: string) => |
| 74 | + [event.title, event.description, audienceLabel(event.audience), formatDateTime(event.starts_at, locale()), formatDateTime(event.ends_at, locale())] | ||
| 75 | + .join(" ") | ||
| 76 | + .toLocaleLowerCase(locale()) | ||
| 77 | + .includes(query.toLocaleLowerCase(locale())); | ||
| 78 | + const columns = createMemo<ColumnDef<Event>[]>(() => [ | ||
| 79 | + { | ||
| 80 | + accessorKey: "title", | ||
| 81 | + header: t("events.title"), | ||
| 82 | + cell: (cell) => ( | ||
| 83 | + <div class="min-w-0"> | ||
| 84 | + <Link to="/events/$id" params={{ id: cell.row.original.id }} class="truncate font-medium hover:text-primary hover:underline"> | ||
| 85 | + {cell.row.original.title} | ||
| 86 | + </Link> | ||
| 87 | + <p class="truncate text-xs text-muted-foreground">{cell.row.original.description || "—"}</p> | ||
| 88 | + </div> | ||
| 89 | + ), | ||
| 90 | + }, | ||
| 91 | + { | ||
| 92 | + id: "starts_at", | ||
| 93 | + accessorFn: (event) => event.starts_at ?? 0, | ||
| 94 | + header: t("events.starts"), | ||
| 95 | + meta: { cellClass: "mono text-xs text-muted-foreground" }, | ||
| 96 | + cell: (cell) => formatDateTime(cell.row.original.starts_at, locale()), | ||
| 97 | + }, | ||
| 98 | + { | ||
| 99 | + id: "ends_at", | ||
| 100 | + accessorFn: (event) => event.ends_at ?? 0, | ||
| 101 | + header: t("events.ends"), | ||
| 102 | + meta: { cellClass: "mono text-xs text-muted-foreground" }, | ||
| 103 | + cell: (cell) => formatDateTime(cell.row.original.ends_at, locale()), | ||
| 104 | + }, | ||
| 105 | + { | ||
| 106 | + id: "audience", | ||
| 107 | + accessorFn: (event) => audienceLabel(event.audience), | ||
| 108 | + header: t("events.audience"), | ||
| 109 | + cell: (cell) => <Badge variant="outline" class="rounded-sm">{audienceLabel(cell.row.original.audience)}</Badge>, | ||
| 110 | + }, | ||
| 111 | + { | ||
| 112 | + id: "status", | ||
| 113 | + accessorFn: (event) => statusLabel(eventStatus(event)), | ||
| 114 | + header: t("attempt.status"), | ||
| 115 | + meta: { headerClass: "text-center", cellClass: "text-center" }, | ||
| 116 | + cell: (cell) => { | ||
| 117 | + const status = eventStatus(cell.row.original); | ||
| 118 | + return ( | ||
| 119 | + <Badge variant="outline" class={cn("w-28 justify-center rounded-sm", scheduleStatusClass(status))}> | ||
| 120 | + <span class={cn("mr-1.5 h-1.5 w-1.5 rounded-full", scheduleStatusDotClass(status))} /> | ||
| 121 | + {statusLabel(status)} | ||
| 122 | + </Badge> | ||
| 123 | + ); | ||
| 124 | + }, | ||
| 125 | + }, | ||
| 126 | + { | ||
| 127 | + id: "actions", | ||
| 128 | + header: t("common.actions"), | ||
| 129 | + cell: (cell) => ( | ||
| 130 | + <TableRowActions | ||
| 131 | + label={t("common.actions")} | ||
| 132 | + actions={[{ | ||
| 133 | + label: t("common.view"), | ||
| 134 | + icon: <IconEye class="h-4 w-4" />, | ||
| 135 | + onSelect: () => void navigate({ to: "/events/$id", params: { id: cell.row.original.id } }), | ||
| 136 | + }]} | ||
| 137 | + /> | ||
| 138 | + ), | ||
| 139 | + }, | ||
| 140 | + ]); | ||
| 80 | 141 | ||
| 81 | return ( | 142 | return ( |
| 82 | <div class="space-y-6"> | 143 | <div class="space-y-6"> |
| @@ -138,75 +199,26 @@ function EventsContent() { | |||
| 138 | {error() && <p class="text-sm text-destructive">{error()}</p>} | 199 | {error() && <p class="text-sm text-destructive">{error()}</p>} |
| 139 | 200 | ||
| 140 | <section class="data-shell space-y-4 p-4"> | 201 | <section class="data-shell space-y-4 p-4"> |
| 141 | - <DataToolbar | 202 | + <Suspense fallback={<DataTableSkeleton columns={5} rows={8} />}> |
| 142 | - searchValue={query()} | ||
| 143 | - searchPlaceholder={t("common.searchPlaceholder")} | ||
| 144 | - onSearchInput={(value) => { | ||
| 145 | - setQuery(value); | ||
| 146 | - setPage(0); | ||
| 147 | - }} | ||
| 148 | - filters={ | ||
| 149 | - <Select | ||
| 150 | - class="h-9 w-full rounded-sm sm:w-44" | ||
| 151 | - value={timeFilter()} | ||
| 152 | - aria-label={t("events.title")} | ||
| 153 | - onChange={(event) => { | ||
| 154 | - setTimeFilter(event.currentTarget.value); | ||
| 155 | - setPage(0); | ||
| 156 | - }} | ||
| 157 | - > | ||
| 158 | - <option value="all">{t("common.all")}</option> | ||
| 159 | - <option value="upcoming">{t("events.upcoming")}</option> | ||
| 160 | - <option value="past">{t("events.past")}</option> | ||
| 161 | - </Select> | ||
| 162 | - } | ||
| 163 | - /> | ||
| 164 | - | ||
| 165 | - <Suspense fallback={<PageSpinner />}> | ||
| 166 | <Show when={list.error}> | 203 | <Show when={list.error}> |
| 167 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> | 204 | <Alert variant="destructive">{formatApiError(list.error)}</Alert> |
| 168 | </Show> | 205 | </Show> |
| 169 | - <Show when={list() !== undefined}> | 206 | + <DataTable |
| 170 | - <Show | 207 | + columns={columns()} |
| 171 | - when={pageItems().length > 0} | 208 | + data={rows()} |
| 172 | - fallback={ | 209 | + tableClass="table-fixed min-w-[58rem]" |
| 173 | - <EmptyState | 210 | + searchPredicate={searchEvent} |
| 174 | - title={t("events.empty")} | 211 | + enablePagination |
| 175 | - action={ | 212 | + pageSize={EVENT_PAGE_SIZE} |
| 176 | - canCreate() ? ( | 213 | + empty={t("events.empty")} |
| 177 | - <Button type="button" size="sm" class="rounded-lg" onClick={() => setShowForm(true)}> | 214 | + filters={ |
| 178 | - <IconPlus class="h-4 w-4" /> | 215 | + <Select class="h-9 w-full rounded-sm sm:w-44" value={timeFilter()} aria-label={t("events.title")} onChange={(event) => setTimeFilter(event.currentTarget.value)}> |
| 179 | - {t("events.create")} | 216 | + <option value="all">{t("common.all")}</option> |
| 180 | - </Button> | 217 | + <option value="upcoming">{t("events.upcoming")}</option> |
| 181 | - ) : undefined | 218 | + <option value="past">{t("events.past")}</option> |
| 182 | - } | 219 | + </Select> |
| 183 | - /> | 220 | + } |
| 184 | - } | 221 | + /> |
| 185 | - > | ||
| 186 | - <div class="space-y-4"> | ||
| 187 | - <div class="flex flex-wrap items-center justify-between gap-3"> | ||
| 188 | - <div> | ||
| 189 | - <h2 class="font-display text-lg font-semibold">{t("events.title")}</h2> | ||
| 190 | - <p class="mt-1 text-sm text-muted-foreground"> | ||
| 191 | - {pageItems().length} / {total()} {t("nav.events")} | ||
| 192 | - </p> | ||
| 193 | - </div> | ||
| 194 | - </div> | ||
| 195 | - <ul class="grid grid-cols-2 gap-3 xl:grid-cols-3"> | ||
| 196 | - <For each={pageItems()}> | ||
| 197 | - {(event) => ( | ||
| 198 | - <li class="animate-fade-up"> | ||
| 199 | - <EventCard event={event} /> | ||
| 200 | - </li> | ||
| 201 | - )} | ||
| 202 | - </For> | ||
| 203 | - </ul> | ||
| 204 | - <Show when={total() > EVENT_PAGE_SIZE}> | ||
| 205 | - <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} /> | ||
| 206 | - </Show> | ||
| 207 | - </div> | ||
| 208 | - </Show> | ||
| 209 | - </Show> | ||
| 210 | </Suspense> | 222 | </Suspense> |
| 211 | </section> | 223 | </section> |
| 212 | </div> | 224 | </div> |