hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show, Suspense, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { useNavigate } from "@tanstack/solid-router"; |
| 4 | import { getParentStudents } from "@/api/parents"; |
| 5 | import { postParentStudent } from "@/api/parents"; |
| 6 | import { deleteParentStudent } from "@/api/parents"; |
| 7 | import { formatApiError } from "@/api/client"; |
| 8 | import type { PersonRef } from "@/api/client"; |
| 9 | import { SidePanel } from "@/components/ui/side-panel"; |
| 10 | import { Button } from "@/components/ui/button"; |
| 11 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 12 | import { IconChevronRight, IconPlus, IconTrash } from "@/components/ui/icons"; |
| 13 | import { personLabel } from "@/lib/person"; |
| 14 | import { useT } from "@/stores/preferences-context"; |
| 15 | import { UserSearchSelect } from "@/components/users/user-search-select"; |
| 16 | import { createFlash } from "@/lib/flash"; |
| 17 | import { Alert } from "@/components/ui/alert"; |
| 18 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 19 | |
| 20 | // Admin-facing: the students one parent is the guardian of. This is somebody |
| 21 | // else's list, never the viewer's own children — the parent's own view of the |
| 22 | // same relation lives at /students. |
| 23 | export function ParentStudentsPanel(props: { |
| 24 | open: boolean; |
| 25 | onOpenChange: (open: boolean) => void; |
| 26 | parent: PersonRef | null; |
| 27 | }) { |
| 28 | const t = useT(); |
| 29 | const navigate = useNavigate(); |
| 30 | const [error, setError] = createSignal(""); |
| 31 | const [flash, setFlash] = createFlash(); |
| 32 | const [removing, setRemoving] = createSignal<PersonRef | null>(null); |
| 33 | |
| 34 | const [students, { refetch }] = createResource( |
| 35 | () => (props.open && props.parent ? props.parent.id : null), |
| 36 | async (id) => (await getParentStudents(id)).items |
| 37 | ); |
| 38 | |
| 39 | const [selectedStudentId, setSelectedStudentId] = createSignal(""); |
| 40 | |
| 41 | const linkedIds = () => (students.latest ?? []).map((student) => student.id); |
| 42 | |
| 43 | const handleAdd = async () => { |
| 44 | if (!props.parent || !selectedStudentId()) return; |
| 45 | setError(""); |
| 46 | try { |
| 47 | await postParentStudent(props.parent.id, selectedStudentId()); |
| 48 | await refetch(); |
| 49 | setSelectedStudentId(""); |
| 50 | setFlash(t("parentLink.linked")); |
| 51 | } catch (err) { |
| 52 | setError(formatApiError(err)); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | const handleRemove = async () => { |
| 57 | const student = removing(); |
| 58 | if (!props.parent || !student) return; |
| 59 | setError(""); |
| 60 | try { |
| 61 | await deleteParentStudent(props.parent.id, student.id); |
| 62 | setRemoving(null); |
| 63 | await refetch(); |
| 64 | setFlash(t("parentLink.removed")); |
| 65 | } catch (err) { |
| 66 | setError(formatApiError(err)); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return ( |
| 71 | <SidePanel |
| 72 | open={props.open} |
| 73 | onOpenChange={(open) => { |
| 74 | if (!open) { |
| 75 | setError(""); |
| 76 | setSelectedStudentId(""); |
| 77 | } |
| 78 | props.onOpenChange(open); |
| 79 | }} |
| 80 | title={t("parentLink.title")} |
| 81 | description={props.parent ? t("parentLink.subtitle", { name: personLabel(props.parent) }) : ""} |
| 82 | > |
| 83 | <div class="flex h-full flex-col space-y-6 p-4"> |
| 84 | <Show when={flash()}> |
| 85 | <Alert variant="success">{flash()}</Alert> |
| 86 | </Show> |
| 87 | {error() && <p class="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive-text">{error()}</p>} |
| 88 | |
| 89 | <div class="flex items-end gap-2"> |
| 90 | <div class="flex-1"> |
| 91 | {/* A guardian link only ever points at a student, so the picker |
| 92 | must not offer teachers or other parents. */} |
| 93 | <UserSearchSelect |
| 94 | id="parent-student-select" |
| 95 | role="student" |
| 96 | value={selectedStudentId()} |
| 97 | onChange={setSelectedStudentId} |
| 98 | label={t("parentLink.selectStudent")} |
| 99 | excludeIds={linkedIds()} |
| 100 | /> |
| 101 | </div> |
| 102 | <Button onClick={handleAdd} disabled={!selectedStudentId()}> |
| 103 | <IconPlus class="mr-2 h-4 w-4" /> |
| 104 | {t("parentLink.add")} |
| 105 | </Button> |
| 106 | </div> |
| 107 | |
| 108 | <div class="flex-1 overflow-auto rounded-md border"> |
| 109 | <Suspense fallback={<div class="p-8"><PageSpinner /></div>}> |
| 110 | <Show when={students()}> |
| 111 | <Show |
| 112 | when={students()!.length > 0} |
| 113 | fallback={<div class="p-8 text-center text-sm text-muted-foreground">{t("parentLink.empty")}</div>} |
| 114 | > |
| 115 | <div class="divide-y"> |
| 116 | <For each={students()}> |
| 117 | {(student) => ( |
| 118 | <div class="flex items-center gap-2 p-3"> |
| 119 | {/* A router <Link> inside this modal panel never |
| 120 | navigates — the dialog's dismiss handling swallows |
| 121 | the click — so close the panel first, then go. */} |
| 122 | <button |
| 123 | type="button" |
| 124 | class="group min-w-0 flex-1 rounded-md text-left outline-hidden focus-visible:ring-2 focus-visible:ring-ring" |
| 125 | title={t("profile.viewProfile")} |
| 126 | onClick={() => { |
| 127 | props.onOpenChange(false); |
| 128 | void navigate({ to: "/profile/$userId", params: { userId: student.id } }); |
| 129 | }} |
| 130 | > |
| 131 | <div class="flex items-center gap-1 truncate text-sm font-medium group-hover:underline"> |
| 132 | {personLabel(student)} |
| 133 | <IconChevronRight class="h-3.5 w-3.5 shrink-0 text-muted-foreground" /> |
| 134 | </div> |
| 135 | <div class="truncate text-xs text-muted-foreground">@{student.username}</div> |
| 136 | </button> |
| 137 | <Button |
| 138 | variant="ghost" |
| 139 | size="icon" |
| 140 | class="shrink-0 text-destructive-text hover:bg-destructive/10 hover:text-destructive-text" |
| 141 | aria-label={t("parentLink.remove")} |
| 142 | onClick={() => setRemoving(student)} |
| 143 | > |
| 144 | <IconTrash class="h-4 w-4" /> |
| 145 | </Button> |
| 146 | </div> |
| 147 | )} |
| 148 | </For> |
| 149 | </div> |
| 150 | </Show> |
| 151 | </Show> |
| 152 | </Suspense> |
| 153 | </div> |
| 154 | </div> |
| 155 | |
| 156 | <ConfirmDialog |
| 157 | open={removing() !== null} |
| 158 | onOpenChange={(open) => !open && setRemoving(null)} |
| 159 | title={t("parentLink.remove")} |
| 160 | summary={t("parentLink.removeConfirm", { |
| 161 | student: personLabel(removing()), |
| 162 | parent: personLabel(props.parent), |
| 163 | })} |
| 164 | variant="destructive" |
| 165 | onConfirm={handleRemove} |
| 166 | /> |
| 167 | </SidePanel> |
| 168 | ); |
| 169 | } |