hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, createMemo, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 4 | import { deleteClassBlueprintByGrade, getClassBlueprints, getClassBlueprintStatus } from "@/api/classes"; |
| 5 | import { getCourses } from "@/api/courses"; |
| 6 | import { getLimits } from "@/api/limits"; |
| 7 | import { formatApiError, type BlueprintResult, type BlueprintSectionStatus, type BlueprintSkip, type BlueprintStatus, type ClassBlueprint } from "@/api/client"; |
| 8 | import { BlueprintPanel } from "@/components/classes/blueprint-panel"; |
| 9 | import { BlueprintSkippedReport } from "@/components/classes/blueprint-skipped-report"; |
| 10 | import { Alert } from "@/components/ui/alert"; |
| 11 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 12 | import { Button } from "@/components/ui/button"; |
| 13 | import { TOOLBAR_CONTROL } from "@/components/ui/data-toolbar"; |
| 14 | import { cn } from "@/lib/cn"; |
| 15 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 16 | import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; |
| 17 | import { IconClipboardCheck, IconEdit, IconPlus, IconTrash } from "@/components/ui/icons"; |
| 18 | import { SidePanel } from "@/components/ui/side-panel"; |
| 19 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 20 | import { createFlash } from "@/lib/flash"; |
| 21 | import { gradeLevelLabel } from "@/lib/grade-level"; |
| 22 | import { personLabel } from "@/lib/person"; |
| 23 | import { useT } from "@/stores/preferences-context"; |
| 24 | |
| 25 | export function BlueprintsTab(props: { |
| 26 | /** Every blueprint route is manager+; below that the tab is not rendered. */ |
| 27 | canManage: () => boolean; |
| 28 | /** Tabs keep both panels mounted, so the list is only fetched once opened. */ |
| 29 | active: () => boolean; |
| 30 | }) { |
| 31 | const t = useT(); |
| 32 | const [error, setError] = createSignal(""); |
| 33 | const [flash, setFlash] = createFlash(); |
| 34 | const [panelOpen, setPanelOpen] = createSignal(false); |
| 35 | const [editing, setEditing] = createSignal<ClassBlueprint | null>(null); |
| 36 | const [deleting, setDeleting] = createSignal<ClassBlueprint | null>(null); |
| 37 | const [skipped, setSkipped] = createSignal<BlueprintSkip[]>([]); |
| 38 | const [unmatchedGrade, setUnmatchedGrade] = createSignal<string | null>(null); |
| 39 | const [reportOpen, setReportOpen] = createSignal(false); |
| 40 | const [status, setStatus] = createSignal<BlueprintStatus | null>(null); |
| 41 | |
| 42 | const enabled = () => (props.active() && props.canManage() ? true : null); |
| 43 | const [list, { refetch }] = createResource(enabled, async () => |
| 44 | (await getClassBlueprints({ limit: 200 })).items, |
| 45 | ); |
| 46 | const [courses] = createResource(enabled, async () => (await getCourses({ limit: 200 })).items); |
| 47 | const [limits] = createResource(enabled, () => getLimits()); |
| 48 | |
| 49 | // `.latest` rethrows a failed read; these keep one failure from throwing |
| 50 | // the whole page out (the list shows ErrorAlert, the rest fall back). |
| 51 | const courseList = () => (courses.error ? [] : courses.latest ?? []); |
| 52 | const limitData = () => (limits.error ? undefined : limits.latest); |
| 53 | const courseTitle = (id: string) => courseList().find((c) => c.id === id)?.title ?? id; |
| 54 | |
| 55 | const columns = createMemo<ColumnDef<ClassBlueprint>[]>(() => [ |
| 56 | { |
| 57 | id: "grade", |
| 58 | accessorFn: (row) => gradeLevelLabel(row.grade_level, t), |
| 59 | sortingFn: (a, b) => a.original.grade_level - b.original.grade_level, |
| 60 | header: t("classBlueprints.grade"), |
| 61 | meta: { cellClass: "font-medium" }, |
| 62 | }, |
| 63 | { |
| 64 | id: "count", |
| 65 | accessorFn: (row) => t("classBlueprints.courseCount", { count: row.courses.length }), |
| 66 | // The label is text ("10 ders" sorts before "3 ders"); order by the count. |
| 67 | sortingFn: (a, b) => a.original.courses.length - b.original.courses.length, |
| 68 | header: t("classBlueprints.courseCountColumn"), |
| 69 | }, |
| 70 | { |
| 71 | id: "titles", |
| 72 | accessorFn: (row) => { |
| 73 | const names = row.courses.slice(0, 3).map(courseTitle); |
| 74 | const rest = row.courses.length - names.length; |
| 75 | return rest > 0 ? `${names.join(", ")} +${rest}` : names.join(", "); |
| 76 | }, |
| 77 | header: t("classBlueprints.courses"), |
| 78 | meta: { cellClass: "text-muted-foreground" }, |
| 79 | }, |
| 80 | { |
| 81 | id: "creator", |
| 82 | accessorFn: (row) => personLabel(row.creator), |
| 83 | header: t("common.creator"), |
| 84 | meta: { cellClass: "text-muted-foreground" }, |
| 85 | }, |
| 86 | { |
| 87 | id: "actions", |
| 88 | header: t("common.actions"), |
| 89 | meta: { headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", cellClass: "text-center" }, |
| 90 | cell: (cell) => ( |
| 91 | <TableRowActions |
| 92 | label={t("common.actions")} |
| 93 | actions={[ |
| 94 | { |
| 95 | label: t("classBlueprints.edit"), |
| 96 | icon: <IconEdit class="h-4 w-4" />, |
| 97 | onSelect: () => { |
| 98 | setEditing(cell.row.original); |
| 99 | setPanelOpen(true); |
| 100 | }, |
| 101 | }, |
| 102 | { |
| 103 | label: t("classBlueprints.status"), |
| 104 | icon: <IconClipboardCheck class="h-4 w-4" />, |
| 105 | onSelect: () => void checkStatus(cell.row.original.grade_level), |
| 106 | }, |
| 107 | { |
| 108 | label: t("classBlueprints.delete"), |
| 109 | icon: <IconTrash class="h-4 w-4" />, |
| 110 | destructive: true, |
| 111 | onSelect: () => setDeleting(cell.row.original), |
| 112 | }, |
| 113 | ]} |
| 114 | /> |
| 115 | ), |
| 116 | }, |
| 117 | ]); |
| 118 | |
| 119 | // A saved blueprint is not an error even when some pairs did not attach: the |
| 120 | // template was written either way, so the toast fires and the shortfall is a |
| 121 | // separate, dismissible report. |
| 122 | const handleSaved = async (result: BlueprintResult) => { |
| 123 | setPanelOpen(false); |
| 124 | setEditing(null); |
| 125 | await refetch(); |
| 126 | setSkipped(result.skipped); |
| 127 | // A grade no class sits on is saved just the same, so it must not |
| 128 | // read as "applied" — that is the one outcome the admin needs to fix. |
| 129 | if (result.matched === 0) { |
| 130 | setUnmatchedGrade(gradeLevelLabel(result.blueprint.grade_level, t)); |
| 131 | return; |
| 132 | } |
| 133 | setUnmatchedGrade(null); |
| 134 | setFlash(t("classBlueprints.savedMatched", { count: result.matched })); |
| 135 | }; |
| 136 | |
| 137 | const remove = async () => { |
| 138 | const bp = deleting(); |
| 139 | if (!bp) return; |
| 140 | setError(""); |
| 141 | try { |
| 142 | await deleteClassBlueprintByGrade(bp.grade_level); |
| 143 | setDeleting(null); |
| 144 | await refetch(); |
| 145 | setFlash(t("classBlueprints.deleted")); |
| 146 | } catch (err) { |
| 147 | setError(formatApiError(err)); |
| 148 | } |
| 149 | }; |
| 150 | const checkStatus = async (gradeLevel: number) => { |
| 151 | setError(""); |
| 152 | try { |
| 153 | setStatus(await getClassBlueprintStatus(gradeLevel)); |
| 154 | } catch (err) { |
| 155 | setError(formatApiError(err)); |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | const statusColumns = createMemo<ColumnDef<BlueprintSectionStatus>[]>(() => [ |
| 160 | { |
| 161 | id: "section", |
| 162 | accessorFn: (row) => row.class_name || row.class, |
| 163 | header: t("classBlueprints.statusSection"), |
| 164 | meta: { cellClass: "font-medium" }, |
| 165 | }, |
| 166 | { |
| 167 | id: "missing", |
| 168 | accessorFn: (row) => row.missing.map(courseTitle).join(", "), |
| 169 | header: t("classBlueprints.statusMissing"), |
| 170 | meta: { cellClass: "text-muted-foreground" }, |
| 171 | }, |
| 172 | ]); |
| 173 | |
| 174 | return ( |
| 175 | <div class="space-y-4"> |
| 176 | <Show when={flash()}> |
| 177 | <Alert variant="success">{flash()}</Alert> |
| 178 | </Show> |
| 179 | <Show when={error()}> |
| 180 | <Alert variant="destructive">{error()}</Alert> |
| 181 | </Show> |
| 182 | <Show when={unmatchedGrade()}> |
| 183 | {(grade) => ( |
| 184 | <Alert class="border-amber-500/40 bg-amber-500/10 text-amber-900 dark:text-amber-200"> |
| 185 | {t("classBlueprints.noMatch", { grade: grade() })} |
| 186 | </Alert> |
| 187 | )} |
| 188 | </Show> |
| 189 | <Show when={skipped().length > 0}> |
| 190 | <Alert class="flex flex-wrap items-center justify-between gap-3 border-amber-500/40 bg-amber-500/10 text-amber-900 dark:text-amber-200"> |
| 191 | <span class="min-w-0 flex-1">{t("classBlueprints.skippedSummary", { count: skipped().length })}</span> |
| 192 | <Button type="button" size="sm" variant="outline" class={cn(TOOLBAR_CONTROL, "shrink-0 px-3.5")} onClick={() => setReportOpen(true)}> |
| 193 | {t("classBlueprints.skippedDetails")} |
| 194 | </Button> |
| 195 | </Alert> |
| 196 | </Show> |
| 197 | |
| 198 | {/* The list is read through `.latest`, so a refetch after a save never |
| 199 | re-suspends the tab; the skeleton only covers the first load. */} |
| 200 | <Show when={list.error}> |
| 201 | <ErrorAlert message={formatApiError(list.error)} onRetry={() => void refetch()} /> |
| 202 | </Show> |
| 203 | <Show when={!list.error && (!list.loading || list.latest)} fallback={<Show when={!list.error}><DataTableSkeleton /></Show>}> |
| 204 | <DataTable |
| 205 | columns={columns()} |
| 206 | data={list.latest ?? []} |
| 207 | filterColumn="grade" |
| 208 | filterPlaceholder={t("classBlueprints.searchPlaceholder")} |
| 209 | filterHint={t("search.hint.blueprints")} |
| 210 | enablePagination |
| 211 | pageSize={10} |
| 212 | title={t("classBlueprints.title")} |
| 213 | description={t("classBlueprints.subtitle")} |
| 214 | empty={t("classBlueprints.empty")} |
| 215 | actions={ |
| 216 | <Button |
| 217 | size="sm" |
| 218 | class="min-w-[7.5rem]" |
| 219 | onClick={() => { |
| 220 | setEditing(null); |
| 221 | setPanelOpen(true); |
| 222 | }} |
| 223 | > |
| 224 | <IconPlus class="h-4 w-4" /> |
| 225 | {t("classBlueprints.new")} |
| 226 | </Button> |
| 227 | } |
| 228 | /> |
| 229 | </Show> |
| 230 | |
| 231 | <BlueprintPanel |
| 232 | open={panelOpen()} |
| 233 | onOpenChange={(open) => { |
| 234 | setPanelOpen(open); |
| 235 | if (!open) setEditing(null); |
| 236 | }} |
| 237 | blueprint={editing()} |
| 238 | courses={courseList()} |
| 239 | maxCourses={limitData()?.course.max_class_courses ?? 50} |
| 240 | minGradeLevel={limitData()?.course.min_grade_level} |
| 241 | maxGradeLevel={limitData()?.course.max_grade_level} |
| 242 | onSaved={(result) => void handleSaved(result)} |
| 243 | /> |
| 244 | |
| 245 | <BlueprintSkippedReport |
| 246 | open={reportOpen()} |
| 247 | onOpenChange={setReportOpen} |
| 248 | skipped={skipped()} |
| 249 | courseTitle={courseTitle} |
| 250 | /> |
| 251 | |
| 252 | <SidePanel |
| 253 | open={status() != null} |
| 254 | onOpenChange={(open) => !open && setStatus(null)} |
| 255 | title={t("classBlueprints.statusTitle", { grade: status() ? gradeLevelLabel(status()!.grade_level, t) : "" })} |
| 256 | size="wide" |
| 257 | > |
| 258 | <Show when={status()}> |
| 259 | {(current) => ( |
| 260 | <Show |
| 261 | when={current().sections.some((section) => section.missing.length > 0)} |
| 262 | fallback={<p class="text-sm text-muted-foreground">{t("classBlueprints.statusEmpty")}</p>} |
| 263 | > |
| 264 | <DataTable |
| 265 | columns={statusColumns()} |
| 266 | data={current().sections.filter((section) => section.missing.length > 0)} |
| 267 | /> |
| 268 | </Show> |
| 269 | )} |
| 270 | </Show> |
| 271 | </SidePanel> |
| 272 | <ConfirmDialog |
| 273 | open={deleting() !== null} |
| 274 | onOpenChange={(open) => !open && setDeleting(null)} |
| 275 | title={t("classBlueprints.delete")} |
| 276 | summary={t("classBlueprints.deleteConfirm", { grade: deleting() ? gradeLevelLabel(deleting()!.grade_level, t) : "" })} |
| 277 | variant="destructive" |
| 278 | onConfirm={remove} |
| 279 | /> |
| 280 | </div> |
| 281 | ); |
| 282 | } |