✨ add user search picker for person selection
Browse files at this commit41442f809d4726f791662f1e6c5aebe92c8e9cd1Parent: 64327d6- feat
- ✨ add user search picker for person selection
6 changed files +117 -58
- src/api/getUserSearch.ts +8 -0
- src/components/exams/grade-form.tsx +3 -2
- src/components/users/user-search-select.tsx +82 -0
- src/pages/course-detail-page.tsx +10 -39
- src/pages/event-detail-page.tsx +8 -11
- src/pages/student-marks-page.tsx +6 -6
src/api/getUserSearch.tsadded+8 -0
| @@ -0,0 +1,8 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | +import type { PersonRef, Role } from "./types"; | |
| 3 | + | |
| 4 | +export function getUserSearch(query: string, role?: Role, signal?: AbortSignal): Promise<PersonRef[]> { | |
| 5 | + const params = new URLSearchParams({ q: query }); | |
| 6 | + if (role) params.set("role", role); | |
| 7 | + return client<PersonRef[]>(`/users/search?${params.toString()}`, { signal }); | |
| 8 | +} |
src/components/exams/grade-form.tsxmodified+3 -2
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { For, createSignal } from "solid-js"; | |
| 1 | +import { For, createMemo, createSignal } from "solid-js"; | |
| 2 | 2 | import { formatApiError } from "@/api/client"; |
| 3 | 3 | import { Button } from "@/components/ui/button"; |
| 4 | 4 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| @@ -24,6 +24,7 @@ export function GradeForm(props: { | ||
| 24 | 24 | const [pending, setPending] = createSignal(false); |
| 25 | 25 | const [confirmOpen, setConfirmOpen] = createSignal(false); |
| 26 | 26 | const [pendingValues, setPendingValues] = createSignal<GradeFormValues | null>(null); |
| 27 | + const selectedStudentLabel = createMemo(() => props.students.find((student) => student.id === userId())?.label ?? userId()); | |
| 27 | 28 | |
| 28 | 29 | const handleSubmit = async (e: SubmitEvent) => { |
| 29 | 30 | e.preventDefault(); |
| @@ -89,7 +90,7 @@ export function GradeForm(props: { | ||
| 89 | 90 | onOpenChange={setConfirmOpen} |
| 90 | 91 | title={t("confirm.updateTitle")} |
| 91 | 92 | summary={t("confirm.gradeStudent", { |
| 92 | - user: pendingValues()?.user_id ?? "", | |
| 93 | + user: selectedStudentLabel(), | |
| 93 | 94 | mark: pendingValues()?.mark ?? 0, |
| 94 | 95 | })} |
| 95 | 96 | onConfirm={async () => { |
src/components/users/user-search-select.tsxadded+82 -0
| @@ -0,0 +1,82 @@ | ||
| 1 | +import { For, Show, createMemo, createResource, createSignal } from "solid-js"; | |
| 2 | +import { getUserSearch } from "@/api/getUserSearch"; | |
| 3 | +import type { Role } from "@/api/types"; | |
| 4 | +import { Input } from "@/components/ui/input"; | |
| 5 | +import { Label } from "@/components/ui/label"; | |
| 6 | +import { Select } from "@/components/ui/select"; | |
| 7 | +import { personLabelWithId } from "@/lib/person"; | |
| 8 | +import { useT } from "@/stores/preferences-context"; | |
| 9 | + | |
| 10 | +export function UserSearchSelect(props: { | |
| 11 | + id: string; | |
| 12 | + value: string; | |
| 13 | + onChange: (value: string) => void; | |
| 14 | + label?: string; | |
| 15 | + role?: Role; | |
| 16 | + excludeIds?: string[]; | |
| 17 | + placeholder?: string; | |
| 18 | + disabled?: boolean; | |
| 19 | + allowManualValue?: boolean; | |
| 20 | +}) { | |
| 21 | + const t = useT(); | |
| 22 | + const [query, setQuery] = createSignal(""); | |
| 23 | + const search = createMemo(() => { | |
| 24 | + const q = query().trim(); | |
| 25 | + if (q.length < 2) return null; | |
| 26 | + return { q, role: props.role }; | |
| 27 | + }); | |
| 28 | + const [users] = createResource(search, async (source) => { | |
| 29 | + if (!source) return []; | |
| 30 | + return getUserSearch(source.q, source.role); | |
| 31 | + }); | |
| 32 | + const options = createMemo(() => { | |
| 33 | + const excluded = new Set(props.excludeIds ?? []); | |
| 34 | + return (users() ?? []).filter((user) => !excluded.has(user.id)); | |
| 35 | + }); | |
| 36 | + const hasManualValue = createMemo(() => { | |
| 37 | + const value = props.value.trim(); | |
| 38 | + return props.allowManualValue && value.length > 0 && !options().some((user) => user.id === value); | |
| 39 | + }); | |
| 40 | + const hint = () => { | |
| 41 | + if (query().trim().length < 2) return t("common.searchPlaceholder"); | |
| 42 | + if (users.loading) return t("common.loading"); | |
| 43 | + if (hasManualValue()) return props.value; | |
| 44 | + if (options().length === 0) return t("form.noStudents"); | |
| 45 | + return props.placeholder ?? t("form.selectStudent"); | |
| 46 | + }; | |
| 47 | + | |
| 48 | + return ( | |
| 49 | + <div class="space-y-2"> | |
| 50 | + <Show when={props.label}> | |
| 51 | + {(label) => <Label for={`${props.id}-query`}>{label()}</Label>} | |
| 52 | + </Show> | |
| 53 | + <Input | |
| 54 | + id={`${props.id}-query`} | |
| 55 | + class="h-10" | |
| 56 | + value={query()} | |
| 57 | + disabled={props.disabled} | |
| 58 | + placeholder={props.placeholder ?? t("common.searchPlaceholder")} | |
| 59 | + onInput={(e) => { | |
| 60 | + const value = e.currentTarget.value; | |
| 61 | + setQuery(value); | |
| 62 | + props.onChange(props.allowManualValue ? value.trim() : ""); | |
| 63 | + }} | |
| 64 | + /> | |
| 65 | + <Select | |
| 66 | + id={props.id} | |
| 67 | + class="h-10 rounded-md bg-background text-foreground" | |
| 68 | + value={props.value} | |
| 69 | + disabled={props.disabled || (!hasManualValue() && options().length === 0)} | |
| 70 | + onChange={(e) => props.onChange(e.currentTarget.value)} | |
| 71 | + > | |
| 72 | + <option value="">{hint()}</option> | |
| 73 | + <Show when={hasManualValue()}> | |
| 74 | + <option value={props.value}>{props.value}</option> | |
| 75 | + </Show> | |
| 76 | + <For each={options()}> | |
| 77 | + {(user) => <option value={user.id}>{personLabelWithId(user)}</option>} | |
| 78 | + </For> | |
| 79 | + </Select> | |
| 80 | + </div> | |
| 81 | + ); | |
| 82 | +} |
src/pages/course-detail-page.tsxmodified+10 -39
| @@ -6,7 +6,6 @@ import { getCourseById } from "@/api/getCourseById"; | ||
| 6 | 6 | import { getCourseEnrollments } from "@/api/getCourseEnrollments"; |
| 7 | 7 | import { getCourseExams } from "@/api/getCourseExams"; |
| 8 | 8 | import { getMyCourses } from "@/api/getMyCourses"; |
| 9 | -import { getUsers } from "@/api/getUsers"; | |
| 10 | 9 | import { patchCourseById } from "@/api/patchCourseById"; |
| 11 | 10 | import { postCourseEnrollment } from "@/api/postCourseEnrollment"; |
| 12 | 11 | import { postCourseExam } from "@/api/postCourseExam"; |
| @@ -22,7 +21,6 @@ import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/ | ||
| 22 | 21 | import { Input } from "@/components/ui/input"; |
| 23 | 22 | import { Label } from "@/components/ui/label"; |
| 24 | 23 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 25 | -import { Select } from "@/components/ui/select"; | |
| 26 | 24 | import { |
| 27 | 25 | Table, |
| 28 | 26 | TableBody, |
| @@ -32,6 +30,7 @@ import { | ||
| 32 | 30 | TableRow, |
| 33 | 31 | } from "@/components/ui/table"; |
| 34 | 32 | import { Textarea } from "@/components/ui/textarea"; |
| 33 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | |
| 35 | 34 | import { useAuth } from "@/stores/auth-context"; |
| 36 | 35 | import { useT } from "@/stores/preferences-context"; |
| 37 | 36 | import { examKindLabel } from "@/lib/exam-labels"; |
| @@ -59,10 +58,6 @@ function CourseDetailContent() { | ||
| 59 | 58 | () => (isTeacherPlus() ? id() : null), |
| 60 | 59 | async (courseId) => (courseId ? getCourseEnrollments(courseId) : []), |
| 61 | 60 | ); |
| 62 | - const [users] = createResource( | |
| 63 | - () => (hasMinRole(auth.user()?.role, "admin") ? true : null), | |
| 64 | - async (enabled) => (enabled ? getUsers() : []), | |
| 65 | - ); | |
| 66 | 61 | const [mine] = createResource( |
| 67 | 62 | () => (auth.user()?.role === "student" ? true : null), |
| 68 | 63 | async (enabled) => (enabled ? getMyCourses() : []), |
| @@ -103,14 +98,7 @@ function CourseDetailContent() { | ||
| 103 | 98 | const rosterCount = createMemo(() => roster()?.length ?? 0); |
| 104 | 99 | const totalWeight = createMemo(() => (exams() ?? []).reduce((sum, exam) => sum + exam.weight, 0)); |
| 105 | 100 | |
| 106 | - const enrollableUsers = () => { | |
| 107 | - const enrolled = new Set((roster() ?? []).map((row) => row.user.id)); | |
| 108 | - return (users() ?? []).filter((user) => user.role === "student" && !enrolled.has(user.id)); | |
| 109 | - }; | |
| 110 | - const userOptionLabel = (user: { id: string; username: string; name: string | null; surname: string | null }) => { | |
| 111 | - const fullName = [user.name, user.surname].filter(Boolean).join(" ").trim(); | |
| 112 | - return `${fullName || user.username} - ${user.id}`; | |
| 113 | - }; | |
| 101 | + const enrolledUserIds = () => (roster() ?? []).map((row) => row.user.id); | |
| 114 | 102 | |
| 115 | 103 | const wrap = async (fn: () => Promise<void>) => { |
| 116 | 104 | setError(""); |
| @@ -403,31 +391,14 @@ function CourseDetailContent() { | ||
| 403 | 391 | }} |
| 404 | 392 | > |
| 405 | 393 | <div class="min-w-0"> |
| 406 | - <Show | |
| 407 | - when={(users() ?? []).length > 0} | |
| 408 | - fallback={ | |
| 409 | - <Input | |
| 410 | - class="h-10" | |
| 411 | - placeholder={t("events.userId")} | |
| 412 | - value={enrollUserId()} | |
| 413 | - onInput={(e) => setEnrollUserId(e.currentTarget.value)} | |
| 414 | - /> | |
| 415 | - } | |
| 416 | - > | |
| 417 | - <Select | |
| 418 | - class="h-10 rounded-md bg-background text-foreground" | |
| 419 | - value={enrollUserId()} | |
| 420 | - disabled={enrollableUsers().length === 0} | |
| 421 | - onChange={(e) => setEnrollUserId(e.currentTarget.value)} | |
| 422 | - > | |
| 423 | - <option value=""> | |
| 424 | - {enrollableUsers().length === 0 ? t("form.noStudents") : t("form.selectStudent")} | |
| 425 | - </option> | |
| 426 | - <For each={enrollableUsers()}> | |
| 427 | - {(user) => <option value={user.id}>{userOptionLabel(user)}</option>} | |
| 428 | - </For> | |
| 429 | - </Select> | |
| 430 | - </Show> | |
| 394 | + <UserSearchSelect | |
| 395 | + id="course-enroll-user" | |
| 396 | + value={enrollUserId()} | |
| 397 | + role="student" | |
| 398 | + excludeIds={enrolledUserIds()} | |
| 399 | + placeholder={t("form.selectStudent")} | |
| 400 | + onChange={setEnrollUserId} | |
| 401 | + /> | |
| 431 | 402 | </div> |
| 432 | 403 | <Button type="submit" class="h-10" disabled={pending()}> |
| 433 | 404 | {t("courses.enroll")} |
src/pages/event-detail-page.tsxmodified+8 -11
| @@ -17,9 +17,8 @@ import { Alert } from "@/components/ui/alert"; | ||
| 17 | 17 | import { Button } from "@/components/ui/button"; |
| 18 | 18 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 19 | 19 | import { IconChevronLeft, IconEdit, IconTrash } from "@/components/ui/icons"; |
| 20 | -import { Input } from "@/components/ui/input"; | |
| 21 | -import { Label } from "@/components/ui/label"; | |
| 22 | 20 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 21 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | |
| 23 | 22 | import { formatDateTime } from "@/lib/format"; |
| 24 | 23 | import { hasMinRole } from "@/lib/roles"; |
| 25 | 24 | import { useAuth } from "@/stores/auth-context"; |
| @@ -199,15 +198,13 @@ function EventDetailContent() { | ||
| 199 | 198 | <p class="mt-1 text-sm text-muted-foreground">{t("events.userId")}</p> |
| 200 | 199 | </div> |
| 201 | 200 | <div class="mt-4 grid gap-3"> |
| 202 | - <div class="space-y-1.5"> | |
| 203 | - <Label for="other-user">{t("events.userId")}</Label> | |
| 204 | - <Input | |
| 205 | - id="other-user" | |
| 206 | - class="h-10 rounded-md" | |
| 207 | - value={otherUserId()} | |
| 208 | - onInput={(e) => setOtherUserId(e.currentTarget.value)} | |
| 209 | - /> | |
| 210 | - </div> | |
| 201 | + <UserSearchSelect | |
| 202 | + id="other-user" | |
| 203 | + label={t("events.userId")} | |
| 204 | + value={otherUserId()} | |
| 205 | + placeholder={t("form.selectStudent")} | |
| 206 | + onChange={setOtherUserId} | |
| 207 | + /> | |
| 211 | 208 | <AttendanceStatusPicker |
| 212 | 209 | id="other-status" |
| 213 | 210 | value={status()} |
src/pages/student-marks-page.tsxmodified+6 -6
| @@ -5,9 +5,8 @@ import { MarksReportView } from "@/components/marks/marks-report-view"; | ||
| 5 | 5 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 6 | 6 | import { PageHeader } from "@/components/layout/page-header"; |
| 7 | 7 | import { Button } from "@/components/ui/button"; |
| 8 | -import { Input } from "@/components/ui/input"; | |
| 9 | -import { Label } from "@/components/ui/label"; | |
| 10 | 8 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 9 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | |
| 11 | 10 | import { useT } from "@/stores/preferences-context"; |
| 12 | 11 | |
| 13 | 12 | export default function StudentMarksPage() { |
| @@ -51,13 +50,14 @@ function StudentMarksContent() { | ||
| 51 | 50 | }} |
| 52 | 51 | > |
| 53 | 52 | <div class="min-w-0 flex-1 space-y-1.5"> |
| 54 | - <Label for="student-marks-user">{t("marks.userIdentity")}</Label> | |
| 55 | - <Input | |
| 53 | + <UserSearchSelect | |
| 56 | 54 | id="student-marks-user" |
| 57 | - class="h-10" | |
| 55 | + label={t("marks.userIdentity")} | |
| 56 | + role="student" | |
| 58 | 57 | placeholder={t("marks.userIdentity")} |
| 59 | 58 | value={userId()} |
| 60 | - onInput={(e) => setUserId(e.currentTarget.value)} | |
| 59 | + allowManualValue | |
| 60 | + onChange={setUserId} | |
| 61 | 61 | /> |
| 62 | 62 | </div> |
| 63 | 63 | <Button type="submit" class="h-10 w-full sm:w-auto">{t("marks.show")}</Button> |