hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createEffect, createMemo, createSignal, type JSX } from "solid-js"; |
| 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | import { formatApiError } from "@/api/client"; |
| 4 | import type { ExamWeightEntry } from "@/api/client"; |
| 5 | import { Alert } from "@/components/ui/alert"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { DataTable } from "@/components/ui/data-table"; |
| 8 | import { IconEdit, IconTrash } from "@/components/ui/icons"; |
| 9 | import { Input } from "@/components/ui/input"; |
| 10 | import { Label } from "@/components/ui/label"; |
| 11 | import { Select } from "@/components/ui/select"; |
| 12 | import { SidePanel } from "@/components/ui/side-panel"; |
| 13 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 14 | import { examKindLabel } from "@/lib/exam-labels"; |
| 15 | import { useT } from "@/stores/preferences-context"; |
| 16 | |
| 17 | /** |
| 18 | * An exam-kind weight map. `editing` opens the set-a-weight panel (the parent |
| 19 | * passes its add button through `actions`, into the table toolbar beside |
| 20 | * "Sütunlar"); rows can be edited or removed when `canEdit`. |
| 21 | */ |
| 22 | export function ExamWeightsEditor(props: { |
| 23 | weights: ExamWeightEntry[]; |
| 24 | /** The kinds the school runs (settings.exam_kinds), for the picker. */ |
| 25 | kinds: string[]; |
| 26 | canEdit: boolean; |
| 27 | onSet: (entry: ExamWeightEntry) => Promise<void>; |
| 28 | onRemove?: (kind: string) => Promise<void>; |
| 29 | editing: ExamWeightEntry | "new" | null; |
| 30 | onEditingChange: (entry: ExamWeightEntry | "new" | null) => void; |
| 31 | /** Toolbar controls (add, reset), rendered in the table's toolbar card. */ |
| 32 | actions?: JSX.Element; |
| 33 | }) { |
| 34 | const t = useT(); |
| 35 | const [kind, setKind] = createSignal(""); |
| 36 | const [weight, setWeight] = createSignal(""); |
| 37 | const [error, setError] = createSignal(""); |
| 38 | const [pending, setPending] = createSignal(false); |
| 39 | const [removeError, setRemoveError] = createSignal(""); |
| 40 | |
| 41 | const remove = async (kind: string) => { |
| 42 | setRemoveError(""); |
| 43 | try { |
| 44 | await props.onRemove?.(kind); |
| 45 | } catch (err) { |
| 46 | setRemoveError(formatApiError(err)); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | createEffect(() => { |
| 51 | const target = props.editing; |
| 52 | if (target === null) return; |
| 53 | setKind(target === "new" ? props.kinds.find((k) => !props.weights.some((w) => w.kind === k)) ?? props.kinds[0] ?? "" : target.kind); |
| 54 | setWeight(target === "new" ? "" : String(target.weight)); |
| 55 | setError(""); |
| 56 | }); |
| 57 | |
| 58 | const save = async (event: SubmitEvent) => { |
| 59 | event.preventDefault(); |
| 60 | const value = Number(weight().trim()); |
| 61 | if (!Number.isInteger(value) || value < 1 || value > 100) { |
| 62 | setError(t("instances.weightInvalid")); |
| 63 | return; |
| 64 | } |
| 65 | setPending(true); |
| 66 | try { |
| 67 | await props.onSet({ kind: kind(), weight: value }); |
| 68 | props.onEditingChange(null); |
| 69 | } catch (err) { |
| 70 | setError(formatApiError(err)); |
| 71 | } finally { |
| 72 | setPending(false); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | const columns = createMemo<ColumnDef<ExamWeightEntry>[]>(() => [ |
| 77 | { |
| 78 | id: "kind", |
| 79 | accessorFn: (row) => examKindLabel(row.kind, t), |
| 80 | header: t("instances.examKind"), |
| 81 | meta: { cellClass: "font-medium" }, |
| 82 | }, |
| 83 | { |
| 84 | id: "weight", |
| 85 | accessorFn: (row) => row.weight, |
| 86 | header: t("instances.weight"), |
| 87 | meta: { cellClass: "tabular-nums" }, |
| 88 | }, |
| 89 | ...(props.canEdit |
| 90 | ? [ |
| 91 | { |
| 92 | id: "actions", |
| 93 | header: t("common.actions"), |
| 94 | meta: { headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", cellClass: "text-center" }, |
| 95 | cell: (cell) => ( |
| 96 | <TableRowActions |
| 97 | label={t("common.actions")} |
| 98 | actions={[ |
| 99 | { label: t("instances.setWeight"), icon: <IconEdit class="h-4 w-4" />, onSelect: () => props.onEditingChange(cell.row.original) }, |
| 100 | ...(props.onRemove |
| 101 | ? [{ label: t("instances.removeWeight"), icon: <IconTrash class="h-4 w-4" />, destructive: true, onSelect: () => void remove(cell.row.original.kind) }] |
| 102 | : []), |
| 103 | ]} |
| 104 | /> |
| 105 | ), |
| 106 | } satisfies ColumnDef<ExamWeightEntry>, |
| 107 | ] |
| 108 | : []), |
| 109 | ]); |
| 110 | |
| 111 | return ( |
| 112 | <> |
| 113 | <Show when={removeError()}><Alert variant="destructive">{removeError()}</Alert></Show> |
| 114 | <DataTable columns={columns()} data={props.weights} empty={t("instances.noWeights")} actions={props.actions} /> |
| 115 | <SidePanel |
| 116 | open={props.editing !== null} |
| 117 | onOpenChange={(open) => !open && props.onEditingChange(null)} |
| 118 | title={props.editing === "new" ? t("instances.addWeight") : t("instances.setWeight")} |
| 119 | description={t("instances.examWeightsHelp")} |
| 120 | > |
| 121 | <form class="space-y-4" noValidate onSubmit={save}> |
| 122 | <div class="space-y-1.5"> |
| 123 | <Label for="weight-kind">{t("instances.examKind")}</Label> |
| 124 | <Select id="weight-kind" value={kind()} disabled={props.editing !== "new"} onChange={(e) => setKind(e.currentTarget.value)}> |
| 125 | <For each={props.kinds}>{(k) => <option value={k}>{examKindLabel(k, t)}</option>}</For> |
| 126 | </Select> |
| 127 | </div> |
| 128 | <div class="space-y-1.5"> |
| 129 | <Label for="weight-value">{t("instances.weight")}</Label> |
| 130 | <Input id="weight-value" type="number" inputMode="numeric" min={1} max={100} value={weight()} onInput={(e) => setWeight(e.currentTarget.value)} /> |
| 131 | </div> |
| 132 | <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> |
| 133 | <div class="flex gap-2 border-t border-border-hairline pt-4"> |
| 134 | <Button type="submit" disabled={pending() || !kind()}>{t("common.save")}</Button> |
| 135 | <Button type="button" variant="outline" onClick={() => props.onEditingChange(null)}>{t("common.cancel")}</Button> |
| 136 | </div> |
| 137 | </form> |
| 138 | </SidePanel> |
| 139 | </> |
| 140 | ); |
| 141 | } |