From 41442f809d4726f791662f1e6c5aebe92c8e9cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Mon, 13 Jul 2026 08:35:45 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20user=20search=20picke?= =?UTF-8?q?r=20for=20person=20selection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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.ts | 8 ++ src/components/exams/grade-form.tsx | 5 +- src/components/users/user-search-select.tsx | 82 +++++++++++++++++++++ src/pages/course-detail-page.tsx | 49 +++--------- src/pages/event-detail-page.tsx | 19 ++--- src/pages/student-marks-page.tsx | 12 +-- 6 files changed, 117 insertions(+), 58 deletions(-) create mode 100644 src/api/getUserSearch.ts create mode 100644 src/components/users/user-search-select.tsx diff --git a/src/api/getUserSearch.ts b/src/api/getUserSearch.ts new file mode 100644 index 0000000..32d9311 --- /dev/null +++ b/src/api/getUserSearch.ts @@ -0,0 +1,8 @@ +import { client } from "./client"; +import type { PersonRef, Role } from "./types"; + +export function getUserSearch(query: string, role?: Role, signal?: AbortSignal): Promise { + const params = new URLSearchParams({ q: query }); + if (role) params.set("role", role); + return client(`/users/search?${params.toString()}`, { signal }); +} diff --git a/src/components/exams/grade-form.tsx b/src/components/exams/grade-form.tsx index 37fb43b..63ca5b1 100644 --- a/src/components/exams/grade-form.tsx +++ b/src/components/exams/grade-form.tsx @@ -1,4 +1,4 @@ -import { For, createSignal } from "solid-js"; +import { For, createMemo, createSignal } from "solid-js"; import { formatApiError } from "@/api/client"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; @@ -24,6 +24,7 @@ export function GradeForm(props: { const [pending, setPending] = createSignal(false); const [confirmOpen, setConfirmOpen] = createSignal(false); const [pendingValues, setPendingValues] = createSignal(null); + const selectedStudentLabel = createMemo(() => props.students.find((student) => student.id === userId())?.label ?? userId()); const handleSubmit = async (e: SubmitEvent) => { e.preventDefault(); @@ -89,7 +90,7 @@ export function GradeForm(props: { onOpenChange={setConfirmOpen} title={t("confirm.updateTitle")} summary={t("confirm.gradeStudent", { - user: pendingValues()?.user_id ?? "", + user: selectedStudentLabel(), mark: pendingValues()?.mark ?? 0, })} onConfirm={async () => { diff --git a/src/components/users/user-search-select.tsx b/src/components/users/user-search-select.tsx new file mode 100644 index 0000000..4f8e333 --- /dev/null +++ b/src/components/users/user-search-select.tsx @@ -0,0 +1,82 @@ +import { For, Show, createMemo, createResource, createSignal } from "solid-js"; +import { getUserSearch } from "@/api/getUserSearch"; +import type { Role } from "@/api/types"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Select } from "@/components/ui/select"; +import { personLabelWithId } from "@/lib/person"; +import { useT } from "@/stores/preferences-context"; + +export function UserSearchSelect(props: { + id: string; + value: string; + onChange: (value: string) => void; + label?: string; + role?: Role; + excludeIds?: string[]; + placeholder?: string; + disabled?: boolean; + allowManualValue?: boolean; +}) { + const t = useT(); + const [query, setQuery] = createSignal(""); + const search = createMemo(() => { + const q = query().trim(); + if (q.length < 2) return null; + return { q, role: props.role }; + }); + const [users] = createResource(search, async (source) => { + if (!source) return []; + return getUserSearch(source.q, source.role); + }); + const options = createMemo(() => { + const excluded = new Set(props.excludeIds ?? []); + return (users() ?? []).filter((user) => !excluded.has(user.id)); + }); + const hasManualValue = createMemo(() => { + const value = props.value.trim(); + return props.allowManualValue && value.length > 0 && !options().some((user) => user.id === value); + }); + const hint = () => { + if (query().trim().length < 2) return t("common.searchPlaceholder"); + if (users.loading) return t("common.loading"); + if (hasManualValue()) return props.value; + if (options().length === 0) return t("form.noStudents"); + return props.placeholder ?? t("form.selectStudent"); + }; + + return ( +
+ + {(label) => } + + { + const value = e.currentTarget.value; + setQuery(value); + props.onChange(props.allowManualValue ? value.trim() : ""); + }} + /> + +
+ ); +} diff --git a/src/pages/course-detail-page.tsx b/src/pages/course-detail-page.tsx index 7ce9d9f..4258c51 100644 --- a/src/pages/course-detail-page.tsx +++ b/src/pages/course-detail-page.tsx @@ -6,7 +6,6 @@ import { getCourseById } from "@/api/getCourseById"; import { getCourseEnrollments } from "@/api/getCourseEnrollments"; import { getCourseExams } from "@/api/getCourseExams"; import { getMyCourses } from "@/api/getMyCourses"; -import { getUsers } from "@/api/getUsers"; import { patchCourseById } from "@/api/patchCourseById"; import { postCourseEnrollment } from "@/api/postCourseEnrollment"; import { postCourseExam } from "@/api/postCourseExam"; @@ -22,7 +21,6 @@ import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { PageSpinner } from "@/components/ui/page-spinner"; -import { Select } from "@/components/ui/select"; import { Table, TableBody, @@ -32,6 +30,7 @@ import { TableRow, } from "@/components/ui/table"; import { Textarea } from "@/components/ui/textarea"; +import { UserSearchSelect } from "@/components/users/user-search-select"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; import { examKindLabel } from "@/lib/exam-labels"; @@ -59,10 +58,6 @@ function CourseDetailContent() { () => (isTeacherPlus() ? id() : null), async (courseId) => (courseId ? getCourseEnrollments(courseId) : []), ); - const [users] = createResource( - () => (hasMinRole(auth.user()?.role, "admin") ? true : null), - async (enabled) => (enabled ? getUsers() : []), - ); const [mine] = createResource( () => (auth.user()?.role === "student" ? true : null), async (enabled) => (enabled ? getMyCourses() : []), @@ -103,14 +98,7 @@ function CourseDetailContent() { const rosterCount = createMemo(() => roster()?.length ?? 0); const totalWeight = createMemo(() => (exams() ?? []).reduce((sum, exam) => sum + exam.weight, 0)); - const enrollableUsers = () => { - const enrolled = new Set((roster() ?? []).map((row) => row.user.id)); - return (users() ?? []).filter((user) => user.role === "student" && !enrolled.has(user.id)); - }; - const userOptionLabel = (user: { id: string; username: string; name: string | null; surname: string | null }) => { - const fullName = [user.name, user.surname].filter(Boolean).join(" ").trim(); - return `${fullName || user.username} - ${user.id}`; - }; + const enrolledUserIds = () => (roster() ?? []).map((row) => row.user.id); const wrap = async (fn: () => Promise) => { setError(""); @@ -403,31 +391,14 @@ function CourseDetailContent() { }} >
- 0} - fallback={ - setEnrollUserId(e.currentTarget.value)} - /> - } - > - - +