✨ add terms management functionality with CRUD operations and UI integration
Browse files at this commit1b1abf733355d07bf6bd7bcd0b071f7149d17e03Parent: 53c1ba4- feat
- ✨ add terms management functionality with CRUD operations and UI integration
14 changed files +320 -4
- docs/backend-ui-alignment-plan.md +2 -2
- src/api/deleteTermById.ts +5 -0
- src/api/getTerms.ts +6 -0
- src/api/patchCourseById.ts +1 -0
- src/api/patchTermById.ts +12 -0
- src/api/postCourse.ts +1 -0
- src/api/postTerm.ts +12 -0
- src/api/types.ts +7 -0
- src/components/layout/side-nav.tsx +1 -0
- src/i18n/messages.ts +28 -1
- src/pages/course-detail-page.tsx +24 -1
- src/pages/courses-page.tsx +20 -0
- src/pages/terms-page.tsx +193 -0
- src/routes/router.tsx +8 -0
docs/backend-ui-alignment-plan.mdmodified+2 -2
| @@ -33,13 +33,13 @@ This plan covers the remaining backend features that need simple, low-complexity | |||
| 33 | - Commit 1 complete in UI: live monitor shows absent count/status, attempt usage, and left timestamp; exam room summary shows attempt usage and left timestamp. | 33 | - Commit 1 complete in UI: live monitor shows absent count/status, attempt usage, and left timestamp; exam room summary shows attempt usage and left timestamp. |
| 34 | - Commit 2 complete in UI: report card types accept grade-band fields and marks show `mark / grade` when backend sends labels. | 34 | - Commit 2 complete in UI: report card types accept grade-band fields and marks show `mark / grade` when backend sends labels. |
| 35 | - Commit 3 complete in UI: manager settings page edits exam kinds, attendance statuses, and grade bands through `PATCH /settings`. | 35 | - Commit 3 complete in UI: manager settings page edits exam kinds, attendance statuses, and grade bands through `PATCH /settings`. |
| 36 | -- Commits 4-7 remain. | 36 | +- Commit 4 complete in UI: manager terms page edits terms, and course create/edit can assign an optional term. |
| 37 | +- Commits 5-7 remain. | ||
| 37 | 38 | ||
| 38 | ## Remaining Commit Order | 39 | ## Remaining Commit Order |
| 39 | 40 | ||
| 40 | | Commit | Scope | Priority | | 41 | | Commit | Scope | Priority | |
| 41 | | --- | --- | --- | | 42 | | --- | --- | --- | |
| 42 | -| 4 | Terms API + course term select | Medium | | ||
| 43 | | 5 | Course sessions + roll call | Medium | | 43 | | 5 | Course sessions + roll call | Medium | |
| 44 | | 6 | Attendance report page | Medium | | 44 | | 6 | Attendance report page | Medium | |
| 45 | | 7 | Work log page | Medium | | 45 | | 7 | Work log page | Medium | |
src/api/deleteTermById.tsadded+5 -0
| @@ -0,0 +1,5 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | + | ||
| 3 | +export function deleteTermById(id: string): Promise<void> { | ||
| 4 | + return client<void>(`/terms/${id}`, { method: "DELETE" }); | ||
| 5 | +} | ||
src/api/getTerms.tsadded+6 -0
| @@ -0,0 +1,6 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import type { Term } from "./types"; | ||
| 3 | + | ||
| 4 | +export function getTerms(signal?: AbortSignal): Promise<Term[]> { | ||
| 5 | + return client<Term[]>("/terms", { signal }); | ||
| 6 | +} | ||
src/api/patchCourseById.tsmodified+1 -0
| @@ -4,6 +4,7 @@ import type { Course } from "./types"; | |||
| 4 | export type PatchCourseBody = { | 4 | export type PatchCourseBody = { |
| 5 | title?: string | null; | 5 | title?: string | null; |
| 6 | description?: string | null; | 6 | description?: string | null; |
| 7 | + term_id?: string | null; | ||
| 7 | }; | 8 | }; |
| 8 | 9 | ||
| 9 | export function patchCourseById(id: string, body: PatchCourseBody): Promise<Course> { | 10 | export function patchCourseById(id: string, body: PatchCourseBody): Promise<Course> { |
src/api/patchTermById.tsadded+12 -0
| @@ -0,0 +1,12 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import type { Term } from "./types"; | ||
| 3 | + | ||
| 4 | +export type PatchTermBody = { | ||
| 5 | + name?: string | null; | ||
| 6 | + starts_at?: number | null; | ||
| 7 | + ends_at?: number | null; | ||
| 8 | +}; | ||
| 9 | + | ||
| 10 | +export function patchTermById(id: string, body: PatchTermBody): Promise<Term> { | ||
| 11 | + return client<Term>(`/terms/${id}`, { method: "PATCH", body }); | ||
| 12 | +} | ||
src/api/postCourse.tsmodified+1 -0
| @@ -4,6 +4,7 @@ import type { Course } from "./types"; | |||
| 4 | export type PostCourseBody = { | 4 | export type PostCourseBody = { |
| 5 | title: string; | 5 | title: string; |
| 6 | description?: string | null; | 6 | description?: string | null; |
| 7 | + term_id?: string | null; | ||
| 7 | }; | 8 | }; |
| 8 | 9 | ||
| 9 | export function postCourse(body: PostCourseBody): Promise<Course> { | 10 | export function postCourse(body: PostCourseBody): Promise<Course> { |
src/api/postTerm.tsadded+12 -0
| @@ -0,0 +1,12 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import type { Term } from "./types"; | ||
| 3 | + | ||
| 4 | +export type PostTermBody = { | ||
| 5 | + name: string; | ||
| 6 | + starts_at: number; | ||
| 7 | + ends_at: number; | ||
| 8 | +}; | ||
| 9 | + | ||
| 10 | +export function postTerm(body: PostTermBody): Promise<Term> { | ||
| 11 | + return client<Term>("/terms", { method: "POST", body }); | ||
| 12 | +} | ||
src/api/types.tsmodified+7 -0
| @@ -63,6 +63,13 @@ export type Course = { | |||
| 63 | term_id?: string | null; | 63 | term_id?: string | null; |
| 64 | }; | 64 | }; |
| 65 | 65 | ||
| 66 | +export type Term = { | ||
| 67 | + id: string; | ||
| 68 | + name: string; | ||
| 69 | + starts_at: number; | ||
| 70 | + ends_at: number; | ||
| 71 | +}; | ||
| 72 | + | ||
| 66 | export type Enrollment = { | 73 | export type Enrollment = { |
| 67 | id: string; | 74 | id: string; |
| 68 | course: string; | 75 | course: string; |
src/components/layout/side-nav.tsxmodified+1 -0
| @@ -35,6 +35,7 @@ const MAIN_ITEMS: NavItem[] = [ | |||
| 35 | { to: "/events", labelKey: "nav.events", Icon: IconCalendar }, | 35 | { to: "/events", labelKey: "nav.events", Icon: IconCalendar }, |
| 36 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, | 36 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, |
| 37 | { to: "/management/settings", labelKey: "nav.settings", Icon: IconEdit, minRole: "manager" }, | 37 | { to: "/management/settings", labelKey: "nav.settings", Icon: IconEdit, minRole: "manager" }, |
| 38 | + { to: "/management/terms", labelKey: "nav.terms", Icon: IconCalendar, minRole: "manager" }, | ||
| 38 | { to: "/admin/users", labelKey: "nav.users", Icon: IconUsers, minRole: "admin" }, | 39 | { to: "/admin/users", labelKey: "nav.users", Icon: IconUsers, minRole: "admin" }, |
| 39 | ]; | 40 | ]; |
| 40 | 41 | ||
src/i18n/messages.tsmodified+28 -1
| @@ -12,6 +12,7 @@ export type MessageKey = | |||
| 12 | | "nav.users" | 12 | | "nav.users" |
| 13 | | "nav.studentMarks" | 13 | | "nav.studentMarks" |
| 14 | | "nav.settings" | 14 | | "nav.settings" |
| 15 | + | "nav.terms" | ||
| 15 | | "nav.guide" | 16 | | "nav.guide" |
| 16 | | "nav.logout" | 17 | | "nav.logout" |
| 17 | | "nav.admin" | 18 | | "nav.admin" |
| @@ -375,7 +376,15 @@ export type MessageKey = | |||
| 375 | | "settings.status" | 376 | | "settings.status" |
| 376 | | "settings.min" | 377 | | "settings.min" |
| 377 | | "settings.label" | 378 | | "settings.label" |
| 378 | - | "settings.addRow"; | 379 | + | "settings.addRow" |
| 380 | + | "terms.title" | ||
| 381 | + | "terms.subtitle" | ||
| 382 | + | "terms.create" | ||
| 383 | + | "terms.edit" | ||
| 384 | + | "terms.empty" | ||
| 385 | + | "terms.term" | ||
| 386 | + | "terms.unassigned" | ||
| 387 | + | "terms.dateRequired"; | ||
| 379 | 388 | ||
| 380 | type Dict = Record<MessageKey, string>; | 389 | type Dict = Record<MessageKey, string>; |
| 381 | 390 | ||
| @@ -391,6 +400,7 @@ const en: Dict = { | |||
| 391 | "nav.users": "Users", | 400 | "nav.users": "Users", |
| 392 | "nav.studentMarks": "Student marks", | 401 | "nav.studentMarks": "Student marks", |
| 393 | "nav.settings": "Settings", | 402 | "nav.settings": "Settings", |
| 403 | + "nav.terms": "Terms", | ||
| 394 | "nav.guide": "Guide", | 404 | "nav.guide": "Guide", |
| 395 | "nav.admin": "Admin", | 405 | "nav.admin": "Admin", |
| 396 | "nav.logout": "Log out", | 406 | "nav.logout": "Log out", |
| @@ -769,6 +779,14 @@ const en: Dict = { | |||
| 769 | "settings.min": "Minimum", | 779 | "settings.min": "Minimum", |
| 770 | "settings.label": "Label", | 780 | "settings.label": "Label", |
| 771 | "settings.addRow": "Add row", | 781 | "settings.addRow": "Add row", |
| 782 | + "terms.title": "Academic terms", | ||
| 783 | + "terms.subtitle": "Manage calendar terms and assign courses to them.", | ||
| 784 | + "terms.create": "Create term", | ||
| 785 | + "terms.edit": "Edit term", | ||
| 786 | + "terms.empty": "No terms yet.", | ||
| 787 | + "terms.term": "Term", | ||
| 788 | + "terms.unassigned": "Unassigned", | ||
| 789 | + "terms.dateRequired": "Start and end dates are required.", | ||
| 772 | }; | 790 | }; |
| 773 | 791 | ||
| 774 | const tr: Dict = { | 792 | const tr: Dict = { |
| @@ -783,6 +801,7 @@ const tr: Dict = { | |||
| 783 | "nav.users": "Kullanıcılar", | 801 | "nav.users": "Kullanıcılar", |
| 784 | "nav.studentMarks": "Öğrenci notları", | 802 | "nav.studentMarks": "Öğrenci notları", |
| 785 | "nav.settings": "Ayarlar", | 803 | "nav.settings": "Ayarlar", |
| 804 | + "nav.terms": "Dönemler", | ||
| 786 | "nav.guide": "Rehber", | 805 | "nav.guide": "Rehber", |
| 787 | "nav.admin": "Yönetim", | 806 | "nav.admin": "Yönetim", |
| 788 | "nav.logout": "Çıkış yap", | 807 | "nav.logout": "Çıkış yap", |
| @@ -1161,6 +1180,14 @@ const tr: Dict = { | |||
| 1161 | "settings.min": "Alt sınır", | 1180 | "settings.min": "Alt sınır", |
| 1162 | "settings.label": "Etiket", | 1181 | "settings.label": "Etiket", |
| 1163 | "settings.addRow": "Satır ekle", | 1182 | "settings.addRow": "Satır ekle", |
| 1183 | + "terms.title": "Akademik dönemler", | ||
| 1184 | + "terms.subtitle": "Takvim dönemlerini yönet ve dersleri dönemlere bağla.", | ||
| 1185 | + "terms.create": "Dönem oluştur", | ||
| 1186 | + "terms.edit": "Dönemi düzenle", | ||
| 1187 | + "terms.empty": "Henüz dönem yok.", | ||
| 1188 | + "terms.term": "Dönem", | ||
| 1189 | + "terms.unassigned": "Atanmamış", | ||
| 1190 | + "terms.dateRequired": "Başlangıç ve bitiş tarihi gerekli.", | ||
| 1164 | }; | 1191 | }; |
| 1165 | 1192 | ||
| 1166 | export const messages: Record<Locale, Dict> = { en, tr }; | 1193 | export const messages: Record<Locale, Dict> = { en, tr }; |
src/pages/course-detail-page.tsxmodified+24 -1
| @@ -6,6 +6,7 @@ import { getCourseById } from "@/api/getCourseById"; | |||
| 6 | import { getCourseEnrollments } from "@/api/getCourseEnrollments"; | 6 | import { getCourseEnrollments } from "@/api/getCourseEnrollments"; |
| 7 | import { getCourseExams } from "@/api/getCourseExams"; | 7 | import { getCourseExams } from "@/api/getCourseExams"; |
| 8 | import { getMyCourses } from "@/api/getMyCourses"; | 8 | import { getMyCourses } from "@/api/getMyCourses"; |
| 9 | +import { getTerms } from "@/api/getTerms"; | ||
| 9 | import { patchCourseById } from "@/api/patchCourseById"; | 10 | import { patchCourseById } from "@/api/patchCourseById"; |
| 10 | import { postCourseEnrollment } from "@/api/postCourseEnrollment"; | 11 | import { postCourseEnrollment } from "@/api/postCourseEnrollment"; |
| 11 | import { postCourseExam } from "@/api/postCourseExam"; | 12 | import { postCourseExam } from "@/api/postCourseExam"; |
| @@ -23,6 +24,7 @@ import { IconChevronLeft, IconEdit, IconPlus, IconTrash } from "@/components/ui/ | |||
| 23 | import { Input } from "@/components/ui/input"; | 24 | import { Input } from "@/components/ui/input"; |
| 24 | import { Label } from "@/components/ui/label"; | 25 | import { Label } from "@/components/ui/label"; |
| 25 | import { PageSpinner } from "@/components/ui/page-spinner"; | 26 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 27 | +import { Select } from "@/components/ui/select"; | ||
| 26 | import { | 28 | import { |
| 27 | Table, | 29 | Table, |
| 28 | TableBody, | 30 | TableBody, |
| @@ -55,6 +57,7 @@ function CourseDetailContent() { | |||
| 55 | const id = () => params().id; | 57 | const id = () => params().id; |
| 56 | 58 | ||
| 57 | const [course, { refetch: refetchCourse }] = createResource(id, (courseId) => getCourseById(courseId)); | 59 | const [course, { refetch: refetchCourse }] = createResource(id, (courseId) => getCourseById(courseId)); |
| 60 | + const [terms] = createResource(() => getTerms()); | ||
| 58 | const [exams, { refetch: refetchExams }] = createResource(id, (courseId) => getCourseExams(courseId)); | 61 | const [exams, { refetch: refetchExams }] = createResource(id, (courseId) => getCourseExams(courseId)); |
| 59 | const isTeacherPlus = () => hasMinRole(auth.user()?.role, "teacher"); | 62 | const isTeacherPlus = () => hasMinRole(auth.user()?.role, "teacher"); |
| 60 | const [roster, { refetch: refetchRoster }] = createResource( | 63 | const [roster, { refetch: refetchRoster }] = createResource( |
| @@ -69,6 +72,7 @@ function CourseDetailContent() { | |||
| 69 | const [editing, setEditing] = createSignal(false); | 72 | const [editing, setEditing] = createSignal(false); |
| 70 | const [title, setTitle] = createSignal(""); | 73 | const [title, setTitle] = createSignal(""); |
| 71 | const [description, setDescription] = createSignal(""); | 74 | const [description, setDescription] = createSignal(""); |
| 75 | + const [termId, setTermId] = createSignal(""); | ||
| 72 | const [showExamForm, setShowExamForm] = createSignal(false); | 76 | const [showExamForm, setShowExamForm] = createSignal(false); |
| 73 | const [enrollUserId, setEnrollUserId] = createSignal(""); | 77 | const [enrollUserId, setEnrollUserId] = createSignal(""); |
| 74 | const [error, setError] = createSignal(""); | 78 | const [error, setError] = createSignal(""); |
| @@ -121,6 +125,7 @@ function CourseDetailContent() { | |||
| 121 | if (!c) return; | 125 | if (!c) return; |
| 122 | setTitle(c.title); | 126 | setTitle(c.title); |
| 123 | setDescription(c.description); | 127 | setDescription(c.description); |
| 128 | + setTermId(c.term_id ?? ""); | ||
| 124 | setEditing(true); | 129 | setEditing(true); |
| 125 | }; | 130 | }; |
| 126 | 131 | ||
| @@ -227,6 +232,7 @@ function CourseDetailContent() { | |||
| 227 | await patchCourseById(id(), { | 232 | await patchCourseById(id(), { |
| 228 | title: title().trim(), | 233 | title: title().trim(), |
| 229 | description: description(), | 234 | description: description(), |
| 235 | + term_id: termId() || null, | ||
| 230 | }); | 236 | }); |
| 231 | setEditing(false); | 237 | setEditing(false); |
| 232 | await refetchCourse(); | 238 | await refetchCourse(); |
| @@ -251,6 +257,13 @@ function CourseDetailContent() { | |||
| 251 | onInput={(e) => setDescription(e.currentTarget.value)} | 257 | onInput={(e) => setDescription(e.currentTarget.value)} |
| 252 | /> | 258 | /> |
| 253 | </div> | 259 | </div> |
| 260 | + <div class="space-y-1.5"> | ||
| 261 | + <Label for="edit-course-term">{t("terms.term")}</Label> | ||
| 262 | + <Select id="edit-course-term" value={termId()} onChange={(e) => setTermId(e.currentTarget.value)}> | ||
| 263 | + <option value="">{t("terms.unassigned")}</option> | ||
| 264 | + <For each={terms() ?? []}>{(term) => <option value={term.id}>{term.name}</option>}</For> | ||
| 265 | + </Select> | ||
| 266 | + </div> | ||
| 254 | <div class="flex justify-end"> | 267 | <div class="flex justify-end"> |
| 255 | <Button type="submit" class="w-full sm:w-auto" disabled={pending()}> | 268 | <Button type="submit" class="w-full sm:w-auto" disabled={pending()}> |
| 256 | {t("common.update")} | 269 | {t("common.update")} |
| @@ -263,7 +276,7 @@ function CourseDetailContent() { | |||
| 263 | <p class="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">{error()}</p> | 276 | <p class="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">{error()}</p> |
| 264 | )} | 277 | )} |
| 265 | 278 | ||
| 266 | - <section class="grid gap-3 sm:grid-cols-3"> | 279 | + <section class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4"> |
| 267 | <div class="surface-card bg-card/80 p-4"> | 280 | <div class="surface-card bg-card/80 p-4"> |
| 268 | <p class="text-xs font-semibold uppercase tracking-wider text-muted-foreground"> | 281 | <p class="text-xs font-semibold uppercase tracking-wider text-muted-foreground"> |
| 269 | {t("courses.exams")} | 282 | {t("courses.exams")} |
| @@ -289,6 +302,16 @@ function CourseDetailContent() { | |||
| 289 | <p class="mt-2 font-display text-3xl font-semibold tabular-nums">{examKindCount()}</p> | 302 | <p class="mt-2 font-display text-3xl font-semibold tabular-nums">{examKindCount()}</p> |
| 290 | <p class="mt-1 text-xs text-muted-foreground">{t("courses.exams")}</p> | 303 | <p class="mt-1 text-xs text-muted-foreground">{t("courses.exams")}</p> |
| 291 | </div> | 304 | </div> |
| 305 | + | ||
| 306 | + <div class="surface-card bg-card/80 p-4"> | ||
| 307 | + <p class="text-xs font-semibold uppercase tracking-wider text-muted-foreground"> | ||
| 308 | + {t("terms.term")} | ||
| 309 | + </p> | ||
| 310 | + <p class="mt-2 truncate font-display text-xl font-semibold"> | ||
| 311 | + {terms()?.find((term) => term.id === c().term_id)?.name ?? t("terms.unassigned")} | ||
| 312 | + </p> | ||
| 313 | + <p class="mt-1 text-xs text-muted-foreground">{t("terms.title")}</p> | ||
| 314 | + </div> | ||
| 292 | </section> | 315 | </section> |
| 293 | 316 | ||
| 294 | {/* Course exams */} | 317 | {/* Course exams */} |
src/pages/courses-page.tsxmodified+20 -0
| @@ -2,6 +2,7 @@ import { For, Show, Suspense, createMemo, createResource, createSignal } from "s | |||
| 2 | import { Link } from "@tanstack/solid-router"; | 2 | import { Link } from "@tanstack/solid-router"; |
| 3 | import { getCourses } from "@/api/getCourses"; | 3 | import { getCourses } from "@/api/getCourses"; |
| 4 | import { getMyCourses } from "@/api/getMyCourses"; | 4 | import { getMyCourses } from "@/api/getMyCourses"; |
| 5 | +import { getTerms } from "@/api/getTerms"; | ||
| 5 | import { postCourse } from "@/api/postCourse"; | 6 | import { postCourse } from "@/api/postCourse"; |
| 6 | import { formatApiError } from "@/api/client"; | 7 | import { formatApiError } from "@/api/client"; |
| 7 | import { Alert } from "@/components/ui/alert"; | 8 | import { Alert } from "@/components/ui/alert"; |
| @@ -11,6 +12,7 @@ import { Badge } from "@/components/ui/badge"; | |||
| 11 | import { Button } from "@/components/ui/button"; | 12 | import { Button } from "@/components/ui/button"; |
| 12 | import { Input } from "@/components/ui/input"; | 13 | import { Input } from "@/components/ui/input"; |
| 13 | import { Label } from "@/components/ui/label"; | 14 | import { Label } from "@/components/ui/label"; |
| 15 | +import { Select } from "@/components/ui/select"; | ||
| 14 | import { Textarea } from "@/components/ui/textarea"; | 16 | import { Textarea } from "@/components/ui/textarea"; |
| 15 | import { PageSpinner } from "@/components/ui/page-spinner"; | 17 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 16 | import { PaginationControls } from "@/components/ui/pagination-controls"; | 18 | import { PaginationControls } from "@/components/ui/pagination-controls"; |
| @@ -32,11 +34,13 @@ function CoursesContent() { | |||
| 32 | const auth = useAuth(); | 34 | const auth = useAuth(); |
| 33 | const t = useT(); | 35 | const t = useT(); |
| 34 | const [courses, { refetch }] = createResource(() => getCourses()); | 36 | const [courses, { refetch }] = createResource(() => getCourses()); |
| 37 | + const [terms] = createResource(() => getTerms()); | ||
| 35 | const [mine] = createResource(() => getMyCourses()); | 38 | const [mine] = createResource(() => getMyCourses()); |
| 36 | const enrolled = () => new Set((mine() ?? []).map((c) => c.id)); | 39 | const enrolled = () => new Set((mine() ?? []).map((c) => c.id)); |
| 37 | const [showForm, setShowForm] = createSignal(false); | 40 | const [showForm, setShowForm] = createSignal(false); |
| 38 | const [title, setTitle] = createSignal(""); | 41 | const [title, setTitle] = createSignal(""); |
| 39 | const [description, setDescription] = createSignal(""); | 42 | const [description, setDescription] = createSignal(""); |
| 43 | + const [termId, setTermId] = createSignal(""); | ||
| 40 | const [error, setError] = createSignal(""); | 44 | const [error, setError] = createSignal(""); |
| 41 | const [pending, setPending] = createSignal(false); | 45 | const [pending, setPending] = createSignal(false); |
| 42 | const [page, setPage] = createSignal(0); | 46 | const [page, setPage] = createSignal(0); |
| @@ -58,9 +62,11 @@ function CoursesContent() { | |||
| 58 | await postCourse({ | 62 | await postCourse({ |
| 59 | title: title().trim(), | 63 | title: title().trim(), |
| 60 | description: description().trim() || undefined, | 64 | description: description().trim() || undefined, |
| 65 | + term_id: termId() || null, | ||
| 61 | }); | 66 | }); |
| 62 | setTitle(""); | 67 | setTitle(""); |
| 63 | setDescription(""); | 68 | setDescription(""); |
| 69 | + setTermId(""); | ||
| 64 | setShowForm(false); | 70 | setShowForm(false); |
| 65 | await refetch(); | 71 | await refetch(); |
| 66 | } catch (err) { | 72 | } catch (err) { |
| @@ -108,6 +114,13 @@ function CoursesContent() { | |||
| 108 | onInput={(e) => setDescription(e.currentTarget.value)} | 114 | onInput={(e) => setDescription(e.currentTarget.value)} |
| 109 | /> | 115 | /> |
| 110 | </div> | 116 | </div> |
| 117 | + <div class="space-y-1.5"> | ||
| 118 | + <Label for="course-term">{t("terms.term")}</Label> | ||
| 119 | + <Select id="course-term" value={termId()} onChange={(e) => setTermId(e.currentTarget.value)}> | ||
| 120 | + <option value="">{t("terms.unassigned")}</option> | ||
| 121 | + <For each={terms() ?? []}>{(term) => <option value={term.id}>{term.name}</option>}</For> | ||
| 122 | + </Select> | ||
| 123 | + </div> | ||
| 111 | {error() && <p class="text-sm text-destructive">{error()}</p>} | 124 | {error() && <p class="text-sm text-destructive">{error()}</p>} |
| 112 | <Button type="submit" disabled={pending()}> | 125 | <Button type="submit" disabled={pending()}> |
| 113 | {t("common.create")} | 126 | {t("common.create")} |
| @@ -145,6 +158,13 @@ function CoursesContent() { | |||
| 145 | </div> | 158 | </div> |
| 146 | </div> | 159 | </div> |
| 147 | <div class="flex flex-1 flex-col p-4"> | 160 | <div class="flex flex-1 flex-col p-4"> |
| 161 | + <Show when={course.term_id}> | ||
| 162 | + {(tid) => ( | ||
| 163 | + <Badge variant="outline" class="mb-3 w-fit rounded-full"> | ||
| 164 | + {terms()?.find((term) => term.id === tid())?.name ?? t("terms.term")} | ||
| 165 | + </Badge> | ||
| 166 | + )} | ||
| 167 | + </Show> | ||
| 148 | <p class="line-clamp-3 flex-1 text-sm text-muted-foreground"> | 168 | <p class="line-clamp-3 flex-1 text-sm text-muted-foreground"> |
| 149 | {course.description || "—"} | 169 | {course.description || "—"} |
| 150 | </p> | 170 | </p> |
src/pages/terms-page.tsxadded+193 -0
| @@ -0,0 +1,193 @@ | |||
| 1 | +import { For, Show, Suspense, createSignal } from "solid-js"; | ||
| 2 | +import { createResource } from "solid-js"; | ||
| 3 | +import { deleteTermById } from "@/api/deleteTermById"; | ||
| 4 | +import { getTerms } from "@/api/getTerms"; | ||
| 5 | +import { patchTermById } from "@/api/patchTermById"; | ||
| 6 | +import { postTerm } from "@/api/postTerm"; | ||
| 7 | +import { formatApiError } from "@/api/client"; | ||
| 8 | +import type { Term } from "@/api/types"; | ||
| 9 | +import { RouteGuard } from "@/components/layout/route-guard"; | ||
| 10 | +import { PageHeader } from "@/components/layout/page-header"; | ||
| 11 | +import { Alert } from "@/components/ui/alert"; | ||
| 12 | +import { Button } from "@/components/ui/button"; | ||
| 13 | +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | ||
| 14 | +import { DatePicker } from "@/components/ui/date-picker"; | ||
| 15 | +import { IconEdit, IconSave, IconTrash } from "@/components/ui/icons"; | ||
| 16 | +import { Input } from "@/components/ui/input"; | ||
| 17 | +import { Label } from "@/components/ui/label"; | ||
| 18 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 19 | +import { formatDateTime } from "@/lib/format"; | ||
| 20 | +import { usePreferences, useT } from "@/stores/preferences-context"; | ||
| 21 | + | ||
| 22 | +function dateInputFromMs(ms: number): string { | ||
| 23 | + const date = new Date(ms); | ||
| 24 | + const pad = (n: number) => String(n).padStart(2, "0"); | ||
| 25 | + return `${pad(date.getDate())}/${pad(date.getMonth() + 1)}/${date.getFullYear()}`; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +function dateInputToMs(value: string): number | null { | ||
| 29 | + const match = value.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/); | ||
| 30 | + if (!match) return null; | ||
| 31 | + const [, dayRaw, monthRaw, yearRaw] = match; | ||
| 32 | + const day = Number(dayRaw); | ||
| 33 | + const month = Number(monthRaw); | ||
| 34 | + const year = Number(yearRaw); | ||
| 35 | + const date = new Date(year, month - 1, day, 0, 0, 0, 0); | ||
| 36 | + if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) return null; | ||
| 37 | + return date.getTime(); | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +export default function TermsPage() { | ||
| 41 | + return ( | ||
| 42 | + <RouteGuard minRole="manager"> | ||
| 43 | + <TermsContent /> | ||
| 44 | + </RouteGuard> | ||
| 45 | + ); | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +function TermsContent() { | ||
| 49 | + const t = useT(); | ||
| 50 | + const { locale } = usePreferences(); | ||
| 51 | + const [terms, { refetch }] = createResource(() => getTerms()); | ||
| 52 | + const [name, setName] = createSignal(""); | ||
| 53 | + const [starts, setStarts] = createSignal(""); | ||
| 54 | + const [ends, setEnds] = createSignal(""); | ||
| 55 | + const [editing, setEditing] = createSignal<Term | null>(null); | ||
| 56 | + const [deleteTarget, setDeleteTarget] = createSignal<Term | null>(null); | ||
| 57 | + const [error, setError] = createSignal(""); | ||
| 58 | + const [pending, setPending] = createSignal(false); | ||
| 59 | + | ||
| 60 | + const resetForm = () => { | ||
| 61 | + setName(""); | ||
| 62 | + setStarts(""); | ||
| 63 | + setEnds(""); | ||
| 64 | + setEditing(null); | ||
| 65 | + }; | ||
| 66 | + | ||
| 67 | + const startEdit = (term: Term) => { | ||
| 68 | + setName(term.name); | ||
| 69 | + setStarts(dateInputFromMs(term.starts_at)); | ||
| 70 | + setEnds(dateInputFromMs(term.ends_at)); | ||
| 71 | + setEditing(term); | ||
| 72 | + }; | ||
| 73 | + | ||
| 74 | + const save = async (e: SubmitEvent) => { | ||
| 75 | + e.preventDefault(); | ||
| 76 | + setError(""); | ||
| 77 | + const starts_at = dateInputToMs(starts()); | ||
| 78 | + const ends_at = dateInputToMs(ends()); | ||
| 79 | + if (starts_at == null || ends_at == null) { | ||
| 80 | + setError(t("terms.dateRequired")); | ||
| 81 | + return; | ||
| 82 | + } | ||
| 83 | + if (ends_at < starts_at) { | ||
| 84 | + setError(t("form.timeOrder")); | ||
| 85 | + return; | ||
| 86 | + } | ||
| 87 | + setPending(true); | ||
| 88 | + try { | ||
| 89 | + const current = editing(); | ||
| 90 | + if (current) { | ||
| 91 | + await patchTermById(current.id, { name: name().trim(), starts_at, ends_at }); | ||
| 92 | + } else { | ||
| 93 | + await postTerm({ name: name().trim(), starts_at, ends_at }); | ||
| 94 | + } | ||
| 95 | + resetForm(); | ||
| 96 | + await refetch(); | ||
| 97 | + } catch (err) { | ||
| 98 | + setError(formatApiError(err)); | ||
| 99 | + } finally { | ||
| 100 | + setPending(false); | ||
| 101 | + } | ||
| 102 | + }; | ||
| 103 | + | ||
| 104 | + return ( | ||
| 105 | + <div class="space-y-6"> | ||
| 106 | + <PageHeader accent="violet" eyebrow={t("nav.admin")} title={t("terms.title")} description={t("terms.subtitle")} /> | ||
| 107 | + | ||
| 108 | + {error() && <Alert variant="destructive">{error()}</Alert>} | ||
| 109 | + | ||
| 110 | + <section class="surface-card space-y-4 p-5"> | ||
| 111 | + <h2 class="font-display text-lg font-semibold">{editing() ? t("terms.edit") : t("terms.create")}</h2> | ||
| 112 | + <form class="grid gap-3 md:grid-cols-[minmax(0,1fr)_11rem_11rem_auto]" onSubmit={save}> | ||
| 113 | + <div class="space-y-1.5"> | ||
| 114 | + <Label for="term-name">{t("settings.name")}</Label> | ||
| 115 | + <Input id="term-name" required maxlength={100} value={name()} onInput={(e) => setName(e.currentTarget.value)} /> | ||
| 116 | + </div> | ||
| 117 | + <div class="space-y-1.5"> | ||
| 118 | + <Label for="term-starts">{t("events.starts")}</Label> | ||
| 119 | + <DatePicker id="term-starts" placeholder={t("form.datePlaceholder")} required value={starts()} onChange={setStarts} /> | ||
| 120 | + </div> | ||
| 121 | + <div class="space-y-1.5"> | ||
| 122 | + <Label for="term-ends">{t("events.ends")}</Label> | ||
| 123 | + <DatePicker id="term-ends" placeholder={t("form.datePlaceholder")} required value={ends()} onChange={setEnds} /> | ||
| 124 | + </div> | ||
| 125 | + <div class="flex items-end gap-2"> | ||
| 126 | + <Button type="submit" disabled={pending()}> | ||
| 127 | + <IconSave class="h-4 w-4" /> | ||
| 128 | + {editing() ? t("common.update") : t("common.create")} | ||
| 129 | + </Button> | ||
| 130 | + <Show when={editing()}> | ||
| 131 | + <Button type="button" variant="outline" onClick={resetForm}>{t("common.cancel")}</Button> | ||
| 132 | + </Show> | ||
| 133 | + </div> | ||
| 134 | + </form> | ||
| 135 | + </section> | ||
| 136 | + | ||
| 137 | + <Suspense fallback={<PageSpinner />}> | ||
| 138 | + <Show when={terms.error}> | ||
| 139 | + <Alert variant="destructive">{formatApiError(terms.error)}</Alert> | ||
| 140 | + </Show> | ||
| 141 | + <Show | ||
| 142 | + when={(terms() ?? []).length > 0} | ||
| 143 | + fallback={<div class="rounded-md border border-dashed px-6 py-16 text-center text-sm text-muted-foreground">{t("terms.empty")}</div>} | ||
| 144 | + > | ||
| 145 | + <section class="grid gap-3 md:grid-cols-2 xl:grid-cols-3"> | ||
| 146 | + <For each={terms() ?? []}> | ||
| 147 | + {(term) => ( | ||
| 148 | + <article class="surface-card space-y-3 p-4"> | ||
| 149 | + <div> | ||
| 150 | + <h3 class="font-display text-lg font-semibold">{term.name}</h3> | ||
| 151 | + <p class="mt-1 text-sm text-muted-foreground"> | ||
| 152 | + {formatDateTime(term.starts_at, locale())} - {formatDateTime(term.ends_at, locale())} | ||
| 153 | + </p> | ||
| 154 | + </div> | ||
| 155 | + <div class="flex gap-2"> | ||
| 156 | + <Button type="button" variant="outline" size="sm" onClick={() => startEdit(term)}> | ||
| 157 | + <IconEdit class="h-4 w-4" /> | ||
| 158 | + {t("common.edit")} | ||
| 159 | + </Button> | ||
| 160 | + <Button type="button" variant="ghost" size="sm" class="text-destructive hover:text-destructive" onClick={() => setDeleteTarget(term)}> | ||
| 161 | + <IconTrash class="h-4 w-4" /> | ||
| 162 | + {t("common.delete")} | ||
| 163 | + </Button> | ||
| 164 | + </div> | ||
| 165 | + </article> | ||
| 166 | + )} | ||
| 167 | + </For> | ||
| 168 | + </section> | ||
| 169 | + </Show> | ||
| 170 | + </Suspense> | ||
| 171 | + | ||
| 172 | + <ConfirmDialog | ||
| 173 | + open={deleteTarget() != null} | ||
| 174 | + onOpenChange={(open) => !open && setDeleteTarget(null)} | ||
| 175 | + title={t("confirm.deleteTitle")} | ||
| 176 | + variant="destructive" | ||
| 177 | + summary={deleteTarget()?.name ?? ""} | ||
| 178 | + onConfirm={async () => { | ||
| 179 | + const term = deleteTarget(); | ||
| 180 | + if (!term) return; | ||
| 181 | + try { | ||
| 182 | + await deleteTermById(term.id); | ||
| 183 | + await refetch(); | ||
| 184 | + } catch (err) { | ||
| 185 | + setError(formatApiError(err)); | ||
| 186 | + } finally { | ||
| 187 | + setDeleteTarget(null); | ||
| 188 | + } | ||
| 189 | + }} | ||
| 190 | + /> | ||
| 191 | + </div> | ||
| 192 | + ); | ||
| 193 | +} | ||
src/routes/router.tsxmodified+8 -0
| @@ -41,6 +41,7 @@ const ProfilePage = lazyRoute(() => import("@/pages/profile-page")); | |||
| 41 | const LiveMonitorPage = lazyRoute(() => import("@/pages/live-monitor-page")); | 41 | const LiveMonitorPage = lazyRoute(() => import("@/pages/live-monitor-page")); |
| 42 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); | 42 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); |
| 43 | const SettingsPage = lazyRoute(() => import("@/pages/settings-page")); | 43 | const SettingsPage = lazyRoute(() => import("@/pages/settings-page")); |
| 44 | +const TermsPage = lazyRoute(() => import("@/pages/terms-page")); | ||
| 44 | const GuidePage = lazyRoute(() => import("@/pages/guide-page")); | 45 | const GuidePage = lazyRoute(() => import("@/pages/guide-page")); |
| 45 | 46 | ||
| 46 | function RootComponent() { | 47 | function RootComponent() { |
| @@ -161,6 +162,12 @@ const settingsRoute = createRoute({ | |||
| 161 | component: SettingsPage, | 162 | component: SettingsPage, |
| 162 | }); | 163 | }); |
| 163 | 164 | ||
| 165 | +const termsRoute = createRoute({ | ||
| 166 | + getParentRoute: () => rootRoute, | ||
| 167 | + path: "/management/terms", | ||
| 168 | + component: TermsPage, | ||
| 169 | +}); | ||
| 170 | + | ||
| 164 | const liveMonitorRoute = createRoute({ | 171 | const liveMonitorRoute = createRoute({ |
| 165 | getParentRoute: () => rootRoute, | 172 | getParentRoute: () => rootRoute, |
| 166 | path: "/exams/$id/live", | 173 | path: "/exams/$id/live", |
| @@ -215,6 +222,7 @@ const routeTree = rootRoute.addChildren([ | |||
| 215 | marksRoute, | 222 | marksRoute, |
| 216 | studentMarksRoute, | 223 | studentMarksRoute, |
| 217 | settingsRoute, | 224 | settingsRoute, |
| 225 | + termsRoute, | ||
| 218 | profileRoute, | 226 | profileRoute, |
| 219 | adminUsersRoute, | 227 | adminUsersRoute, |
| 220 | guideRoute, | 228 | guideRoute, |