From e00544e71550996326017f7d2145797af494b2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Mon, 20 Jul 2026 00:31:32 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=94=A7=20type=20hardening,=20?= =?UTF-8?q?date-picker=20locale,=20answer=20sheet=20merge,=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - types: Exam/ExamAttempt/LiveMonitor fields required instead of optional, remove legacy weight variants (kind_weight, type_weight, exam_type_weight) - AttemptQuestion: split into AttemptQuestionResponse + mapped AttemptQuestion - StudentAnswer replaces GradedAnswer, matches backend shape exactly - getExamAttemptQuestions: adds exam field via map (backend omits it) - answer-sheet-view: fetch questions + answers, merge client-side for display - date-picker: locale-aware day headers via Intl, Portal for overlay, selected-day label, improved outside-click detection - createNow interval: 1000ms -> 30000ms (reduce polling) - exam-weight: remove legacy field fallbacks - live-monitor: pass maxAttempts from exam to attemptLabel - cleanup: delete getSessionById, getSubjectById, getTermById (unused) --- src/api/getExamAttemptQuestions.ts | 7 +- src/api/getSessionById.ts | 6 -- src/api/getSubjectById.ts | 6 -- src/api/getTermById.ts | 6 -- src/api/types.ts | 75 +++++++-------- src/components/exams/answer-sheet-view.tsx | 33 ++++++- src/components/exams/exam-room-ws.tsx | 12 +-- src/components/ui/date-picker.tsx | 103 ++++++++++++--------- src/lib/create-now.ts | 2 +- src/lib/exam-weight.ts | 5 +- src/pages/live-monitor-page.tsx | 6 +- 11 files changed, 134 insertions(+), 127 deletions(-) delete mode 100644 src/api/getSessionById.ts delete mode 100644 src/api/getSubjectById.ts delete mode 100644 src/api/getTermById.ts diff --git a/src/api/getExamAttemptQuestions.ts b/src/api/getExamAttemptQuestions.ts index 553e2dd..4f20038 100644 --- a/src/api/getExamAttemptQuestions.ts +++ b/src/api/getExamAttemptQuestions.ts @@ -1,6 +1,7 @@ import { client } from "./client"; -import type { AttemptQuestion } from "./types"; +import type { AttemptQuestion, AttemptQuestionResponse } from "./types"; -export function getExamAttemptQuestions(examId: string, signal?: AbortSignal): Promise { - return client(`/exams/${examId}/attempt/questions`, { signal }); +export async function getExamAttemptQuestions(examId: string, signal?: AbortSignal): Promise { + const questions = await client(`/exams/${examId}/attempt/questions`, { signal }); + return questions.map((question) => ({ ...question, exam: examId })); } diff --git a/src/api/getSessionById.ts b/src/api/getSessionById.ts deleted file mode 100644 index 210637a..0000000 --- a/src/api/getSessionById.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { client } from "./client"; -import type { CourseSession } from "./types"; - -export function getSessionById(id: string, signal?: AbortSignal): Promise { - return client(`/sessions/${id}`, { signal }); -} diff --git a/src/api/getSubjectById.ts b/src/api/getSubjectById.ts deleted file mode 100644 index 5edad40..0000000 --- a/src/api/getSubjectById.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { client } from "./client"; -import type { Subject } from "./types"; - -export function getSubjectById(id: string, signal?: AbortSignal): Promise { - return client(`/subjects/${id}`, { signal }); -} diff --git a/src/api/getTermById.ts b/src/api/getTermById.ts deleted file mode 100644 index 196372f..0000000 --- a/src/api/getTermById.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { client } from "./client"; -import type { Term } from "./types"; - -export function getTermById(id: string, signal?: AbortSignal): Promise { - return client(`/terms/${id}`, { signal }); -} diff --git a/src/api/types.ts b/src/api/types.ts index b4756ef..3a177f4 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -94,9 +94,8 @@ export type Course = { title: string; description: string; kind: CourseKind; - /** Academic term id from API (`CourseResponse.term`). */ - term?: string | null; - capacity?: number | null; + term: string | null; + capacity: number | null; }; export type Term = { @@ -138,17 +137,13 @@ export type Exam = { title: string; description: string; kind: ExamKind | string; - weight?: number | null; - kind_weight?: number | null; - type_weight?: number | null; - exam_type_weight?: number | null; mode: ExamMode | string | null; starts_at: number | null; ends_at: number | null; duration_ms: number | null; - max_attempts?: number | null; - allow_rejoin?: boolean | null; - draft?: boolean | null; + max_attempts: number; + allow_rejoin: boolean; + draft: boolean; }; export type ImageMeta = { @@ -178,16 +173,18 @@ export type ExamQuestion = { }; export type ExamAttempt = { - id?: string; - exam?: string; - user?: PersonRef; + id: string; + exam: string; + user: PersonRef; status: AttemptStatus; - attempt?: number; - attempts_used?: number; - max_attempts?: number; + attempt: number; + attempts_used: number; + max_attempts: number; + started_at: number; + finished_at: number | null; deadline: number | null; remaining_ms: number | null; - left_at?: number | null; + left_at: number | null; mark: number | null; answered: number; question_count: number; @@ -200,9 +197,8 @@ export type AttemptAnswer = { updated_at?: number; }; -export type AttemptQuestion = { +export type AttemptQuestionResponse = { id: string; - exam: string; subject: string; text: string; kind: QuestionKind; @@ -213,6 +209,8 @@ export type AttemptQuestion = { answer: AttemptAnswer | null; }; +export type AttemptQuestion = AttemptQuestionResponse & { exam: string }; + export type PomodoroSession = { id: string; user: string; @@ -240,10 +238,7 @@ export type MarkEntry = { exam: string; title: string; kind: string; - weight?: number | null; - kind_weight?: number | null; - type_weight?: number | null; - exam_type_weight?: number | null; + weight: number; mark: number; grade?: string | null; graded_by: string; @@ -294,44 +289,40 @@ export type MarksReport = { }; export type ExamStatistics = { + exam: string; graded: number; average: number | null; min: number | null; max: number | null; }; -export type GradedAnswer = { - question_id: string; - text: string; - kind: QuestionKind; - points: number; - choices: string[] | null; - correct: number | null; +export type StudentAnswer = { + question: string; selected: number | null; - text_answer: string | null; + text: string | null; + updated_at: number; is_correct: boolean | null; - auto_score: { earned: number; possible: number }; }; export type StudentAnswerSheet = { + exam: string; user: PersonRef; - answers: GradedAnswer[]; + answers: StudentAnswer[]; auto_score: { earned: number; possible: number }; }; export type LiveRosterEntry = { user: PersonRef; status: AttemptStatus | "not_started" | "absent"; - attempt?: number | null; - attempts_used?: number | null; - max_attempts?: number | null; + attempt: number | null; + attempts_used: number; deadline: number | null; remaining_ms: number | null; - left_at?: number | null; + left_at: number | null; mark: number | null; answered: number; - started_at?: number | null; - finished_at?: number | null; + started_at: number | null; + finished_at: number | null; last_activity: number | null; }; @@ -341,13 +332,13 @@ export type LiveMonitor = { question_count: number; students: LiveRosterEntry[]; counts: { - enrolled?: number; + enrolled: number; not_started: number; - absent?: number; + absent: number; in_progress: number; submitted: number; expired: number; - graded?: number; + graded: number; }; }; diff --git a/src/components/exams/answer-sheet-view.tsx b/src/components/exams/answer-sheet-view.tsx index b7795e7..748671f 100644 --- a/src/components/exams/answer-sheet-view.tsx +++ b/src/components/exams/answer-sheet-view.tsx @@ -1,5 +1,7 @@ import { For, Show, Suspense, createResource } from "solid-js"; +import { getExamQuestions } from "@/api/getExamQuestions"; import { getStudentAnswers } from "@/api/getStudentAnswers"; +import type { ExamQuestion, StudentAnswer } from "@/api/types"; import { Badge } from "@/components/ui/badge"; import { PageSpinner } from "@/components/ui/page-spinner"; import { personLabelWithId } from "@/lib/person"; @@ -9,7 +11,17 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { const t = useT(); const [sheet] = createResource( () => [props.examId, props.userId] as const, - async ([examId, userId]) => getStudentAnswers(examId, userId), + async ([examId, userId]) => { + const [answers, questions] = await Promise.all([getStudentAnswers(examId, userId), getExamQuestions(examId)]); + const byId = new Map(questions.items.map((question) => [question.id, question])); + return { + ...answers, + rows: answers.answers.flatMap((answer) => { + const question = byId.get(answer.question); + return question ? [answerRow(answer, question)] : []; + }), + }; + }, ); return ( @@ -24,7 +36,7 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { - + {(answer, idx) => (
@@ -87,3 +99,20 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { ); } + +function answerRow(answer: StudentAnswer, question: ExamQuestion) { + const isChoice = question.kind === "choice"; + const possible = isChoice ? question.points : 0; + return { + question_id: answer.question, + text: question.text, + kind: question.kind, + points: question.points, + choices: question.choices, + correct: question.correct, + selected: answer.selected, + text_answer: answer.text, + is_correct: answer.is_correct, + auto_score: { earned: answer.is_correct ? question.points : 0, possible }, + }; +} diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index 9c79fc3..e700657 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -89,17 +89,7 @@ export function ExamRoomWS(props: { exam: Exam }) { setAttempt((prev) => prev ? { ...prev, status: msg.status as any, deadline: msg.deadline, remaining_ms: msg.remaining_ms, answered: msg.answered, question_count: msg.question_count, now: msg.now } - : { - id: undefined, - exam: props.exam.id, - status: msg.status as any, - deadline: msg.deadline, - remaining_ms: msg.remaining_ms, - mark: null, - answered: msg.answered, - question_count: msg.question_count, - now: msg.now, - }, + : prev, ); setRemainingMs(msg.remaining_ms); break; diff --git a/src/components/ui/date-picker.tsx b/src/components/ui/date-picker.tsx index 2796265..c4fb286 100644 --- a/src/components/ui/date-picker.tsx +++ b/src/components/ui/date-picker.tsx @@ -1,8 +1,10 @@ import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js"; +import { Portal } from "solid-js/web"; import { Button } from "@/components/ui/button"; import { IconCalendar, IconChevronLeft, IconChevronRight } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { cn } from "@/lib/cn"; +import { usePreferences } from "@/stores/preferences-context"; function pad(value: number): string { return String(value).padStart(2, "0"); @@ -47,15 +49,29 @@ export function DatePicker(props: { class?: string; onChange: (value: string) => void; }) { + const { locale } = usePreferences(); let root: HTMLDivElement | undefined; + let panel: HTMLDivElement | undefined; const [open, setOpen] = createSignal(false); const [position, setPosition] = createSignal({ left: 0, top: 0, width: 288 }); const selected = createMemo(() => parseDate(props.value)); const [month, setMonth] = createSignal(selected() ?? new Date()); const days = createMemo(() => monthDays(month())); const monthLabel = createMemo(() => - new Intl.DateTimeFormat(undefined, { month: "long", year: "numeric" }).format(month()), + new Intl.DateTimeFormat(locale(), { month: "long", year: "numeric" }).format(month()), ); + const selectedDayLabel = createMemo(() => { + const date = selected(); + return date ? new Intl.DateTimeFormat(locale(), { weekday: "long" }).format(date) : ""; + }); + const dayHeaders = createMemo(() => { + const base = new Date(2024, 0, 1); + return Array.from({ length: 7 }, (_, i) => { + const d = new Date(base); + d.setDate(base.getDate() + i); + return new Intl.DateTimeFormat(locale(), { weekday: "short" }).format(d); + }); + }); createEffect(() => { const date = selected(); @@ -81,7 +97,8 @@ export function DatePicker(props: { }; updatePosition(); const closeOnOutside = (event: MouseEvent) => { - if (root && !root.contains(event.target as Node)) setOpen(false); + const target = event.target as Node; + if (root && !root.contains(target) && panel && !panel.contains(target)) setOpen(false); }; window.addEventListener("resize", updatePosition); window.addEventListener("scroll", updatePosition, true); @@ -119,49 +136,49 @@ export function DatePicker(props: { />
+ + {(day) =>

{day()}

} +
-
-
- -

{monthLabel()}

- -
-
- Pzt - Sal - Çar - Per - Cum - Cmt - Paz -
-
- - {(date) => ( - }> - {(day) => ( - - )} - - )} - + +
+
+ +

{monthLabel()}

+ +
+
+ {(day) => {day}} +
+
+ + {(date) => ( + }> + {(day) => ( + + )} + + )} + +
-
+
); diff --git a/src/lib/create-now.ts b/src/lib/create-now.ts index 26913e1..c775e0f 100644 --- a/src/lib/create-now.ts +++ b/src/lib/create-now.ts @@ -1,7 +1,7 @@ import { createSignal, onCleanup } from "solid-js"; import { getTime } from "@/api/getTime"; -export function createNow(intervalMs = 1000) { +export function createNow(intervalMs = 30000) { let offset = 0; const serverNow = () => Date.now() + offset; const [now, setNow] = createSignal(serverNow()); diff --git a/src/lib/exam-weight.ts b/src/lib/exam-weight.ts index 3b48c35..f601d85 100644 --- a/src/lib/exam-weight.ts +++ b/src/lib/exam-weight.ts @@ -1,9 +1,6 @@ type ExamWeightSource = { kind?: string | null; weight?: number | null; - kind_weight?: number | null; - type_weight?: number | null; - exam_type_weight?: number | null; }; type KindWeight = { @@ -15,7 +12,7 @@ export function examWeight( source: ExamWeightSource, kinds?: readonly KindWeight[] | null, ): number | null { - const direct = source.kind_weight ?? source.type_weight ?? source.exam_type_weight ?? source.weight ?? null; + const direct = source.weight ?? null; if (typeof direct === "number" && Number.isFinite(direct)) return direct; if (source.kind != null && kinds?.length) { const match = kinds.find((item) => item.name === String(source.kind)); diff --git a/src/pages/live-monitor-page.tsx b/src/pages/live-monitor-page.tsx index 693e6ab..3933cbb 100644 --- a/src/pages/live-monitor-page.tsx +++ b/src/pages/live-monitor-page.tsx @@ -84,8 +84,8 @@ function isLowRemaining(entry: LiveRosterEntry): boolean { return entry.status === "in_progress" && remaining <= 5 * 60 * 1000; } -function attemptLabel(entry: LiveRosterEntry): string { - if (entry.attempts_used != null && entry.max_attempts != null) return `${entry.attempts_used} / ${entry.max_attempts}`; +function attemptLabel(entry: LiveRosterEntry, maxAttempts: number | null): string { + if (entry.attempts_used != null && maxAttempts != null) return `${entry.attempts_used} / ${maxAttempts}`; if (entry.attempt != null) return String(entry.attempt); if (entry.attempts_used != null) return String(entry.attempts_used); return "—"; @@ -246,7 +246,7 @@ function LiveMonitorContent() { accessorFn: (entry) => entry.attempts_used ?? entry.attempt ?? 0, header: t("attempt.attempt"), meta: { headerClass: "text-center", cellClass: "text-center tabular-nums" }, - cell: (cell) => attemptLabel(cell.row.original), + cell: (cell) => attemptLabel(cell.row.original, m.exam.max_attempts), }, { id: "progress",