diff --git a/src/api/client.ts b/src/api/client.ts index 437490e..63867d1 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -1,3 +1,5 @@ +import type { Locale } from "@/i18n/messages"; + export class ApiError extends Error { readonly status: number; readonly retryAfter: number | null; @@ -18,6 +20,44 @@ type RequestOptions = { const API_PREFIX = "/api"; +const API_ERROR_MESSAGES: Record> = { + "not enrolled in this exam's course": { + en: "You are not enrolled in this exam's course.", + tr: "Bu sınavın dersine kayıtlı değilsin.", + }, + "you are not enrolled in this exam's course": { + en: "You are not enrolled in this exam's course.", + tr: "Bu sınavın dersine kayıtlı değilsin.", + }, + "exam is not scheduled": { + en: "This exam is not scheduled for online sitting.", + tr: "Bu sınav çevrim içi oturum için zamanlanmamış.", + }, + "attempt not found": { + en: "Start the exam before opening the exam room.", + tr: "Sınav odasını açmadan önce sınavı başlat.", + }, + "attempt is closed": { + en: "This attempt is closed. Answers are read-only.", + tr: "Bu oturum kapalı. Cevaplar salt okunur.", + }, +}; + +function normalizeApiMessage(message: string): string { + return message + .trim() + .replace(/[’‘]/g, "'") + .replace(/\s+/g, " ") + .replace(/[.!?]+$/, "") + .toLowerCase(); +} + +function sentenceCase(message: string): string { + const trimmed = message.trim(); + if (!trimmed) return trimmed; + return trimmed[0].toLocaleUpperCase("en-US") + trimmed.slice(1); +} + export async function client(path: string, options: RequestOptions = {}): Promise { const headers: Record = {}; let body: string | undefined; @@ -70,13 +110,17 @@ export async function client(path: string, options: RequestOptions = {}): Pro return data as T; } -export function formatApiError(err: unknown): string { +export function formatApiErrorMessage(message: string, locale: Locale = "en"): string { + return API_ERROR_MESSAGES[normalizeApiMessage(message)]?.[locale] ?? sentenceCase(message); +} + +export function formatApiError(err: unknown, locale: Locale = "en"): string { if (err instanceof ApiError) { if (err.status === 429 && err.retryAfter != null) { - return `Try again in ${err.retryAfter}s`; + return locale === "tr" ? `${err.retryAfter} sn sonra tekrar dene.` : `Try again in ${err.retryAfter}s.`; } - return err.message; + return formatApiErrorMessage(err.message, locale); } - if (err instanceof Error) return err.message; - return "Something went wrong"; + if (err instanceof Error) return formatApiErrorMessage(err.message, locale); + return locale === "tr" ? "Bir şeyler ters gitti." : "Something went wrong."; } diff --git a/src/components/exams/exam-card.tsx b/src/components/exams/exam-card.tsx index ee980d6..683aadd 100644 --- a/src/components/exams/exam-card.tsx +++ b/src/components/exams/exam-card.tsx @@ -17,6 +17,7 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number }; const status = () => { + if (props.exam.mode !== "sync" && props.exam.mode !== "async") return "unscheduled"; 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"; @@ -25,6 +26,7 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number const statusLabel = () => { const s = status(); + if (s === "unscheduled") return t("exams.unscheduled"); if (s === "finished") return t("exams.finished"); if (s === "upcoming") return t("exams.upcoming"); return t("exams.active"); @@ -32,6 +34,7 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number const statusClass = () => { const s = status(); + if (s === "unscheduled") return "bg-muted text-muted-foreground border-muted"; if (s === "finished") return "bg-muted text-muted-foreground border-muted"; if (s === "upcoming") return "bg-amber-500/15 text-amber-600 border-amber-500/30"; return "bg-emerald-500/15 text-emerald-600 border-emerald-500/30"; @@ -39,6 +42,7 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number const statusDot = () => { const s = status(); + if (s === "unscheduled") return "bg-muted-foreground"; if (s === "finished") return "bg-muted-foreground"; if (s === "upcoming") return "bg-amber-600"; return "bg-emerald-600"; @@ -71,13 +75,15 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number {props.exam.description || "—"}

-
-
{t("exams.mode")}
-
{modeLabel()}
-
-
-
{t("exams.window")}
-
{formatDateTime(props.exam.starts_at, locale())} → {formatDateTime(props.exam.ends_at, locale())}
+
+ + {modeLabel()} + + + + {formatDateTime(props.exam.starts_at, locale())} → {formatDateTime(props.exam.ends_at, locale())} + +
{(props.exam.mode === "sync" || props.exam.mode === "async") && (
diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index 7441356..e2dbe1c 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -4,7 +4,7 @@ import { getExamAttemptQuestions } from "@/api/getExamAttemptQuestions"; import { postExamAttempt } from "@/api/postExamAttempt"; import { postExamAttemptAnswer } from "@/api/postExamAttemptAnswer"; import { postExamAttemptFinish } from "@/api/postExamAttemptFinish"; -import { formatApiError } from "@/api/client"; +import { formatApiError, formatApiErrorMessage } from "@/api/client"; import type { AttemptQuestion, Exam, ExamAttempt } from "@/api/types"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; @@ -34,6 +34,7 @@ function formatRemaining(ms: number): string { export function ExamRoomWS(props: { exam: Exam }) { const t = useT(); + const { locale } = usePreferences(); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); const [finishOpen, setFinishOpen] = createSignal(false); @@ -123,7 +124,7 @@ export function ExamRoomWS(props: { exam: Exam }) { break; } case "error": { - setError(msg.message); + setError(formatApiErrorMessage(msg.message, locale())); break; } } @@ -146,7 +147,7 @@ export function ExamRoomWS(props: { exam: Exam }) { setQuestions(qs); connectWs(); } catch (err) { - setError(formatApiError(err)); + setError(formatApiError(err, locale())); } finally { setPending(false); } @@ -161,7 +162,7 @@ export function ExamRoomWS(props: { exam: Exam }) { setQuestions(qs); connectWs(); } catch (err) { - setError(formatApiError(err)); + setError(formatApiError(err, locale())); } }; @@ -191,7 +192,7 @@ export function ExamRoomWS(props: { exam: Exam }) { const qs = await getExamAttemptQuestions(props.exam.id); setQuestions(qs); } catch (err) { - setError(formatApiError(err)); + setError(formatApiError(err, locale())); } finally { setPending(false); } @@ -210,7 +211,7 @@ export function ExamRoomWS(props: { exam: Exam }) { setAttempt(next); } } catch (err) { - setError(formatApiError(err)); + setError(formatApiError(err, locale())); } finally { setPending(false); } @@ -286,13 +287,22 @@ export function ExamRoomWS(props: { exam: Exam }) {