forge
Explore
Log in

Don't you dare go Hollow

✨ add user search picker for person selection

Browse files at this commit

41442f809d4726f791662f1e6c5aebe92c8e9cd1Burak BoduroğluParent: 64327d6
  • feat
    • ✨ add user search picker for person selection
Show full message
feat: ✨ add user search picker for person selection

- add /users/search API wrapper and reusable user search select

- replace raw student id inputs in course enrollment, event attendance, and marks lookup

- keep marks lookup compatible with manual user ids when search returns no match

- show selected student labels in grade confirmation text
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";
22 import { formatApiError } from "@/api/client";
33 import { Button } from "@/components/ui/button";
44 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
@@ -24,6 +24,7 @@ export function GradeForm(props: {
2424 const [pending, setPending] = createSignal(false);
2525 const [confirmOpen, setConfirmOpen] = createSignal(false);
2626 const [pendingValues, setPendingValues] = createSignal<GradeFormValues | null>(null);
27+ const selectedStudentLabel = createMemo(() => props.students.find((student) => student.id === userId())?.label ?? userId());
2728
2829 const handleSubmit = async (e: SubmitEvent) => {
2930 e.preventDefault();
@@ -89,7 +90,7 @@ export function GradeForm(props: {
8990 onOpenChange={setConfirmOpen}
9091 title={t("confirm.updateTitle")}
9192 summary={t("confirm.gradeStudent", {
92- user: pendingValues()?.user_id ?? "",
93+ user: selectedStudentLabel(),
9394 mark: pendingValues()?.mark ?? 0,
9495 })}
9596 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";
66 import { getCourseEnrollments } from "@/api/getCourseEnrollments";
77 import { getCourseExams } from "@/api/getCourseExams";
88 import { getMyCourses } from "@/api/getMyCourses";
9-import { getUsers } from "@/api/getUsers";
109 import { patchCourseById } from "@/api/patchCourseById";
1110 import { postCourseEnrollment } from "@/api/postCourseEnrollment";
1211 import { postCourseExam } from "@/api/postCourseExam";
@@ -22,7 +21,6 @@ import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/
2221 import { Input } from "@/components/ui/input";
2322 import { Label } from "@/components/ui/label";
2423 import { PageSpinner } from "@/components/ui/page-spinner";
25-import { Select } from "@/components/ui/select";
2624 import {
2725 Table,
2826 TableBody,
@@ -32,6 +30,7 @@ import {
3230 TableRow,
3331 } from "@/components/ui/table";
3432 import { Textarea } from "@/components/ui/textarea";
33+import { UserSearchSelect } from "@/components/users/user-search-select";
3534 import { useAuth } from "@/stores/auth-context";
3635 import { useT } from "@/stores/preferences-context";
3736 import { examKindLabel } from "@/lib/exam-labels";
@@ -59,10 +58,6 @@ function CourseDetailContent() {
5958 () => (isTeacherPlus() ? id() : null),
6059 async (courseId) => (courseId ? getCourseEnrollments(courseId) : []),
6160 );
62- const [users] = createResource(
63- () => (hasMinRole(auth.user()?.role, "admin") ? true : null),
64- async (enabled) => (enabled ? getUsers() : []),
65- );
6661 const [mine] = createResource(
6762 () => (auth.user()?.role === "student" ? true : null),
6863 async (enabled) => (enabled ? getMyCourses() : []),
@@ -103,14 +98,7 @@ function CourseDetailContent() {
10398 const rosterCount = createMemo(() => roster()?.length ?? 0);
10499 const totalWeight = createMemo(() => (exams() ?? []).reduce((sum, exam) => sum + exam.weight, 0));
105100
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);
114102
115103 const wrap = async (fn: () => Promise<void>) => {
116104 setError("");
@@ -403,31 +391,14 @@ function CourseDetailContent() {
403391 }}
404392 >
405393 <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+ />
431402 </div>
432403 <Button type="submit" class="h-10" disabled={pending()}>
433404 {t("courses.enroll")}
src/pages/event-detail-page.tsxmodified+8 -11
@@ -17,9 +17,8 @@ import { Alert } from "@/components/ui/alert";
1717 import { Button } from "@/components/ui/button";
1818 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
1919 import { IconChevronLeft, IconEdit, IconTrash } from "@/components/ui/icons";
20-import { Input } from "@/components/ui/input";
21-import { Label } from "@/components/ui/label";
2220 import { PageSpinner } from "@/components/ui/page-spinner";
21+import { UserSearchSelect } from "@/components/users/user-search-select";
2322 import { formatDateTime } from "@/lib/format";
2423 import { hasMinRole } from "@/lib/roles";
2524 import { useAuth } from "@/stores/auth-context";
@@ -199,15 +198,13 @@ function EventDetailContent() {
199198 <p class="mt-1 text-sm text-muted-foreground">{t("events.userId")}</p>
200199 </div>
201200 <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+ />
211208 <AttendanceStatusPicker
212209 id="other-status"
213210 value={status()}
src/pages/student-marks-page.tsxmodified+6 -6
@@ -5,9 +5,8 @@ import { MarksReportView } from "@/components/marks/marks-report-view";
55 import { RouteGuard } from "@/components/layout/route-guard";
66 import { PageHeader } from "@/components/layout/page-header";
77 import { Button } from "@/components/ui/button";
8-import { Input } from "@/components/ui/input";
9-import { Label } from "@/components/ui/label";
108 import { PageSpinner } from "@/components/ui/page-spinner";
9+import { UserSearchSelect } from "@/components/users/user-search-select";
1110 import { useT } from "@/stores/preferences-context";
1211
1312 export default function StudentMarksPage() {
@@ -51,13 +50,14 @@ function StudentMarksContent() {
5150 }}
5251 >
5352 <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
5654 id="student-marks-user"
57- class="h-10"
55+ label={t("marks.userIdentity")}
56+ role="student"
5857 placeholder={t("marks.userIdentity")}
5958 value={userId()}
60- onInput={(e) => setUserId(e.currentTarget.value)}
59+ allowManualValue
60+ onChange={setUserId}
6161 />
6262 </div>
6363 <Button type="submit" class="h-10 w-full sm:w-auto">{t("marks.show")}</Button>