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, createMemo, createSignal } from "solid-js"; |
| 2 | import { formatApiError } from "@/api/client"; |
| 3 | import type { Subject } from "@/api/client"; |
| 4 | import { Alert } from "@/components/ui/alert"; |
| 5 | import { Button } from "@/components/ui/button"; |
| 6 | import { TOOLBAR_CONTROL } from "@/components/ui/data-toolbar"; |
| 7 | import { IconPlus, IconX } from "@/components/ui/icons"; |
| 8 | import { SearchableSelect } from "@/components/ui/searchable-select"; |
| 9 | import { cn } from "@/lib/cn"; |
| 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | |
| 12 | /** |
| 13 | * A selected-topic set (a grade template's, or a section's own) drawn as |
| 14 | * chips; editors add from the catalog course's subjects and remove chips. |
| 15 | */ |
| 16 | export function SubjectSetEditor(props: { |
| 17 | selected: Subject[]; |
| 18 | /** The catalog course's subjects — the only ones a set may hold. */ |
| 19 | available: Subject[]; |
| 20 | canEdit: boolean; |
| 21 | /** Either list is still loading: draw nothing rather than "no topics" / "all taken". */ |
| 22 | loading?: boolean; |
| 23 | onAdd: (subjectId: string) => Promise<void>; |
| 24 | onRemove: (subject: Subject) => Promise<void>; |
| 25 | }) { |
| 26 | const t = useT(); |
| 27 | const [pick, setPick] = createSignal(""); |
| 28 | const [pending, setPending] = createSignal(false); |
| 29 | const [error, setError] = createSignal(""); |
| 30 | const addable = createMemo(() => { |
| 31 | const taken = new Set(props.selected.map((subject) => subject.id)); |
| 32 | return props.available.filter((subject) => !taken.has(subject.id)); |
| 33 | }); |
| 34 | |
| 35 | const run = async (action: () => Promise<void>) => { |
| 36 | if (pending()) return; |
| 37 | setError(""); |
| 38 | setPending(true); |
| 39 | try { |
| 40 | await action(); |
| 41 | } catch (err) { |
| 42 | setError(formatApiError(err)); |
| 43 | } finally { |
| 44 | setPending(false); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | return ( |
| 49 | <div class="space-y-3" aria-busy={props.loading || undefined}> |
| 50 | <Show when={!props.loading} fallback={<div class="h-8 w-full max-w-sm animate-pulse rounded-full bg-surface-tint" />}> |
| 51 | <Show when={props.selected.length > 0} fallback={<p class="text-sm text-muted-foreground">{t("instances.noSubjects")}</p>}> |
| 52 | <ul class="flex flex-wrap gap-1.5"> |
| 53 | <For each={props.selected}> |
| 54 | {(subject) => ( |
| 55 | <li class="inline-flex items-center gap-1 rounded-md border border-border-line bg-surface-tint py-1 pl-2.5 pr-1 text-sm"> |
| 56 | <span class="max-w-[16rem] truncate" title={subject.description || subject.name}>{subject.name}</span> |
| 57 | <Show when={props.canEdit}> |
| 58 | <button |
| 59 | type="button" |
| 60 | class="inline-flex h-5 w-5 items-center justify-center rounded text-muted-foreground hover:bg-muted hover:text-destructive-text disabled:opacity-50" |
| 61 | aria-label={`${t("instances.removeSubject")}: ${subject.name}`} |
| 62 | disabled={pending()} |
| 63 | onClick={() => void run(() => props.onRemove(subject))} |
| 64 | > |
| 65 | <IconX class="h-3.5 w-3.5" /> |
| 66 | </button> |
| 67 | </Show> |
| 68 | </li> |
| 69 | )} |
| 70 | </For> |
| 71 | </ul> |
| 72 | </Show> |
| 73 | <Show when={props.canEdit}> |
| 74 | <Show when={addable().length > 0} fallback={<p class="text-xs text-muted-foreground">{t("instances.allSubjectsSelected")}</p>}> |
| 75 | {/* Picker and button are one control row: both take the toolbar |
| 76 | pill so the button is not a 26px chip beside a 36px field. */} |
| 77 | <div class="flex flex-wrap items-center gap-2"> |
| 78 | <SearchableSelect |
| 79 | class={cn(TOOLBAR_CONTROL, "min-w-56 flex-1 sm:max-w-sm [&_input]:text-[13px]")} |
| 80 | value={pick()} |
| 81 | onChange={setPick} |
| 82 | placeholder={t("subjects.select")} |
| 83 | options={addable().map((subject) => ({ value: subject.id, label: subject.name }))} |
| 84 | /> |
| 85 | <Button |
| 86 | type="button" |
| 87 | size="sm" |
| 88 | variant="outline" |
| 89 | class={cn(TOOLBAR_CONTROL, "px-3.5")} |
| 90 | disabled={!pick() || pending()} |
| 91 | onClick={() => void run(async () => { await props.onAdd(pick()); setPick(""); })} |
| 92 | > |
| 93 | <IconPlus class="h-4 w-4" /> |
| 94 | {t("instances.addSubject")} |
| 95 | </Button> |
| 96 | </div> |
| 97 | </Show> |
| 98 | </Show> |
| 99 | </Show> |
| 100 | <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> |
| 101 | </div> |
| 102 | ); |
| 103 | } |