✨ student-only pages, session PATCH, manager staff-work log, beforeLoad guards
Browse files at this commit340431fddda66c98e9edd4230b2140672cc160a7Parent: 28c8d36- feat
- ✨ student-only pages, session PATCH, manager staff-work log, beforeLoad guards
17 changed files +596 -60
- docs/frontend-next-steps.md +9 -16
- src/api/deleteWorkEntryById.ts +5 -0
- src/api/getUserWorkLog.ts +12 -0
- src/api/patchSessionById.ts +13 -0
- src/api/patchWorkEntryById.ts +11 -0
- src/api/types.ts +2 -1
- src/components/layout/route-guard.tsx +16 -8
- src/components/layout/side-nav.tsx +14 -4
- src/components/sessions/course-sessions-panel.tsx +95 -22
- src/i18n/messages.ts +34 -1
- src/lib/roles.ts +4 -0
- src/pages/attendance-page.tsx +1 -1
- src/pages/course-detail-page.tsx +2 -2
- src/pages/courses-page.tsx +4 -4
- src/pages/marks-page.tsx +1 -1
- src/pages/staff-work-page.tsx +325 -0
- src/routes/router.tsx +48 -0
docs/frontend-next-steps.mdmodified+9 -16
| @@ -56,22 +56,15 @@ validation, and list filtering cleanup. | |||
| 56 | 56 | ||
| 57 | ## Active Backlog | 57 | ## Active Backlog |
| 58 | 58 | ||
| 59 | -### Known Bugs / Contract Mismatches (P0) | 59 | +### Recently fixed (this batch) |
| 60 | 60 | ||
| 61 | -| # | Issue | Impact | Side | | 61 | +| Item | Fix | |
| 62 | -|---|-------|--------|------| | 62 | +|---|---| |
| 63 | -| 1 | Course term read mismatch: BE `CourseResponse.term`, FE reads `term_id` | Term column/filter always shows "unassigned"; write body `term_id` is correct | FE type fix | | 63 | +| Course term read | FE reads `Course.term` (write still `term_id`) | |
| 64 | -| 2 | `/marks` and `/attendance` open to all roles (nav + route) | Teachers see "Report card" / "My attendance" — product error; dashboard already correct | FE guard | | 64 | +| Student-only pages | `RouteGuard exactRole="student"` + nav `exactRole` + `beforeLoad` on `/marks`, `/attendance` | |
| 65 | -| 3 | No student-only guard (`RouteGuard` only has `minRole`) | Staff can reach personal student pages via direct URL | FE infrastructure | | 65 | +| Session edit | `patchSessionById` + edit SidePanel in course sessions | |
| 66 | - | 66 | +| Manager work log | `/management/staff-work` + get/patch/delete work APIs | |
| 67 | -### Missing BE Endpoints (no FE helper/UI) | 67 | +| Work route guard | `beforeLoad` teacher+ on `/work` | |
| 68 | - | ||
| 69 | -| Endpoint | Purpose | Priority | | ||
| 70 | -|---|---|---| | ||
| 71 | -| `PATCH /sessions/{id}` | Edit course session | High | | ||
| 72 | -| `GET /work/{user}` | Manager read another user's work log | High | | ||
| 73 | -| `PATCH /work/entries/{id}` | Manager correct a closed stint | High | | ||
| 74 | -| `DELETE /work/entries/{id}` | Manager delete a work entry | High | | ||
| 75 | 68 | ||
| 76 | ### Incomplete Product Features | 69 | ### Incomplete Product Features |
| 77 | 70 | ||
| @@ -79,7 +72,7 @@ validation, and list filtering cleanup. | |||
| 79 | |---|---|---|---| | 72 | |---|---|---|---| |
| 80 | | `allow_rejoin` | Type + body exist | No toggle; form always sends `true` | Deferred | | 73 | | `allow_rejoin` | Type + body exist | No toggle; form always sends `true` | Deferred | |
| 81 | | Exam weight badge | BE exam DTO has no weight | Badge never shows; needs weight derived from settings kind map | Needs BE or FE-side resolution | | 74 | | Exam weight badge | BE exam DTO has no weight | Badge never shows; needs weight derived from settings kind map | Needs BE or FE-side resolution | |
| 82 | -| Router `beforeLoad` | — | Missing on `/work`, `/live`, settings, terms; only client-side `RouteGuard` exists | Consistency | | 75 | +| Router `beforeLoad` | — | Still missing on live monitor, settings, terms | Consistency | |
| 83 | | Exam room CTA | — | Visible to manager/admin on detail page; page itself blocks | Minor | | 76 | | Exam room CTA | — | Visible to manager/admin on detail page; page itself blocks | Minor | |
| 84 | 77 | ||
| 85 | ### Backend-Dependent Follow-Up | 78 | ### Backend-Dependent Follow-Up |
src/api/deleteWorkEntryById.tsadded+5 -0
| @@ -0,0 +1,5 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | + | ||
| 3 | +export function deleteWorkEntryById(id: string): Promise<void> { | ||
| 4 | + return client<void>(`/work/entries/${id}`, { method: "DELETE" }); | ||
| 5 | +} | ||
src/api/getUserWorkLog.tsadded+12 -0
| @@ -0,0 +1,12 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import { normalizePage, pageQuery, type Page, type PageParams } from "./page"; | ||
| 3 | +import type { WorkEntry } from "./types"; | ||
| 4 | + | ||
| 5 | +export async function getUserWorkLog( | ||
| 6 | + userId: string, | ||
| 7 | + params?: PageParams, | ||
| 8 | + signal?: AbortSignal, | ||
| 9 | +): Promise<Page<WorkEntry>> { | ||
| 10 | + const data = await client<unknown>(`/work/${userId}${pageQuery(params)}`, { signal }); | ||
| 11 | + return normalizePage<WorkEntry>(data); | ||
| 12 | +} | ||
src/api/patchSessionById.tsadded+13 -0
| @@ -0,0 +1,13 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import type { CourseSession } from "./types"; | ||
| 3 | + | ||
| 4 | +export type PatchSessionBody = { | ||
| 5 | + topic?: string | null; | ||
| 6 | + teacher_id?: string | null; | ||
| 7 | + starts_at?: number | null; | ||
| 8 | + ends_at?: number | null; | ||
| 9 | +}; | ||
| 10 | + | ||
| 11 | +export function patchSessionById(id: string, body: PatchSessionBody): Promise<CourseSession> { | ||
| 12 | + return client<CourseSession>(`/sessions/${id}`, { method: "PATCH", body }); | ||
| 13 | +} | ||
src/api/patchWorkEntryById.tsadded+11 -0
| @@ -0,0 +1,11 @@ | |||
| 1 | +import { client } from "./client"; | ||
| 2 | +import type { WorkEntry } from "./types"; | ||
| 3 | + | ||
| 4 | +export type PatchWorkEntryBody = { | ||
| 5 | + check_in?: number | null; | ||
| 6 | + check_out?: number | null; | ||
| 7 | +}; | ||
| 8 | + | ||
| 9 | +export function patchWorkEntryById(id: string, body: PatchWorkEntryBody): Promise<WorkEntry> { | ||
| 10 | + return client<WorkEntry>(`/work/entries/${id}`, { method: "PATCH", body }); | ||
| 11 | +} | ||
src/api/types.tsmodified+2 -1
| @@ -62,7 +62,8 @@ export type Course = { | |||
| 62 | creator: string; | 62 | creator: string; |
| 63 | title: string; | 63 | title: string; |
| 64 | description: string; | 64 | description: string; |
| 65 | - term_id?: string | null; | 65 | + /** Academic term id from API (`CourseResponse.term`). */ |
| 66 | + term?: string | null; | ||
| 66 | }; | 67 | }; |
| 67 | 68 | ||
| 68 | export type Term = { | 69 | export type Term = { |
src/components/layout/route-guard.tsxmodified+16 -8
| @@ -3,25 +3,33 @@ import { type ParentProps, Show } from "solid-js"; | |||
| 3 | import type { Role } from "@/api/types"; | 3 | import type { Role } from "@/api/types"; |
| 4 | import { Alert } from "@/components/ui/alert"; | 4 | import { Alert } from "@/components/ui/alert"; |
| 5 | import { PageSpinner } from "@/components/ui/page-spinner"; | 5 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 6 | -import { hasMinRole } from "@/lib/roles"; | 6 | +import { hasExactRole, hasMinRole } from "@/lib/roles"; |
| 7 | import { useAuth } from "@/stores/auth-context"; | 7 | import { useAuth } from "@/stores/auth-context"; |
| 8 | import { useT } from "@/stores/preferences-context"; | 8 | import { useT } from "@/stores/preferences-context"; |
| 9 | 9 | ||
| 10 | -export function RouteGuard(props: ParentProps<{ minRole?: Role }>) { | 10 | +export function RouteGuard( |
| 11 | + props: ParentProps<{ | ||
| 12 | + minRole?: Role; | ||
| 13 | + /** When set, only this exact role may enter (e.g. student-only pages). */ | ||
| 14 | + exactRole?: Role; | ||
| 15 | + }>, | ||
| 16 | +) { | ||
| 11 | const auth = useAuth(); | 17 | const auth = useAuth(); |
| 12 | const t = useT(); | 18 | const t = useT(); |
| 13 | 19 | ||
| 20 | + const allowed = (role: Role) => { | ||
| 21 | + if (props.exactRole) return hasExactRole(role, props.exactRole); | ||
| 22 | + if (props.minRole) return hasMinRole(role, props.minRole); | ||
| 23 | + return true; | ||
| 24 | + }; | ||
| 25 | + | ||
| 14 | return ( | 26 | return ( |
| 15 | <Show when={!auth.loading()} fallback={<PageSpinner />}> | 27 | <Show when={!auth.loading()} fallback={<PageSpinner />}> |
| 16 | <Show when={auth.user()} fallback={<Navigate to="/login" />}> | 28 | <Show when={auth.user()} fallback={<Navigate to="/login" />}> |
| 17 | {(u) => ( | 29 | {(u) => ( |
| 18 | <Show | 30 | <Show |
| 19 | - when={!props.minRole || hasMinRole(u().role, props.minRole)} | 31 | + when={allowed(u().role)} |
| 20 | - fallback={ | 32 | + fallback={<Alert variant="destructive">{t("common.accessDenied")}</Alert>} |
| 21 | - <Alert variant="destructive"> | ||
| 22 | - {t("common.accessDenied")} | ||
| 23 | - </Alert> | ||
| 24 | - } | ||
| 25 | > | 33 | > |
| 26 | {props.children} | 34 | {props.children} |
| 27 | </Show> | 35 | </Show> |
src/components/layout/side-nav.tsxmodified+14 -4
| @@ -8,7 +8,7 @@ import { | |||
| 8 | } from "@/components/ui/dropdown-menu"; | 8 | } from "@/components/ui/dropdown-menu"; |
| 9 | import { useAuth } from "@/stores/auth-context"; | 9 | import { useAuth } from "@/stores/auth-context"; |
| 10 | import { useT } from "@/stores/preferences-context"; | 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | -import { hasMinRole } from "@/lib/roles"; | 11 | +import { hasExactRole, hasMinRole } from "@/lib/roles"; |
| 12 | import { cn } from "@/lib/cn"; | 12 | import { cn } from "@/lib/cn"; |
| 13 | import type { MessageKey } from "@/i18n/messages"; | 13 | import type { MessageKey } from "@/i18n/messages"; |
| 14 | import type { Role } from "@/api/types"; | 14 | import type { Role } from "@/api/types"; |
| @@ -34,6 +34,8 @@ type NavItem = { | |||
| 34 | labelKey: MessageKey; | 34 | labelKey: MessageKey; |
| 35 | Icon: Component<{ class?: string }>; | 35 | Icon: Component<{ class?: string }>; |
| 36 | minRole?: Role; | 36 | minRole?: Role; |
| 37 | + /** When set, only this exact role sees the item. */ | ||
| 38 | + exactRole?: Role; | ||
| 37 | exact?: boolean; | 39 | exact?: boolean; |
| 38 | }; | 40 | }; |
| 39 | 41 | ||
| @@ -51,7 +53,7 @@ const NAV_GROUPS: NavGroup[] = [ | |||
| 51 | labelKey: "nav.group.students", | 53 | labelKey: "nav.group.students", |
| 52 | Icon: IconUsers, | 54 | Icon: IconUsers, |
| 53 | items: [ | 55 | items: [ |
| 54 | - { to: "/attendance", labelKey: "nav.attendance", Icon: IconClipboardCheck }, | 56 | + { to: "/attendance", labelKey: "nav.attendance", Icon: IconClipboardCheck, exactRole: "student" }, |
| 55 | ], | 57 | ], |
| 56 | }, | 58 | }, |
| 57 | { | 59 | { |
| @@ -69,7 +71,7 @@ const NAV_GROUPS: NavGroup[] = [ | |||
| 69 | labelKey: "nav.group.grades", | 71 | labelKey: "nav.group.grades", |
| 70 | Icon: IconNote, | 72 | Icon: IconNote, |
| 71 | items: [ | 73 | items: [ |
| 72 | - { to: "/marks", labelKey: "nav.marks", Icon: IconChart }, | 74 | + { to: "/marks", labelKey: "nav.marks", Icon: IconChart, exactRole: "student" }, |
| 73 | { to: "/notes", labelKey: "nav.notes", Icon: IconNote }, | 75 | { to: "/notes", labelKey: "nav.notes", Icon: IconNote }, |
| 74 | ], | 76 | ], |
| 75 | }, | 77 | }, |
| @@ -82,6 +84,7 @@ const NAV_GROUPS: NavGroup[] = [ | |||
| 82 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, | 84 | { to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher" }, |
| 83 | { to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, minRole: "teacher" }, | 85 | { to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, minRole: "teacher" }, |
| 84 | { to: "/work", labelKey: "nav.work", Icon: IconReportAnalytics, minRole: "teacher" }, | 86 | { to: "/work", labelKey: "nav.work", Icon: IconReportAnalytics, minRole: "teacher" }, |
| 87 | + { to: "/management/staff-work", labelKey: "nav.staffWork", Icon: IconReportAnalytics, minRole: "manager" }, | ||
| 85 | ], | 88 | ], |
| 86 | }, | 89 | }, |
| 87 | { | 90 | { |
| @@ -155,12 +158,19 @@ export function SideNav(props: { onNavigate?: () => void; collapsed?: boolean }) | |||
| 155 | const auth = useAuth(); | 158 | const auth = useAuth(); |
| 156 | const t = useT(); | 159 | const t = useT(); |
| 157 | const pathname = useRouterState({ select: (s) => s.location.pathname }); | 160 | const pathname = useRouterState({ select: (s) => s.location.pathname }); |
| 161 | + const itemVisible = (item: NavItem) => { | ||
| 162 | + const role = auth.user()?.role; | ||
| 163 | + if (item.exactRole) return hasExactRole(role, item.exactRole); | ||
| 164 | + if (item.minRole) return hasMinRole(role, item.minRole); | ||
| 165 | + return true; | ||
| 166 | + }; | ||
| 167 | + | ||
| 158 | const visibleGroups = createMemo(() => | 168 | const visibleGroups = createMemo(() => |
| 159 | NAV_GROUPS | 169 | NAV_GROUPS |
| 160 | .filter((group) => !group.minRole || hasMinRole(auth.user()?.role, group.minRole)) | 170 | .filter((group) => !group.minRole || hasMinRole(auth.user()?.role, group.minRole)) |
| 161 | .map((group) => ({ | 171 | .map((group) => ({ |
| 162 | ...group, | 172 | ...group, |
| 163 | - items: group.items.filter((item) => !item.minRole || hasMinRole(auth.user()?.role, item.minRole)), | 173 | + items: group.items.filter(itemVisible), |
| 164 | })) | 174 | })) |
| 165 | .filter((group) => group.items.length > 0), | 175 | .filter((group) => group.items.length > 0), |
| 166 | ); | 176 | ); |
src/components/sessions/course-sessions-panel.tsxmodified+95 -22
| @@ -3,6 +3,7 @@ import { deleteSessionById } from "@/api/deleteSessionById"; | |||
| 3 | import { getCourseSessions } from "@/api/getCourseSessions"; | 3 | import { getCourseSessions } from "@/api/getCourseSessions"; |
| 4 | import { getSessionAttendance } from "@/api/getSessionAttendance"; | 4 | import { getSessionAttendance } from "@/api/getSessionAttendance"; |
| 5 | import { getTime } from "@/api/getTime"; | 5 | import { getTime } from "@/api/getTime"; |
| 6 | +import { patchSessionById } from "@/api/patchSessionById"; | ||
| 6 | import { postCourseSession } from "@/api/postCourseSession"; | 7 | import { postCourseSession } from "@/api/postCourseSession"; |
| 7 | import { postSessionAttendance } from "@/api/postSessionAttendance"; | 8 | import { postSessionAttendance } from "@/api/postSessionAttendance"; |
| 8 | import { formatApiError } from "@/api/client"; | 9 | import { formatApiError } from "@/api/client"; |
| @@ -13,7 +14,7 @@ import { Button } from "@/components/ui/button"; | |||
| 13 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | 14 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 14 | import { DatePicker } from "@/components/ui/date-picker"; | 15 | import { DatePicker } from "@/components/ui/date-picker"; |
| 15 | import { ErrorAlert } from "@/components/ui/error-alert"; | 16 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 16 | -import { IconTrash } from "@/components/ui/icons"; | 17 | +import { IconEdit, IconTrash } from "@/components/ui/icons"; |
| 17 | import { Input } from "@/components/ui/input"; | 18 | import { Input } from "@/components/ui/input"; |
| 18 | import { Label } from "@/components/ui/label"; | 19 | import { Label } from "@/components/ui/label"; |
| 19 | import { PageSpinner } from "@/components/ui/page-spinner"; | 20 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| @@ -39,6 +40,18 @@ function dateInputToMs(date: string, time: string): number | null { | |||
| 39 | return d.getTime(); | 40 | return d.getTime(); |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 43 | +function msToDateInput(ms: number): string { | ||
| 44 | + const d = new Date(ms); | ||
| 45 | + const pad = (n: number) => String(n).padStart(2, "0"); | ||
| 46 | + return `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${d.getFullYear()}`; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +function msToTimeInput(ms: number): string { | ||
| 50 | + const d = new Date(ms); | ||
| 51 | + const pad = (n: number) => String(n).padStart(2, "0"); | ||
| 52 | + return `${pad(d.getHours())}:${pad(d.getMinutes())}`; | ||
| 53 | +} | ||
| 54 | + | ||
| 42 | const ROLL_CALL_PAGE_SIZE = 8; | 55 | const ROLL_CALL_PAGE_SIZE = 8; |
| 43 | 56 | ||
| 44 | export function CourseSessionsPanel(props: { | 57 | export function CourseSessionsPanel(props: { |
| @@ -56,6 +69,7 @@ export function CourseSessionsPanel(props: { | |||
| 56 | async (courseId) => (courseId ? (await getCourseSessions(courseId)).items : []), | 69 | async (courseId) => (courseId ? (await getCourseSessions(courseId)).items : []), |
| 57 | ); | 70 | ); |
| 58 | const [selectedSession, setSelectedSession] = createSignal<CourseSession | null>(null); | 71 | const [selectedSession, setSelectedSession] = createSignal<CourseSession | null>(null); |
| 72 | + const [editingSession, setEditingSession] = createSignal<CourseSession | null>(null); | ||
| 59 | const [deleteTarget, setDeleteTarget] = createSignal<CourseSession | null>(null); | 73 | const [deleteTarget, setDeleteTarget] = createSignal<CourseSession | null>(null); |
| 60 | const [topic, setTopic] = createSignal(""); | 74 | const [topic, setTopic] = createSignal(""); |
| 61 | const [startsDate, setStartsDate] = createSignal(""); | 75 | const [startsDate, setStartsDate] = createSignal(""); |
| @@ -66,45 +80,80 @@ export function CourseSessionsPanel(props: { | |||
| 66 | const [pending, setPending] = createSignal(false); | 80 | const [pending, setPending] = createSignal(false); |
| 67 | const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() }))); | 81 | const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() }))); |
| 68 | 82 | ||
| 69 | - const resetCreateForm = () => { | 83 | + const resetForm = () => { |
| 70 | setTopic(""); | 84 | setTopic(""); |
| 71 | setStartsDate(""); | 85 | setStartsDate(""); |
| 72 | setStartsTime(""); | 86 | setStartsTime(""); |
| 73 | setEndsDate(""); | 87 | setEndsDate(""); |
| 74 | setEndsTime(""); | 88 | setEndsTime(""); |
| 75 | setError(""); | 89 | setError(""); |
| 90 | + setEditingSession(null); | ||
| 76 | }; | 91 | }; |
| 77 | 92 | ||
| 78 | - const createSession = async (e: SubmitEvent) => { | 93 | + const startEdit = (session: CourseSession) => { |
| 79 | - e.preventDefault(); | 94 | + setEditingSession(session); |
| 95 | + setTopic(session.topic || ""); | ||
| 96 | + setStartsDate(msToDateInput(session.starts_at)); | ||
| 97 | + setStartsTime(msToTimeInput(session.starts_at)); | ||
| 98 | + setEndsDate(session.ends_at != null ? msToDateInput(session.ends_at) : ""); | ||
| 99 | + setEndsTime(session.ends_at != null ? msToTimeInput(session.ends_at) : ""); | ||
| 80 | setError(""); | 100 | setError(""); |
| 101 | + props.onCreateOpenChange(false); | ||
| 102 | + }; | ||
| 103 | + | ||
| 104 | + const parseSchedule = () => { | ||
| 81 | const starts_at = dateInputToMs(startsDate(), startsTime()); | 105 | const starts_at = dateInputToMs(startsDate(), startsTime()); |
| 82 | const ends_at = endsDate().trim() || endsTime().trim() ? dateInputToMs(endsDate(), endsTime()) : null; | 106 | const ends_at = endsDate().trim() || endsTime().trim() ? dateInputToMs(endsDate(), endsTime()) : null; |
| 83 | if (starts_at == null) { | 107 | if (starts_at == null) { |
| 84 | setError(t("sessions.startRequired")); | 108 | setError(t("sessions.startRequired")); |
| 85 | - return; | 109 | + return null; |
| 86 | } | 110 | } |
| 87 | if ((endsDate().trim() || endsTime().trim()) && ends_at == null) { | 111 | if ((endsDate().trim() || endsTime().trim()) && ends_at == null) { |
| 88 | setError(t("sessions.endInvalid")); | 112 | setError(t("sessions.endInvalid")); |
| 89 | - return; | 113 | + return null; |
| 90 | } | 114 | } |
| 91 | if (ends_at != null && ends_at < starts_at) { | 115 | if (ends_at != null && ends_at < starts_at) { |
| 92 | setError(t("form.timeOrder")); | 116 | setError(t("form.timeOrder")); |
| 93 | - return; | 117 | + return null; |
| 94 | } | 118 | } |
| 95 | - if (starts_at < (serverTime()?.now ?? Date.now()) || (ends_at != null && ends_at < (serverTime()?.now ?? Date.now()))) { | 119 | + return { starts_at, ends_at }; |
| 120 | + }; | ||
| 121 | + | ||
| 122 | + const saveSession = async (e: SubmitEvent) => { | ||
| 123 | + e.preventDefault(); | ||
| 124 | + setError(""); | ||
| 125 | + const schedule = parseSchedule(); | ||
| 126 | + if (!schedule) return; | ||
| 127 | + const { starts_at, ends_at } = schedule; | ||
| 128 | + const current = editingSession(); | ||
| 129 | + const now = serverTime()?.now ?? Date.now(); | ||
| 130 | + // Only newly set times must not be past (backend rule). | ||
| 131 | + if (!current || starts_at !== current.starts_at) { | ||
| 132 | + if (starts_at < now) { | ||
| 133 | + setError(t("form.timePast")); | ||
| 134 | + return; | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + if (ends_at != null && (!current || ends_at !== current.ends_at) && ends_at < now) { | ||
| 96 | setError(t("form.timePast")); | 138 | setError(t("form.timePast")); |
| 97 | return; | 139 | return; |
| 98 | } | 140 | } |
| 99 | setPending(true); | 141 | setPending(true); |
| 100 | try { | 142 | try { |
| 101 | - const body = { | 143 | + if (current) { |
| 102 | - starts_at, | 144 | + await patchSessionById(current.id, { |
| 103 | - ...(topic().trim() ? { topic: topic().trim() } : {}), | 145 | + topic: topic().trim() || "", |
| 104 | - ...(ends_at != null ? { ends_at } : {}), | 146 | + starts_at, |
| 105 | - }; | 147 | + ends_at, |
| 106 | - await postCourseSession(props.courseId, body); | 148 | + }); |
| 107 | - resetCreateForm(); | 149 | + } else { |
| 150 | + await postCourseSession(props.courseId, { | ||
| 151 | + starts_at, | ||
| 152 | + ...(topic().trim() ? { topic: topic().trim() } : {}), | ||
| 153 | + ...(ends_at != null ? { ends_at } : {}), | ||
| 154 | + }); | ||
| 155 | + } | ||
| 156 | + resetForm(); | ||
| 108 | props.onCreateOpenChange(false); | 157 | props.onCreateOpenChange(false); |
| 109 | await refetch(); | 158 | await refetch(); |
| 110 | } catch (err) { | 159 | } catch (err) { |
| @@ -114,6 +163,8 @@ export function CourseSessionsPanel(props: { | |||
| 114 | } | 163 | } |
| 115 | }; | 164 | }; |
| 116 | 165 | ||
| 166 | + const panelOpen = () => props.createOpen || editingSession() != null; | ||
| 167 | + | ||
| 117 | return ( | 168 | return ( |
| 118 | <div class="space-y-4"> | 169 | <div class="space-y-4"> |
| 119 | {error() && <Alert variant="destructive">{error()}</Alert>} | 170 | {error() && <Alert variant="destructive">{error()}</Alert>} |
| @@ -140,6 +191,16 @@ export function CourseSessionsPanel(props: { | |||
| 140 | <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setSelectedSession(session)}> | 191 | <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setSelectedSession(session)}> |
| 141 | {t("sessions.rollCall")} | 192 | {t("sessions.rollCall")} |
| 142 | </Button> | 193 | </Button> |
| 194 | + <Button | ||
| 195 | + type="button" | ||
| 196 | + variant="ghost" | ||
| 197 | + size="sm" | ||
| 198 | + class="rounded-lg" | ||
| 199 | + aria-label={t("common.edit")} | ||
| 200 | + onClick={() => startEdit(session)} | ||
| 201 | + > | ||
| 202 | + <IconEdit class="h-4 w-4" /> | ||
| 203 | + </Button> | ||
| 143 | <Button | 204 | <Button |
| 144 | type="button" | 205 | type="button" |
| 145 | variant="ghost" | 206 | variant="ghost" |
| @@ -161,15 +222,19 @@ export function CourseSessionsPanel(props: { | |||
| 161 | </Suspense> | 222 | </Suspense> |
| 162 | 223 | ||
| 163 | <SidePanel | 224 | <SidePanel |
| 164 | - open={props.createOpen} | 225 | + open={panelOpen()} |
| 165 | onOpenChange={(open) => { | 226 | onOpenChange={(open) => { |
| 166 | - props.onCreateOpenChange(open); | 227 | + if (!open) { |
| 167 | - if (!open) resetCreateForm(); | 228 | + props.onCreateOpenChange(false); |
| 229 | + resetForm(); | ||
| 230 | + } else if (!editingSession()) { | ||
| 231 | + props.onCreateOpenChange(true); | ||
| 232 | + } | ||
| 168 | }} | 233 | }} |
| 169 | - title={t("sessions.add")} | 234 | + title={editingSession() ? t("sessions.edit") : t("sessions.add")} |
| 170 | description={t("sessions.subtitle")} | 235 | description={t("sessions.subtitle")} |
| 171 | > | 236 | > |
| 172 | - <form onSubmit={createSession}> | 237 | + <form class="space-y-4" onSubmit={saveSession}> |
| 173 | <div class="space-y-1.5"> | 238 | <div class="space-y-1.5"> |
| 174 | <Label for="session-topic">{t("sessions.topic")}</Label> | 239 | <Label for="session-topic">{t("sessions.topic")}</Label> |
| 175 | <Input id="session-topic" value={topic()} maxlength={200} onInput={(e) => setTopic(e.currentTarget.value)} /> | 240 | <Input id="session-topic" value={topic()} maxlength={200} onInput={(e) => setTopic(e.currentTarget.value)} /> |
| @@ -191,11 +256,19 @@ export function CourseSessionsPanel(props: { | |||
| 191 | </div> | 256 | </div> |
| 192 | </div> | 257 | </div> |
| 193 | <div class="flex flex-wrap items-center gap-2"> | 258 | <div class="flex flex-wrap items-center gap-2"> |
| 194 | - <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={() => props.onCreateOpenChange(false)}> | 259 | + <Button |
| 260 | + type="button" | ||
| 261 | + variant="outline" | ||
| 262 | + class="h-10 rounded-lg" | ||
| 263 | + onClick={() => { | ||
| 264 | + props.onCreateOpenChange(false); | ||
| 265 | + resetForm(); | ||
| 266 | + }} | ||
| 267 | + > | ||
| 195 | {t("common.cancel")} | 268 | {t("common.cancel")} |
| 196 | </Button> | 269 | </Button> |
| 197 | <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> | 270 | <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> |
| 198 | - {t("sessions.add")} | 271 | + {editingSession() ? t("common.update") : t("sessions.add")} |
| 199 | </Button> | 272 | </Button> |
| 200 | </div> | 273 | </div> |
| 201 | </form> | 274 | </form> |
src/i18n/messages.tsmodified+34 -1
| @@ -11,6 +11,7 @@ export type MessageKey = | |||
| 11 | | "nav.marks" | 11 | | "nav.marks" |
| 12 | | "nav.attendance" | 12 | | "nav.attendance" |
| 13 | | "nav.work" | 13 | | "nav.work" |
| 14 | + | "nav.staffWork" | ||
| 14 | | "nav.users" | 15 | | "nav.users" |
| 15 | | "nav.studentMarks" | 16 | | "nav.studentMarks" |
| 16 | | "nav.studentAttendance" | 17 | | "nav.studentAttendance" |
| @@ -451,6 +452,7 @@ export type MessageKey = | |||
| 451 | | "sessions.subtitle" | 452 | | "sessions.subtitle" |
| 452 | | "sessions.topic" | 453 | | "sessions.topic" |
| 453 | | "sessions.add" | 454 | | "sessions.add" |
| 455 | + | "sessions.edit" | ||
| 454 | | "sessions.empty" | 456 | | "sessions.empty" |
| 455 | | "sessions.untitled" | 457 | | "sessions.untitled" |
| 456 | | "sessions.teacher" | 458 | | "sessions.teacher" |
| @@ -481,7 +483,16 @@ export type MessageKey = | |||
| 481 | | "work.duration" | 483 | | "work.duration" |
| 482 | | "work.open" | 484 | | "work.open" |
| 483 | | "work.closed" | 485 | | "work.closed" |
| 484 | - | "work.status"; | 486 | + | "work.status" |
| 487 | + | "work.staffTitle" | ||
| 488 | + | "work.staffSubtitle" | ||
| 489 | + | "work.show" | ||
| 490 | + | "work.forUser" | ||
| 491 | + | "work.correct" | ||
| 492 | + | "work.correctHelp" | ||
| 493 | + | "work.cannotEditOpen" | ||
| 494 | + | "work.timesRequired" | ||
| 495 | + | "work.deleteSummary"; | ||
| 485 | 496 | ||
| 486 | type Dict = Record<MessageKey, string>; | 497 | type Dict = Record<MessageKey, string>; |
| 487 | 498 | ||
| @@ -496,6 +507,7 @@ const en: Dict = { | |||
| 496 | "nav.marks": "Report card", | 507 | "nav.marks": "Report card", |
| 497 | "nav.attendance": "Attendance", | 508 | "nav.attendance": "Attendance", |
| 498 | "nav.work": "Work log", | 509 | "nav.work": "Work log", |
| 510 | + "nav.staffWork": "Staff work", | ||
| 499 | "nav.users": "Users", | 511 | "nav.users": "Users", |
| 500 | "nav.studentMarks": "Student marks", | 512 | "nav.studentMarks": "Student marks", |
| 501 | "nav.studentAttendance": "Student attendance", | 513 | "nav.studentAttendance": "Student attendance", |
| @@ -949,6 +961,7 @@ const en: Dict = { | |||
| 949 | "sessions.subtitle": "Create lessons and take course roll call.", | 961 | "sessions.subtitle": "Create lessons and take course roll call.", |
| 950 | "sessions.topic": "Topic", | 962 | "sessions.topic": "Topic", |
| 951 | "sessions.add": "Add session", | 963 | "sessions.add": "Add session", |
| 964 | + "sessions.edit": "Edit session", | ||
| 952 | "sessions.empty": "No lesson sessions yet.", | 965 | "sessions.empty": "No lesson sessions yet.", |
| 953 | "sessions.untitled": "Untitled lesson", | 966 | "sessions.untitled": "Untitled lesson", |
| 954 | "sessions.teacher": "Teacher", | 967 | "sessions.teacher": "Teacher", |
| @@ -980,6 +993,15 @@ const en: Dict = { | |||
| 980 | "work.open": "Open", | 993 | "work.open": "Open", |
| 981 | "work.closed": "Closed", | 994 | "work.closed": "Closed", |
| 982 | "work.status": "Status", | 995 | "work.status": "Status", |
| 996 | + "work.staffTitle": "Staff work logs", | ||
| 997 | + "work.staffSubtitle": "Look up a staff member, correct closed stints, or delete entries.", | ||
| 998 | + "work.show": "Show log", | ||
| 999 | + "work.forUser": "Log for {user}", | ||
| 1000 | + "work.correct": "Correct entry", | ||
| 1001 | + "work.correctHelp": "Only closed stints can be corrected.", | ||
| 1002 | + "work.cannotEditOpen": "Open stints cannot be corrected. Check out or delete them first.", | ||
| 1003 | + "work.timesRequired": "Enter check-in and check-out date and time.", | ||
| 1004 | + "work.deleteSummary": "Delete work entry from {time}?", | ||
| 983 | }; | 1005 | }; |
| 984 | 1006 | ||
| 985 | const tr: Dict = { | 1007 | const tr: Dict = { |
| @@ -993,6 +1015,7 @@ const tr: Dict = { | |||
| 993 | "nav.marks": "Karnem", | 1015 | "nav.marks": "Karnem", |
| 994 | "nav.attendance": "Yoklama", | 1016 | "nav.attendance": "Yoklama", |
| 995 | "nav.work": "Mesai", | 1017 | "nav.work": "Mesai", |
| 1018 | + "nav.staffWork": "Personel mesai", | ||
| 996 | "nav.users": "Kullanıcılar", | 1019 | "nav.users": "Kullanıcılar", |
| 997 | "nav.studentMarks": "Öğrenci notları", | 1020 | "nav.studentMarks": "Öğrenci notları", |
| 998 | "nav.studentAttendance": "Öğrenci yoklaması", | 1021 | "nav.studentAttendance": "Öğrenci yoklaması", |
| @@ -1446,6 +1469,7 @@ const tr: Dict = { | |||
| 1446 | "sessions.subtitle": "Ders oluştur ve ders yoklaması al.", | 1469 | "sessions.subtitle": "Ders oluştur ve ders yoklaması al.", |
| 1447 | "sessions.topic": "Konu", | 1470 | "sessions.topic": "Konu", |
| 1448 | "sessions.add": "Oturum ekle", | 1471 | "sessions.add": "Oturum ekle", |
| 1472 | + "sessions.edit": "Oturumu düzenle", | ||
| 1449 | "sessions.empty": "Henüz ders oturumu yok.", | 1473 | "sessions.empty": "Henüz ders oturumu yok.", |
| 1450 | "sessions.untitled": "Konu girilmemiş ders", | 1474 | "sessions.untitled": "Konu girilmemiş ders", |
| 1451 | "sessions.teacher": "Öğretmen", | 1475 | "sessions.teacher": "Öğretmen", |
| @@ -1477,6 +1501,15 @@ const tr: Dict = { | |||
| 1477 | "work.open": "Açık", | 1501 | "work.open": "Açık", |
| 1478 | "work.closed": "Kapalı", | 1502 | "work.closed": "Kapalı", |
| 1479 | "work.status": "Durum", | 1503 | "work.status": "Durum", |
| 1504 | + "work.staffTitle": "Personel mesai kayıtları", | ||
| 1505 | + "work.staffSubtitle": "Personel ara, kapalı mesaileri düzelt veya kayıt sil.", | ||
| 1506 | + "work.show": "Kaydı göster", | ||
| 1507 | + "work.forUser": "{user} için kayıtlar", | ||
| 1508 | + "work.correct": "Kaydı düzelt", | ||
| 1509 | + "work.correctHelp": "Yalnızca kapalı mesailer düzeltilebilir.", | ||
| 1510 | + "work.cannotEditOpen": "Açık mesai düzeltilemez. Önce çıkış yapın veya silin.", | ||
| 1511 | + "work.timesRequired": "Giriş ve çıkış tarih/saatini girin.", | ||
| 1512 | + "work.deleteSummary": "{time} mesai kaydı silinsin mi?", | ||
| 1480 | }; | 1513 | }; |
| 1481 | 1514 | ||
| 1482 | export const messages: Record<Locale, Dict> = { en, tr }; | 1515 | export const messages: Record<Locale, Dict> = { en, tr }; |
src/lib/roles.tsmodified+4 -0
| @@ -12,4 +12,8 @@ export function hasMinRole(role: Role | undefined | null, min: Role): boolean { | |||
| 12 | return RANK[role] >= RANK[min]; | 12 | return RANK[role] >= RANK[min]; |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | +export function hasExactRole(role: Role | undefined | null, exact: Role): boolean { | ||
| 16 | + return role === exact; | ||
| 17 | +} | ||
| 18 | + | ||
| 15 | export const ROLES: Role[] = ["student", "teacher", "manager", "admin"]; | 19 | export const ROLES: Role[] = ["student", "teacher", "manager", "admin"]; |
src/pages/attendance-page.tsxmodified+1 -1
| @@ -10,7 +10,7 @@ import { useT } from "@/stores/preferences-context"; | |||
| 10 | 10 | ||
| 11 | export default function AttendancePage() { | 11 | export default function AttendancePage() { |
| 12 | return ( | 12 | return ( |
| 13 | - <RouteGuard> | 13 | + <RouteGuard exactRole="student"> |
| 14 | <AttendanceContent /> | 14 | <AttendanceContent /> |
| 15 | </RouteGuard> | 15 | </RouteGuard> |
| 16 | ); | 16 | ); |
src/pages/course-detail-page.tsxmodified+2 -2
| @@ -142,7 +142,7 @@ function CourseDetailContent() { | |||
| 142 | if (!c) return; | 142 | if (!c) return; |
| 143 | setTitle(c.title); | 143 | setTitle(c.title); |
| 144 | setDescription(c.description); | 144 | setDescription(c.description); |
| 145 | - setTermId(c.term_id ?? ""); | 145 | + setTermId(c.term ?? ""); |
| 146 | setEditing(true); | 146 | setEditing(true); |
| 147 | }; | 147 | }; |
| 148 | 148 | ||
| @@ -369,7 +369,7 @@ function CourseDetailContent() { | |||
| 369 | {t("terms.term")} | 369 | {t("terms.term")} |
| 370 | </p> | 370 | </p> |
| 371 | <p class="mono mt-2 truncate text-xl font-semibold"> | 371 | <p class="mono mt-2 truncate text-xl font-semibold"> |
| 372 | - {terms()?.find((term) => term.id === c().term_id)?.name ?? t("terms.unassigned")} | 372 | + {terms()?.find((term) => term.id === c().term)?.name ?? t("terms.unassigned")} |
| 373 | </p> | 373 | </p> |
| 374 | <p class="mt-1 text-xs text-muted-foreground">{t("terms.title")}</p> | 374 | <p class="mt-1 text-xs text-muted-foreground">{t("terms.title")}</p> |
| 375 | </div> | 375 | </div> |
src/pages/courses-page.tsxmodified+4 -4
| @@ -72,10 +72,10 @@ function CoursesContent() { | |||
| 72 | const needle = query().trim().toLocaleLowerCase(); | 72 | const needle = query().trim().toLocaleLowerCase(); |
| 73 | const selectedTerm = termFilter(); | 73 | const selectedTerm = termFilter(); |
| 74 | return items.filter((course) => { | 74 | return items.filter((course) => { |
| 75 | - if (selectedTerm === "unassigned" && course.term_id) return false; | 75 | + if (selectedTerm === "unassigned" && course.term) return false; |
| 76 | - if (selectedTerm !== "all" && selectedTerm !== "unassigned" && course.term_id !== selectedTerm) return false; | 76 | + if (selectedTerm !== "all" && selectedTerm !== "unassigned" && course.term !== selectedTerm) return false; |
| 77 | if (!needle) return true; | 77 | if (!needle) return true; |
| 78 | - return [course.title, course.description, course.creator, creatorName(course.creator), termName(course.term_id)] | 78 | + return [course.title, course.description, course.creator, creatorName(course.creator), termName(course.term)] |
| 79 | .join(" ") | 79 | .join(" ") |
| 80 | .toLocaleLowerCase() | 80 | .toLocaleLowerCase() |
| 81 | .includes(needle); | 81 | .includes(needle); |
| @@ -259,7 +259,7 @@ function CoursesContent() { | |||
| 259 | <TableCell class="truncate text-muted-foreground">{course.description || "—"}</TableCell> | 259 | <TableCell class="truncate text-muted-foreground">{course.description || "—"}</TableCell> |
| 260 | <TableCell> | 260 | <TableCell> |
| 261 | <Badge variant="outline" class="mono max-w-full rounded-sm text-[11px]"> | 261 | <Badge variant="outline" class="mono max-w-full rounded-sm text-[11px]"> |
| 262 | - <span class="truncate">{termName(course.term_id)}</span> | 262 | + <span class="truncate">{termName(course.term)}</span> |
| 263 | </Badge> | 263 | </Badge> |
| 264 | </TableCell> | 264 | </TableCell> |
| 265 | <TableCell class="truncate text-muted-foreground">{creatorName(course.creator)}</TableCell> | 265 | <TableCell class="truncate text-muted-foreground">{creatorName(course.creator)}</TableCell> |
src/pages/marks-page.tsxmodified+1 -1
| @@ -10,7 +10,7 @@ import { useT } from "@/stores/preferences-context"; | |||
| 10 | 10 | ||
| 11 | export default function MarksPage() { | 11 | export default function MarksPage() { |
| 12 | return ( | 12 | return ( |
| 13 | - <RouteGuard> | 13 | + <RouteGuard exactRole="student"> |
| 14 | <MarksContent /> | 14 | <MarksContent /> |
| 15 | </RouteGuard> | 15 | </RouteGuard> |
| 16 | ); | 16 | ); |
src/pages/staff-work-page.tsxadded+325 -0
| @@ -0,0 +1,325 @@ | |||
| 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; | ||
| 2 | +import { deleteWorkEntryById } from "@/api/deleteWorkEntryById"; | ||
| 3 | +import { getUserWorkLog } from "@/api/getUserWorkLog"; | ||
| 4 | +import { patchWorkEntryById } from "@/api/patchWorkEntryById"; | ||
| 5 | +import { formatApiError } from "@/api/client"; | ||
| 6 | +import type { PersonRef, WorkEntry } from "@/api/types"; | ||
| 7 | +import { RouteGuard } from "@/components/layout/route-guard"; | ||
| 8 | +import { PageHeader } from "@/components/layout/page-header"; | ||
| 9 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | ||
| 10 | +import { Alert } from "@/components/ui/alert"; | ||
| 11 | +import { Badge } from "@/components/ui/badge"; | ||
| 12 | +import { Button } from "@/components/ui/button"; | ||
| 13 | +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | ||
| 14 | +import { DataTableFrame } from "@/components/ui/data-table"; | ||
| 15 | +import { DatePicker } from "@/components/ui/date-picker"; | ||
| 16 | +import { ErrorAlert } from "@/components/ui/error-alert"; | ||
| 17 | +import { IconEdit, IconTrash } from "@/components/ui/icons"; | ||
| 18 | +import { Input } from "@/components/ui/input"; | ||
| 19 | +import { Label } from "@/components/ui/label"; | ||
| 20 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 21 | +import { PaginationControls } from "@/components/ui/pagination-controls"; | ||
| 22 | +import { SidePanel } from "@/components/ui/side-panel"; | ||
| 23 | +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; | ||
| 24 | +import { TableRowActions } from "@/components/ui/table-row-actions"; | ||
| 25 | +import { formatDateTime, formatDurationMinutes } from "@/lib/format"; | ||
| 26 | +import { loadListPage, totalPages as pagesOf } from "@/lib/list-page"; | ||
| 27 | +import { personLabel } from "@/lib/person"; | ||
| 28 | +import { usePreferences, useT } from "@/stores/preferences-context"; | ||
| 29 | + | ||
| 30 | +const WORK_PAGE_SIZE = 15; | ||
| 31 | + | ||
| 32 | +function msToDateInput(ms: number): string { | ||
| 33 | + const d = new Date(ms); | ||
| 34 | + const pad = (n: number) => String(n).padStart(2, "0"); | ||
| 35 | + return `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${d.getFullYear()}`; | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +function msToTimeInput(ms: number): string { | ||
| 39 | + const d = new Date(ms); | ||
| 40 | + const pad = (n: number) => String(n).padStart(2, "0"); | ||
| 41 | + return `${pad(d.getHours())}:${pad(d.getMinutes())}`; | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +function dateInputToMs(date: string, time: string): number | null { | ||
| 45 | + const dateMatch = date.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/); | ||
| 46 | + const timeMatch = time.trim().match(/^(\d{2}):(\d{2})$/); | ||
| 47 | + if (!dateMatch || !timeMatch) return null; | ||
| 48 | + const [, dayRaw, monthRaw, yearRaw] = dateMatch; | ||
| 49 | + const [, hourRaw, minuteRaw] = timeMatch; | ||
| 50 | + const day = Number(dayRaw); | ||
| 51 | + const month = Number(monthRaw); | ||
| 52 | + const year = Number(yearRaw); | ||
| 53 | + const hour = Number(hourRaw); | ||
| 54 | + const minute = Number(minuteRaw); | ||
| 55 | + const d = new Date(year, month - 1, day, hour, minute, 0, 0); | ||
| 56 | + if (d.getFullYear() !== year || d.getMonth() !== month - 1 || d.getDate() !== day || d.getHours() !== hour || d.getMinutes() !== minute) return null; | ||
| 57 | + return d.getTime(); | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +export default function StaffWorkPage() { | ||
| 61 | + return ( | ||
| 62 | + <RouteGuard minRole="manager"> | ||
| 63 | + <StaffWorkContent /> | ||
| 64 | + </RouteGuard> | ||
| 65 | + ); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +function StaffWorkContent() { | ||
| 69 | + const t = useT(); | ||
| 70 | + const { locale } = usePreferences(); | ||
| 71 | + const [userId, setUserId] = createSignal(""); | ||
| 72 | + const [selectedUser, setSelectedUser] = createSignal<PersonRef | null>(null); | ||
| 73 | + const [lookupId, setLookupId] = createSignal<string | null>(null); | ||
| 74 | + const [lookupLabel, setLookupLabel] = createSignal(""); | ||
| 75 | + const [page, setPage] = createSignal(0); | ||
| 76 | + const [error, setError] = createSignal(""); | ||
| 77 | + const [editTarget, setEditTarget] = createSignal<WorkEntry | null>(null); | ||
| 78 | + const [deleteTarget, setDeleteTarget] = createSignal<WorkEntry | null>(null); | ||
| 79 | + const [checkInDate, setCheckInDate] = createSignal(""); | ||
| 80 | + const [checkInTime, setCheckInTime] = createSignal(""); | ||
| 81 | + const [checkOutDate, setCheckOutDate] = createSignal(""); | ||
| 82 | + const [checkOutTime, setCheckOutTime] = createSignal(""); | ||
| 83 | + const [pending, setPending] = createSignal(false); | ||
| 84 | + | ||
| 85 | + const [list, { refetch }] = createResource( | ||
| 86 | + () => (lookupId() ? `${lookupId()}|${page()}` : null), | ||
| 87 | + async (key) => { | ||
| 88 | + if (!key) return { items: [] as WorkEntry[], total: 0 }; | ||
| 89 | + const id = key.split("|")[0]!; | ||
| 90 | + return loadListPage({ | ||
| 91 | + page: page(), | ||
| 92 | + pageSize: WORK_PAGE_SIZE, | ||
| 93 | + clientMode: false, | ||
| 94 | + fetch: (params) => getUserWorkLog(id, params), | ||
| 95 | + }); | ||
| 96 | + }, | ||
| 97 | + ); | ||
| 98 | + | ||
| 99 | + const total = () => list()?.total ?? 0; | ||
| 100 | + const pageItems = () => list()?.items ?? []; | ||
| 101 | + const totalPages = createMemo(() => pagesOf(total(), WORK_PAGE_SIZE)); | ||
| 102 | + const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); | ||
| 103 | + | ||
| 104 | + const startEdit = (entry: WorkEntry) => { | ||
| 105 | + if (entry.check_out == null) { | ||
| 106 | + setError(t("work.cannotEditOpen")); | ||
| 107 | + return; | ||
| 108 | + } | ||
| 109 | + setError(""); | ||
| 110 | + setEditTarget(entry); | ||
| 111 | + setCheckInDate(msToDateInput(entry.check_in)); | ||
| 112 | + setCheckInTime(msToTimeInput(entry.check_in)); | ||
| 113 | + setCheckOutDate(msToDateInput(entry.check_out)); | ||
| 114 | + setCheckOutTime(msToTimeInput(entry.check_out)); | ||
| 115 | + }; | ||
| 116 | + | ||
| 117 | + const saveEdit = async (e: SubmitEvent) => { | ||
| 118 | + e.preventDefault(); | ||
| 119 | + const entry = editTarget(); | ||
| 120 | + if (!entry) return; | ||
| 121 | + setError(""); | ||
| 122 | + const check_in = dateInputToMs(checkInDate(), checkInTime()); | ||
| 123 | + const check_out = dateInputToMs(checkOutDate(), checkOutTime()); | ||
| 124 | + if (check_in == null || check_out == null) { | ||
| 125 | + setError(t("work.timesRequired")); | ||
| 126 | + return; | ||
| 127 | + } | ||
| 128 | + if (check_out < check_in) { | ||
| 129 | + setError(t("form.timeOrder")); | ||
| 130 | + return; | ||
| 131 | + } | ||
| 132 | + setPending(true); | ||
| 133 | + try { | ||
| 134 | + await patchWorkEntryById(entry.id, { check_in, check_out }); | ||
| 135 | + setEditTarget(null); | ||
| 136 | + await refetch(); | ||
| 137 | + } catch (err) { | ||
| 138 | + setError(formatApiError(err)); | ||
| 139 | + } finally { | ||
| 140 | + setPending(false); | ||
| 141 | + } | ||
| 142 | + }; | ||
| 143 | + | ||
| 144 | + return ( | ||
| 145 | + <div class="space-y-6"> | ||
| 146 | + <div class="space-y-2"> | ||
| 147 | + <div class="flex flex-wrap items-center gap-1 text-xs text-muted-foreground"> | ||
| 148 | + <span>{t("nav.admin")}</span> | ||
| 149 | + <span>/</span> | ||
| 150 | + <span>{t("nav.staffWork")}</span> | ||
| 151 | + </div> | ||
| 152 | + <PageHeader accent="amber" eyebrow={t("nav.admin")} title={t("work.staffTitle")} description={t("work.staffSubtitle")} /> | ||
| 153 | + </div> | ||
| 154 | + | ||
| 155 | + <section class="data-shell space-y-4 p-4"> | ||
| 156 | + <form | ||
| 157 | + class="grid gap-2 sm:grid-cols-[minmax(0,1fr)_auto] sm:items-end" | ||
| 158 | + onSubmit={(e) => { | ||
| 159 | + e.preventDefault(); | ||
| 160 | + const id = userId().trim(); | ||
| 161 | + if (!id) return; | ||
| 162 | + setLookupId(id); | ||
| 163 | + setLookupLabel(personLabel(selectedUser()) === "—" ? id : personLabel(selectedUser())); | ||
| 164 | + setPage(0); | ||
| 165 | + }} | ||
| 166 | + > | ||
| 167 | + <UserSearchSelect | ||
| 168 | + id="staff-work-user" | ||
| 169 | + label={t("marks.userIdentity")} | ||
| 170 | + value={userId()} | ||
| 171 | + onChange={setUserId} | ||
| 172 | + onSelectUser={setSelectedUser} | ||
| 173 | + placeholder={t("common.searchPlaceholder")} | ||
| 174 | + selectPlaceholder={t("marks.userIdentity")} | ||
| 175 | + emptyMessage={t("admin.noUsers")} | ||
| 176 | + allowManualValue | ||
| 177 | + /> | ||
| 178 | + <Button type="submit" class="h-9 w-full rounded-lg sm:w-auto"> | ||
| 179 | + {t("work.show")} | ||
| 180 | + </Button> | ||
| 181 | + </form> | ||
| 182 | + <Show when={error() && !editTarget()}> | ||
| 183 | + <Alert variant="destructive">{error()}</Alert> | ||
| 184 | + </Show> | ||
| 185 | + </section> | ||
| 186 | + | ||
| 187 | + <Show when={lookupId()}> | ||
| 188 | + <section class="data-shell space-y-4 p-4"> | ||
| 189 | + <div class="flex flex-wrap items-center justify-between gap-2"> | ||
| 190 | + <div> | ||
| 191 | + <h2 class="font-display text-lg font-semibold">{t("work.entries")}</h2> | ||
| 192 | + <p class="mt-1 text-sm text-muted-foreground">{t("work.forUser", { user: lookupLabel() || lookupId() || "" })}</p> | ||
| 193 | + </div> | ||
| 194 | + <Badge variant="secondary" class="mono rounded-sm px-3 py-1"> | ||
| 195 | + {total()} | ||
| 196 | + </Badge> | ||
| 197 | + </div> | ||
| 198 | + <Suspense fallback={<PageSpinner />}> | ||
| 199 | + <Show when={list.error}> | ||
| 200 | + <ErrorAlert message={formatApiError(list.error)} onRetry={() => void refetch()} /> | ||
| 201 | + </Show> | ||
| 202 | + <Show | ||
| 203 | + when={pageItems().length > 0} | ||
| 204 | + fallback={<div class="rounded-lg border border-dashed border-border/80 bg-muted/20 px-4 py-8 text-center text-sm text-muted-foreground">{t("work.empty")}</div>} | ||
| 205 | + > | ||
| 206 | + <DataTableFrame> | ||
| 207 | + <Table class="data-table"> | ||
| 208 | + <TableHeader> | ||
| 209 | + <TableRow> | ||
| 210 | + <TableHead>{t("work.checkIn")}</TableHead> | ||
| 211 | + <TableHead>{t("work.checkOut")}</TableHead> | ||
| 212 | + <TableHead>{t("work.duration")}</TableHead> | ||
| 213 | + <TableHead>{t("work.status")}</TableHead> | ||
| 214 | + <TableHead class="w-14 text-center">{t("common.actions")}</TableHead> | ||
| 215 | + </TableRow> | ||
| 216 | + </TableHeader> | ||
| 217 | + <TableBody> | ||
| 218 | + <For each={pageItems()}> | ||
| 219 | + {(entry) => ( | ||
| 220 | + <TableRow> | ||
| 221 | + <TableCell class="mono">{formatDateTime(entry.check_in, locale())}</TableCell> | ||
| 222 | + <TableCell class="mono">{formatDateTime(entry.check_out, locale())}</TableCell> | ||
| 223 | + <TableCell class="mono">{formatDurationMinutes(entry.duration_ms, locale())}</TableCell> | ||
| 224 | + <TableCell> | ||
| 225 | + <Badge variant={entry.check_out == null ? "default" : "secondary"} class="rounded-sm"> | ||
| 226 | + {entry.check_out == null ? t("work.open") : t("work.closed")} | ||
| 227 | + </Badge> | ||
| 228 | + </TableCell> | ||
| 229 | + <TableCell> | ||
| 230 | + <TableRowActions | ||
| 231 | + label={t("common.actions")} | ||
| 232 | + actions={[ | ||
| 233 | + { | ||
| 234 | + label: t("common.edit"), | ||
| 235 | + icon: <IconEdit class="h-4 w-4" />, | ||
| 236 | + disabled: entry.check_out == null, | ||
| 237 | + onSelect: () => startEdit(entry), | ||
| 238 | + }, | ||
| 239 | + { | ||
| 240 | + label: t("common.delete"), | ||
| 241 | + icon: <IconTrash class="h-4 w-4" />, | ||
| 242 | + destructive: true, | ||
| 243 | + onSelect: () => setDeleteTarget(entry), | ||
| 244 | + }, | ||
| 245 | + ]} | ||
| 246 | + /> | ||
| 247 | + </TableCell> | ||
| 248 | + </TableRow> | ||
| 249 | + )} | ||
| 250 | + </For> | ||
| 251 | + </TableBody> | ||
| 252 | + </Table> | ||
| 253 | + </DataTableFrame> | ||
| 254 | + <Show when={total() > WORK_PAGE_SIZE}> | ||
| 255 | + <PaginationControls page={safePage()} totalPages={totalPages()} onPageChange={setPage} /> | ||
| 256 | + </Show> | ||
| 257 | + </Show> | ||
| 258 | + </Suspense> | ||
| 259 | + </section> | ||
| 260 | + </Show> | ||
| 261 | + | ||
| 262 | + <SidePanel | ||
| 263 | + open={editTarget() != null} | ||
| 264 | + onOpenChange={(open) => { | ||
| 265 | + if (!open) { | ||
| 266 | + setEditTarget(null); | ||
| 267 | + setError(""); | ||
| 268 | + } | ||
| 269 | + }} | ||
| 270 | + title={t("work.correct")} | ||
| 271 | + description={t("work.correctHelp")} | ||
| 272 | + > | ||
| 273 | + <form class="space-y-4" onSubmit={saveEdit}> | ||
| 274 | + <Show when={error()}> | ||
| 275 | + <Alert variant="destructive">{error()}</Alert> | ||
| 276 | + </Show> | ||
| 277 | + <div class="space-y-1.5"> | ||
| 278 | + <Label>{t("work.checkIn")}</Label> | ||
| 279 | + <div class="grid grid-cols-2 gap-2"> | ||
| 280 | + <DatePicker id="staff-check-in-date" class="h-10" placeholder={t("form.datePlaceholder")} value={checkInDate()} required onChange={setCheckInDate} /> | ||
| 281 | + <Input class="h-10 rounded-sm font-mono" placeholder="09:00" value={checkInTime()} required onInput={(e) => setCheckInTime(e.currentTarget.value)} /> | ||
| 282 | + </div> | ||
| 283 | + </div> | ||
| 284 | + <div class="space-y-1.5"> | ||
| 285 | + <Label>{t("work.checkOut")}</Label> | ||
| 286 | + <div class="grid grid-cols-2 gap-2"> | ||
| 287 | + <DatePicker id="staff-check-out-date" class="h-10" placeholder={t("form.datePlaceholder")} value={checkOutDate()} required onChange={setCheckOutDate} /> | ||
| 288 | + <Input class="h-10 rounded-sm font-mono" placeholder="17:00" value={checkOutTime()} required onInput={(e) => setCheckOutTime(e.currentTarget.value)} /> | ||
| 289 | + </div> | ||
| 290 | + </div> | ||
| 291 | + <div class="flex flex-wrap gap-2"> | ||
| 292 | + <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={() => setEditTarget(null)}> | ||
| 293 | + {t("common.cancel")} | ||
| 294 | + </Button> | ||
| 295 | + <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> | ||
| 296 | + {t("common.update")} | ||
| 297 | + </Button> | ||
| 298 | + </div> | ||
| 299 | + </form> | ||
| 300 | + </SidePanel> | ||
| 301 | + | ||
| 302 | + <ConfirmDialog | ||
| 303 | + open={deleteTarget() != null} | ||
| 304 | + onOpenChange={(open) => !open && setDeleteTarget(null)} | ||
| 305 | + title={t("confirm.deleteTitle")} | ||
| 306 | + variant="destructive" | ||
| 307 | + summary={t("work.deleteSummary", { | ||
| 308 | + time: deleteTarget() ? formatDateTime(deleteTarget()!.check_in, locale()) : "", | ||
| 309 | + })} | ||
| 310 | + onConfirm={async () => { | ||
| 311 | + const entry = deleteTarget(); | ||
| 312 | + if (!entry) return; | ||
| 313 | + try { | ||
| 314 | + await deleteWorkEntryById(entry.id); | ||
| 315 | + await refetch(); | ||
| 316 | + } catch (err) { | ||
| 317 | + setError(formatApiError(err)); | ||
| 318 | + } finally { | ||
| 319 | + setDeleteTarget(null); | ||
| 320 | + } | ||
| 321 | + }} | ||
| 322 | + /> | ||
| 323 | + </div> | ||
| 324 | + ); | ||
| 325 | +} | ||
src/routes/router.tsxmodified+48 -0
| @@ -40,6 +40,7 @@ const StudentMarksPage = lazyRoute(() => import("@/pages/student-marks-page")); | |||
| 40 | const AttendancePage = lazyRoute(() => import("@/pages/attendance-page")); | 40 | const AttendancePage = lazyRoute(() => import("@/pages/attendance-page")); |
| 41 | const StudentAttendancePage = lazyRoute(() => import("@/pages/student-attendance-page")); | 41 | const StudentAttendancePage = lazyRoute(() => import("@/pages/student-attendance-page")); |
| 42 | const WorkLogPage = lazyRoute(() => import("@/pages/work-log-page")); | 42 | const WorkLogPage = lazyRoute(() => import("@/pages/work-log-page")); |
| 43 | +const StaffWorkPage = lazyRoute(() => import("@/pages/staff-work-page")); | ||
| 43 | const ProfilePage = lazyRoute(() => import("@/pages/profile-page")); | 44 | const ProfilePage = lazyRoute(() => import("@/pages/profile-page")); |
| 44 | const LiveMonitorPage = lazyRoute(() => import("@/pages/live-monitor-page")); | 45 | const LiveMonitorPage = lazyRoute(() => import("@/pages/live-monitor-page")); |
| 45 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); | 46 | const AdminUsersPage = lazyRoute(() => import("@/pages/admin-users-page")); |
| @@ -136,6 +137,16 @@ const courseDetailRoute = createRoute({ | |||
| 136 | const marksRoute = createRoute({ | 137 | const marksRoute = createRoute({ |
| 137 | getParentRoute: () => rootRoute, | 138 | getParentRoute: () => rootRoute, |
| 138 | path: "/marks", | 139 | path: "/marks", |
| 140 | + beforeLoad: async () => { | ||
| 141 | + try { | ||
| 142 | + const user = await getMe(); | ||
| 143 | + if (user.role !== "student") throw redirect({ to: "/" }); | ||
| 144 | + } catch (err) { | ||
| 145 | + if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); | ||
| 146 | + if (isRedirect(err)) throw err; | ||
| 147 | + throw redirect({ to: "/login" }); | ||
| 148 | + } | ||
| 149 | + }, | ||
| 139 | component: MarksPage, | 150 | component: MarksPage, |
| 140 | }); | 151 | }); |
| 141 | 152 | ||
| @@ -162,6 +173,16 @@ const studentMarksRoute = createRoute({ | |||
| 162 | const attendanceRoute = createRoute({ | 173 | const attendanceRoute = createRoute({ |
| 163 | getParentRoute: () => rootRoute, | 174 | getParentRoute: () => rootRoute, |
| 164 | path: "/attendance", | 175 | path: "/attendance", |
| 176 | + beforeLoad: async () => { | ||
| 177 | + try { | ||
| 178 | + const user = await getMe(); | ||
| 179 | + if (user.role !== "student") throw redirect({ to: "/" }); | ||
| 180 | + } catch (err) { | ||
| 181 | + if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); | ||
| 182 | + if (isRedirect(err)) throw err; | ||
| 183 | + throw redirect({ to: "/login" }); | ||
| 184 | + } | ||
| 185 | + }, | ||
| 165 | component: AttendancePage, | 186 | component: AttendancePage, |
| 166 | }); | 187 | }); |
| 167 | 188 | ||
| @@ -188,9 +209,35 @@ const studentAttendanceRoute = createRoute({ | |||
| 188 | const workRoute = createRoute({ | 209 | const workRoute = createRoute({ |
| 189 | getParentRoute: () => rootRoute, | 210 | getParentRoute: () => rootRoute, |
| 190 | path: "/work", | 211 | path: "/work", |
| 212 | + beforeLoad: async () => { | ||
| 213 | + try { | ||
| 214 | + const user = await getMe(); | ||
| 215 | + if (!hasMinRole(user.role, "teacher")) throw redirect({ to: "/" }); | ||
| 216 | + } catch (err) { | ||
| 217 | + if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); | ||
| 218 | + if (isRedirect(err)) throw err; | ||
| 219 | + throw redirect({ to: "/login" }); | ||
| 220 | + } | ||
| 221 | + }, | ||
| 191 | component: WorkLogPage, | 222 | component: WorkLogPage, |
| 192 | }); | 223 | }); |
| 193 | 224 | ||
| 225 | +const staffWorkRoute = createRoute({ | ||
| 226 | + getParentRoute: () => rootRoute, | ||
| 227 | + path: "/management/staff-work", | ||
| 228 | + beforeLoad: async () => { | ||
| 229 | + try { | ||
| 230 | + const user = await getMe(); | ||
| 231 | + if (!hasMinRole(user.role, "manager")) throw redirect({ to: "/" }); | ||
| 232 | + } catch (err) { | ||
| 233 | + if (err instanceof ApiError && err.status === 401) throw redirect({ to: "/login" }); | ||
| 234 | + if (isRedirect(err)) throw err; | ||
| 235 | + throw redirect({ to: "/login" }); | ||
| 236 | + } | ||
| 237 | + }, | ||
| 238 | + component: StaffWorkPage, | ||
| 239 | +}); | ||
| 240 | + | ||
| 194 | const settingsRoute = createRoute({ | 241 | const settingsRoute = createRoute({ |
| 195 | getParentRoute: () => rootRoute, | 242 | getParentRoute: () => rootRoute, |
| 196 | path: "/management/settings", | 243 | path: "/management/settings", |
| @@ -259,6 +306,7 @@ const routeTree = rootRoute.addChildren([ | |||
| 259 | attendanceRoute, | 306 | attendanceRoute, |
| 260 | studentAttendanceRoute, | 307 | studentAttendanceRoute, |
| 261 | workRoute, | 308 | workRoute, |
| 309 | + staffWorkRoute, | ||
| 262 | settingsRoute, | 310 | settingsRoute, |
| 263 | termsRoute, | 311 | termsRoute, |
| 264 | profileRoute, | 312 | profileRoute, |