From bae952564a99658d069fb5467bad055e352f997f 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 07:55:01 +0300
Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=94=84=20extract=20shared=20e?=
=?UTF-8?q?xam=20status=20logic,=20add=20max=5Fattempts,=20unify=20colors?=
=?UTF-8?q?=20-=20new=20src/lib/exam-status.ts:=20examDisplayStatus,=20exa?=
=?UTF-8?q?mStatusTone,=20isSittableExam,=20=20=20ExamAttemptSummary,=20Ex?=
=?UTF-8?q?amDisplayStatus=20(adds=20no=5Fattempts=5Fleft)=20-=20exams-pag?=
=?UTF-8?q?e=20/=20exam-card=20/=20exam-detail-page=20/=20dashboard:=20use?=
=?UTF-8?q?=20shared=20helpers=20-=20dashboard=20+=20exams-page:=20poll=20?=
=?UTF-8?q?attempt=20statuses=20with=20max=5Fattempts=20tracking=20-=20det?=
=?UTF-8?q?ail=20page:=20replace=20draft=20metric=20card=20with=20max=5Fat?=
=?UTF-8?q?tempts=20-=20live-monitor:=20badge=20uses=20schedule-status=20c?=
=?UTF-8?q?olors=20(dot=20+=20semantic=20border)=20-=20i18n:=20attempt.noA?=
=?UTF-8?q?ttemptsLeft=20(EN/TR)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/components/exams/exam-card.tsx | 16 +++++------
src/i18n/messages.ts | 3 ++
src/lib/exam-status.ts | 30 ++++++++++++++++++++
src/pages/dashboard-page.tsx | 39 ++++++++++++++++++++------
src/pages/exam-detail-page.tsx | 31 +++++++++++---------
src/pages/exams-page.tsx | 45 +++++++++++-------------------
src/pages/live-monitor-page.tsx | 18 ++++++++----
7 files changed, 119 insertions(+), 63 deletions(-)
create mode 100644 src/lib/exam-status.ts
diff --git a/src/components/exams/exam-card.tsx b/src/components/exams/exam-card.tsx
index ffd94b2..d833cdd 100644
--- a/src/components/exams/exam-card.tsx
+++ b/src/components/exams/exam-card.tsx
@@ -5,6 +5,7 @@ import { Badge } from "@/components/ui/badge";
import { ExamLink } from "@/components/exams/exam-link";
import { cn } from "@/lib/cn";
import { examKindLabel } from "@/lib/exam-labels";
+import { examDisplayStatus, examStatusTone } from "@/lib/exam-status";
import { examWeight } from "@/lib/exam-weight";
import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format";
import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status";
@@ -22,17 +23,16 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number
};
const status = () => {
- if (props.exam.mode !== "sync" && props.exam.mode !== "async" && props.exam.mode !== "open") return "unscheduled";
- if (props.exam.mode === "open") return "active";
- const current = props.now ?? Date.now();
- if (props.exam.ends_at != null && props.exam.ends_at < current) return "finished";
- if (props.exam.starts_at != null && props.exam.starts_at > current) return "upcoming";
- return "active";
+ return examDisplayStatus(props.exam, props.now ?? Date.now());
};
const statusLabel = () => {
const s = status();
+ if (s === "draft") return t("exams.draft");
if (s === "unscheduled") return t("exams.unscheduled");
+ if (s === "submitted") return t("attempt.submitted");
+ if (s === "expired") return t("attempt.expired");
+ if (s === "no_attempts_left") return t("attempt.noAttemptsLeft");
if (s === "finished") return t("exams.finished");
if (s === "upcoming") return t("exams.upcoming");
return t("exams.active");
@@ -51,8 +51,8 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number
{props.exam.title}
-
-
+
+
{statusLabel()}
diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts
index 5921226..2d07b7e 100644
--- a/src/i18n/messages.ts
+++ b/src/i18n/messages.ts
@@ -372,6 +372,7 @@ export type MessageKey =
| "attempt.submittedInfo"
| "attempt.expired"
| "attempt.expiredInfo"
+ | "attempt.noAttemptsLeft"
| "attempt.closed"
| "attempt.inProgress"
| "attempt.absent"
@@ -983,6 +984,7 @@ const en: Dict = {
"attempt.submittedInfo": "You have already submitted this exam. It cannot be reopened.",
"attempt.expired": "Expired",
"attempt.expiredInfo": "The time for this exam has expired. It cannot be reopened.",
+ "attempt.noAttemptsLeft": "No attempts left",
"attempt.closed": "This attempt is closed. Answers are read-only.",
"attempt.inProgress": "In progress",
"attempt.absent": "No-show",
@@ -1604,6 +1606,7 @@ const tr: Dict = {
"attempt.submittedInfo": "Bu sınavı zaten teslim ettin. Yeniden açılamaz.",
"attempt.expired": "Süresi doldu",
"attempt.expiredInfo": "Bu sınavın süresi doldu. Yeniden açılamaz.",
+ "attempt.noAttemptsLeft": "Deneme hakkı bitti",
"attempt.closed": "Bu oturum kapalı. Cevaplar salt okunur.",
"attempt.inProgress": "Devam ediyor",
"attempt.absent": "Katılmadı",
diff --git a/src/lib/exam-status.ts b/src/lib/exam-status.ts
new file mode 100644
index 0000000..fafebd5
--- /dev/null
+++ b/src/lib/exam-status.ts
@@ -0,0 +1,30 @@
+import type { AttemptStatus, Exam } from "@/api/types";
+
+export type ExamAttemptSummary = {
+ status: AttemptStatus | "not_started";
+ attempts_used: number;
+ max_attempts: number;
+};
+
+export type ExamDisplayStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished" | "submitted" | "expired" | "no_attempts_left";
+
+export function isSittableExam(exam: Exam): boolean {
+ return exam.mode === "sync" || exam.mode === "async" || exam.mode === "open";
+}
+
+export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): ExamDisplayStatus {
+ if (attempt?.status === "submitted" || attempt?.status === "expired") return attempt.status;
+ if (attempt && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts) return "no_attempts_left";
+ if (exam.draft) return "draft";
+ if (!isSittableExam(exam)) return "unscheduled";
+ if (exam.mode === "open") return "active";
+ if (exam.ends_at != null && exam.ends_at < now) return "finished";
+ if (exam.starts_at != null && exam.starts_at > now) return "upcoming";
+ return "active";
+}
+
+export function examStatusTone(status: ExamDisplayStatus): string {
+ if (status === "submitted") return "submitted";
+ if (status === "expired" || status === "no_attempts_left") return "finished";
+ return status;
+}
diff --git a/src/pages/dashboard-page.tsx b/src/pages/dashboard-page.tsx
index 8eb41e6..81f3cf6 100644
--- a/src/pages/dashboard-page.tsx
+++ b/src/pages/dashboard-page.tsx
@@ -1,7 +1,8 @@
-import { For, Show, createEffect, createMemo, createResource, createSignal, type Component } from "solid-js";
+import { For, Show, createEffect, createMemo, createResource, createSignal, onCleanup, type Component } from "solid-js";
import { Link } from "@tanstack/solid-router";
import { formatApiError } from "@/api/client";
import { getCourses } from "@/api/getCourses";
+import { getExamAttempt } from "@/api/getExamAttempt";
import { getEvents } from "@/api/getEvents";
import { getExams } from "@/api/getExams";
import { getMyCourses } from "@/api/getMyCourses";
@@ -29,6 +30,7 @@ import type { MessageKey } from "@/i18n/messages";
import { cn } from "@/lib/cn";
import { createNow } from "@/lib/create-now";
import { examKindLabel } from "@/lib/exam-labels";
+import { examDisplayStatus, isSittableExam, type ExamAttemptSummary } from "@/lib/exam-status";
import { formatDateTime } from "@/lib/format";
import { hasMinRole } from "@/lib/roles";
import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status";
@@ -68,17 +70,17 @@ const ROLE_KEY: Record = {
admin: "role.admin",
};
-function examWindow(exam: Exam, now: number): AttentionKind | "upcoming" | "past" | "unscheduled" {
- if (exam.mode === "open") return "active";
- if (exam.mode !== "sync" && exam.mode !== "async") return "unscheduled";
- if (exam.ends_at != null && exam.ends_at < now) return "past";
+function examWindow(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): AttentionKind | "upcoming" | "past" | "unscheduled" {
+ const status = examDisplayStatus(exam, now, attempt);
+ if (status === "active") return "active";
+ if (status !== "upcoming") return status === "unscheduled" ? "unscheduled" : "past";
if (exam.starts_at != null && exam.starts_at > now) {
const delta = exam.starts_at - now;
if (delta <= 24 * 60 * 60 * 1000) return "today";
if (delta <= WEEK_MS) return "soon";
return "upcoming";
}
- return "active";
+ return "upcoming";
}
function eventWindow(event: Event, now: number): AttentionKind | "upcoming" | "past" | "unscheduled" {
@@ -114,6 +116,7 @@ function DashboardContent() {
const [draggingPortal, setDraggingPortal] = createSignal(null);
const [dragOverPortal, setDragOverPortal] = createSignal(null);
const [editingPortalOrder, setEditingPortalOrder] = createSignal(false);
+ const [attemptStatuses, setAttemptStatuses] = createSignal>({});
const [courses] = createResource(
() => (role() !== "student" ? true : null),
@@ -173,6 +176,26 @@ function DashboardContent() {
return all;
});
+ createEffect(() => {
+ if (role() !== "student") return;
+ const exams = visibleExams();
+ const refresh = () => {
+ for (const exam of exams) {
+ if (!isSittableExam(exam)) continue;
+ const current = attemptStatuses()[exam.id];
+ if (current?.status === "submitted" || current?.status === "expired") continue;
+ if (current && current.max_attempts > 0 && current.attempts_used >= current.max_attempts) continue;
+ if (current != null && current.status !== "in_progress") continue;
+ void getExamAttempt(exam.id)
+ .then((attempt) => setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } })))
+ .catch(() => setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: "not_started", attempts_used: 0, max_attempts: exam.max_attempts } })));
+ }
+ };
+ refresh();
+ const interval = window.setInterval(refresh, 5000);
+ onCleanup(() => window.clearInterval(interval));
+ });
+
const countCoursesByKind = (kind: Course["kind"]) => scopedCourses().filter((course) => course.kind === kind).length;
const courseCount = createMemo(() => countCoursesByKind("course"));
const studyCount = createMemo(() => countCoursesByKind("study"));
@@ -297,7 +320,7 @@ function DashboardContent() {
const items: AttentionItem[] = [];
for (const exam of visibleExams()) {
- const status = examWindow(exam, n);
+ const status = examWindow(exam, n, attemptStatuses()[exam.id]);
if (status === "active" || status === "today" || status === "soon") {
items.push({
id: exam.id,
@@ -342,7 +365,7 @@ function DashboardContent() {
for (const exam of visibleExams()) {
if (exam.starts_at != null && exam.starts_at > n && exam.starts_at <= n + WEEK_MS) {
- const w = examWindow(exam, n);
+ const w = examWindow(exam, n, attemptStatuses()[exam.id]);
if (w === "upcoming" || w === "soon" || w === "today") {
items.push({
id: exam.id,
diff --git a/src/pages/exam-detail-page.tsx b/src/pages/exam-detail-page.tsx
index bc1af55..3cfacd9 100644
--- a/src/pages/exam-detail-page.tsx
+++ b/src/pages/exam-detail-page.tsx
@@ -14,7 +14,7 @@ import { getSettings } from "@/api/getSettings";
import { patchExamById } from "@/api/patchExamById";
import { postExamResult } from "@/api/postExamResult";
import { ApiError, formatApiError } from "@/api/client";
-import type { AttemptStatus, ExamResult } from "@/api/types";
+import type { ExamResult } from "@/api/types";
import { ExamForm } from "@/components/exams/exam-form";
import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel";
import { AnswerSheetView } from "@/components/exams/answer-sheet-view";
@@ -35,6 +35,7 @@ import { TableRowActions } from "@/components/ui/table-row-actions";
import { hasMinRole } from "@/lib/roles";
import { createNow } from "@/lib/create-now";
import { examKindLabel } from "@/lib/exam-labels";
+import { examDisplayStatus, examStatusTone, isSittableExam, type ExamAttemptSummary, type ExamDisplayStatus } from "@/lib/exam-status";
import { examWeight } from "@/lib/exam-weight";
import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format";
import { personId, personLabel, personLabelWithId } from "@/lib/person";
@@ -92,7 +93,10 @@ function ExamDetailContent() {
questions: false,
results: false,
});
- const isSittable = () => exam()?.mode === "sync" || exam()?.mode === "async" || exam()?.mode === "open";
+ const isSittable = () => {
+ const e = exam();
+ return e ? isSittableExam(e) : false;
+ };
const isScheduled = () => isSittable();
const [ownResult] = createResource(
@@ -157,16 +161,17 @@ function ExamDetailContent() {
};
const isFinished = () => examStatus().finished;
const isUpcoming = () => examStatus().upcoming;
- const ownAttemptStatus = (): AttemptStatus | null => ownAttempt()?.status ?? null;
- const detailStatus = () => {
- const own = ownAttemptStatus();
- if (own === "submitted" || own === "expired") return own;
- return !isScheduled() ? "unscheduled" : isFinished() ? "finished" : isUpcoming() ? "upcoming" : "active";
+ const ownAttemptSummary = (): ExamAttemptSummary | null => {
+ const attempt = ownAttempt();
+ return attempt ? { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } : null;
};
+ const noAttemptsLeft = () => detailStatus() === "no_attempts_left";
+ const detailStatus = (): ExamDisplayStatus => exam() ? examDisplayStatus(exam()!, now(), ownAttemptSummary()) : "unscheduled";
const detailStatusLabel = () => {
const status = detailStatus();
if (status === "submitted") return t("attempt.submitted");
if (status === "expired") return t("attempt.expired");
+ if (status === "no_attempts_left") return t("attempt.noAttemptsLeft");
if (status === "unscheduled") return t("exams.unscheduled");
if (status === "finished") return t("exams.finished");
if (status === "upcoming") return t("exams.upcoming");
@@ -301,7 +306,7 @@ function ExamDetailContent() {
-
@@ -347,10 +352,10 @@ function ExamDetailContent() {
variant="outline"
class={cn(
"mt-2 w-fit rounded-sm capitalize",
- scheduleStatusClass(detailStatus() === "expired" ? "finished" : detailStatus()),
+ scheduleStatusClass(examStatusTone(detailStatus())),
)}
>
-
+
{detailStatusLabel()}
@@ -366,8 +371,8 @@ function ExamDetailContent() {
{examModeLabel(ex().mode)}
-
{t("exams.draft")}
-
{ex().draft ? t("exams.draft") : "—"}
+
{t("exams.maxAttempts")}
+
{ex().max_attempts}
diff --git a/src/pages/exams-page.tsx b/src/pages/exams-page.tsx
index c313450..a90525a 100644
--- a/src/pages/exams-page.tsx
+++ b/src/pages/exams-page.tsx
@@ -8,7 +8,7 @@ import { getMyCourses } from "@/api/getMyCourses";
import { patchExamById } from "@/api/patchExamById";
import { postCourseExam } from "@/api/postCourseExam";
import { formatApiError } from "@/api/client";
-import type { AttemptStatus, Course, Exam } from "@/api/types";
+import type { Course, Exam } from "@/api/types";
import { ExamForm, type ExamFormValues } from "@/components/exams/exam-form";
import { RouteGuard } from "@/components/layout/route-guard";
import { PageHeader } from "@/components/layout/page-header";
@@ -22,6 +22,7 @@ import { SidePanel } from "@/components/ui/side-panel";
import { TableRowActions } from "@/components/ui/table-row-actions";
import { createNow } from "@/lib/create-now";
import { examKindLabel } from "@/lib/exam-labels";
+import { examDisplayStatus, examStatusTone, isSittableExam, type ExamAttemptSummary, type ExamDisplayStatus } from "@/lib/exam-status";
import { createFlash } from "@/lib/flash";
import { formatDateTime } from "@/lib/format";
import { hasMinRole } from "@/lib/roles";
@@ -32,8 +33,7 @@ import { usePreferences, useT } from "@/stores/preferences-context";
const EXAM_PAGE_SIZE = 12;
-type ExamStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished" | "submitted" | "expired";
-type ExamRow = Exam & { displayStatus: ExamStatus };
+type ExamRow = Exam & { displayStatus: ExamDisplayStatus };
export default function ExamsPage() {
return (
@@ -49,13 +49,13 @@ function ExamsContent() {
const t = useT();
const { locale } = usePreferences();
const now = createNow();
- const [statusFilter, setStatusFilter] = createSignal("all");
+ const [statusFilter, setStatusFilter] = createSignal("all");
const [courseFilter, setCourseFilter] = createSignal("all");
const [showMoreFilters, setShowMoreFilters] = createSignal(false);
const [createOpen, setCreateOpen] = createSignal(false);
const [selectedCourseId, setSelectedCourseId] = createSignal("");
const [editingExam, setEditingExam] = createSignal(null);
- const [attemptStatuses, setAttemptStatuses] = createSignal>({});
+ const [attemptStatuses, setAttemptStatuses] = createSignal>({});
const [flash, setFlash] = createFlash();
const [courses] = createResource(
@@ -78,18 +78,7 @@ function ExamsContent() {
const courseById = createMemo(() => new Map(visibleCourses().map((course) => [course.id, course])));
const courseTitle = (courseId: string) => courseById().get(courseId)?.title ?? courseId;
- const isSittable = (exam: Exam) => exam.mode === "sync" || exam.mode === "async" || exam.mode === "open";
- const examStatus = (exam: Exam): ExamStatus => {
- const own = attemptStatuses()[exam.id];
- if (own === "submitted" || own === "expired") return own;
- if (exam.draft) return "draft";
- if (!isSittable(exam)) return "unscheduled";
- if (exam.mode === "open") return "active";
- const current = now();
- if (exam.ends_at != null && exam.ends_at < current) return "finished";
- if (exam.starts_at != null && exam.starts_at > current) return "upcoming";
- return "active";
- };
+ const examStatus = (exam: Exam) => examDisplayStatus(exam, now(), attemptStatuses()[exam.id]);
const filterExams = (items: Exam[]) => {
const allowed = isStudent() ? new Set(visibleCourses().map((course) => course.id)) : null;
@@ -120,17 +109,16 @@ function ExamsContent() {
setSelectedCourseId(manageableCourses()[0]?.id ?? "");
});
- const statusLabel = (status: ExamStatus) => {
+ const statusLabel = (status: ExamDisplayStatus) => {
if (status === "submitted") return t("attempt.submitted");
if (status === "expired") return t("attempt.expired");
+ if (status === "no_attempts_left") return t("attempt.noAttemptsLeft");
if (status === "draft") return t("exams.draft");
if (status === "unscheduled") return t("exams.unscheduled");
if (status === "finished") return t("exams.finished");
if (status === "upcoming") return t("exams.upcoming");
return t("exams.active");
};
- const statusTone = (status: ExamStatus) => status === "expired" ? "finished" : status;
-
const rows = (): ExamRow[] => filterExams(list() ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) }));
createEffect(() => {
if (!isStudent()) return;
@@ -139,16 +127,17 @@ function ExamsContent() {
const allowed = new Set(visibleCourses().map((course) => course.id));
const refresh = () => {
for (const exam of exams) {
- if (!allowed.has(exam.course) || !isSittable(exam)) continue;
+ if (!allowed.has(exam.course) || !isSittableExam(exam)) continue;
const current = attemptStatuses()[exam.id];
- if (current === "submitted" || current === "expired") continue;
- if (current != null && current !== "in_progress") continue;
+ if (current?.status === "submitted" || current?.status === "expired") continue;
+ if (current && current.max_attempts > 0 && current.attempts_used >= current.max_attempts) continue;
+ if (current != null && current.status !== "in_progress") continue;
void getExamAttempt(exam.id)
.then((attempt) => {
- setAttemptStatuses((prev) => ({ ...prev, [exam.id]: attempt.status }));
+ setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } }));
})
.catch(() => {
- setAttemptStatuses((prev) => ({ ...prev, [exam.id]: "not_started" }));
+ setAttemptStatuses((prev) => ({ ...prev, [exam.id]: { status: "not_started", attempts_used: 0, max_attempts: exam.max_attempts } }));
});
}
};
@@ -188,8 +177,8 @@ function ExamsContent() {
cell: (cell) => {
const status = cell.row.original.displayStatus;
return (
-
-
+
+
{statusLabel(status)}
);
@@ -282,7 +271,7 @@ function ExamsContent() {
empty={t("exams.empty")}
filters={
<>
-