diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx new file mode 100644 index 0000000..10a2702 --- /dev/null +++ b/src/pages/Home.tsx @@ -0,0 +1,128 @@ +// Landing dashboard: greeting, quick stats, upcoming events, and the caller's +// courses. Every tile links into the section it summarizes. + +import { A } from "@solidjs/router"; +import { For, Show, createResource } from "solid-js"; +import { Empty, Loading } from "../components/Feedback"; +import { IconBook, IconCalendar, IconChart, IconNote } from "../components/Icons"; +import { courses, events, marks, notes } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import { formatWindow } from "../lib/format"; + +export default function Home() { + const { user } = useAuth(); + const [eventList] = createResource(events.list); + const [myCourses] = createResource(courses.mine); + const [report] = createResource(marks.mine); + const [noteList] = createResource(notes.list); + + // Ongoing or future events, soonest first; unscheduled ones sort last. + const upcoming = () => { + const now = Date.now(); + return (eventList() ?? []) + .filter((e) => (e.ends_at ?? e.starts_at ?? now) >= now) + .sort((a, b) => (a.starts_at ?? Infinity) - (b.starts_at ?? Infinity)); + }; + + const greeting = () => { + const hour = new Date().getHours(); + const part = hour < 12 ? "morning" : hour < 18 ? "afternoon" : "evening"; + return `Good ${part}, ${user()?.name ?? user()?.username}`; + }; + const today = new Intl.DateTimeFormat(undefined, { dateStyle: "full" }).format( + new Date(), + ); + + const average = () => { + const value = report()?.overall_average; + return value == null ? null : Math.round(value * 10) / 10; + }; + + return ( +
+
+
+

{greeting()}

+

{today}

+
+
+ +
+ + + Notes + + {noteList()?.length ?? "—"} + + + + Upcoming events + + {eventList() ? upcoming().length : "—"} + + + + My courses + + {myCourses()?.length ?? "—"} + + + + Average + + + {average() ?? "—"} + + / 100 + + + +
+ +
+
+
+

Upcoming events

+ View all +
+ }> + Nothing scheduled.}> + + {(event) => ( + +

{event.title}

+

{formatWindow(event.starts_at, event.ends_at)}

+
+ )} +
+
+
+
+ +
+
+

My courses

+ View all +
+ }> + Not enrolled in any course yet.} + > + + {(course) => ( + +

{course.title}

+ +

{course.description}

+
+
+ )} +
+
+
+
+
+
+ ); +} diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx new file mode 100644 index 0000000..05366e8 --- /dev/null +++ b/src/pages/Profile.tsx @@ -0,0 +1,68 @@ +// The caller's own account: fixed facts (username, role, id) plus the editable +// personal info. Saving patches /users/me and refreshes the session user, so +// the sidebar chip updates instantly. + +import { Show, createSignal } from "solid-js"; +import { ErrorLine } from "../components/Feedback"; +import { ProfileFields, profilePatch } from "../components/ProfileFields"; +import { createAction } from "../lib/action"; +import { users } from "../lib/api"; +import { useAuth } from "../lib/auth"; + +export default function Profile() { + const { user, update } = useAuth(); + const [saved, setSaved] = createSignal(false); + + const save = createAction(async (form: HTMLFormElement) => { + setSaved(false); + update(await users.updateMyProfile(profilePatch(form))); + setSaved(true); + }); + + return ( + + {(current) => ( +
+
+
+ + {(current().name ?? current().username).slice(0, 1)} + +
+

+ {[current().name, current().surname].filter(Boolean).join(" ") || + current().username} +

+

+ {current().username} · {current().role} ·{" "} + {current().id} +

+
+
+
+ +
{ + e.preventDefault(); + void save.run(e.currentTarget); + }} + > +

Personal info

+

Leave a field empty to clear it.

+ + + + + + Saved. + + + +
+ )} +
+ ); +} diff --git a/src/pages/Users.tsx b/src/pages/Users.tsx new file mode 100644 index 0000000..4ecc636 --- /dev/null +++ b/src/pages/Users.tsx @@ -0,0 +1,142 @@ +// Admin console: every user with their personal info, a role selector, and an +// inline profile editor (the school-office path for maintaining records). The +// backend forbids changing your own role, so that selector is read-only. + +import { For, Show, createResource, createSignal } from "solid-js"; +import { Empty, ErrorLine, Loading } from "../components/Feedback"; +import { ProfileFields, profilePatch } from "../components/ProfileFields"; +import { createAction } from "../lib/action"; +import { users } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import { ROLES, type Role, type User } from "../lib/types"; + +export default function Users() { + const { user, update, can } = useAuth(); + const [list, { mutate }] = createResource(users.list); + const [editing, setEditing] = createSignal(null); + + const replace = (updated: User) => { + mutate((current) => current?.map((u) => (u.id === updated.id ? updated : u))); + // Editing yourself must also refresh the session user (top-bar chip). + if (updated.id === user()?.id) update(updated); + }; + + const setRole = createAction(async (target: User, role: Role) => { + replace(await users.setRole(target.id, role)); + }); + + const saveProfile = createAction(async (target: User, form: HTMLFormElement) => { + replace(await users.updateProfile(target.id, profilePatch(form))); + setEditing(null); + }); + + const toggleEdit = (id: string) => { + setEditing((current) => (current === id ? null : id)); + saveProfile.clearError(); + }; + + return ( + Admins only.}> +
+
+
+

Users

+

Every account, with roles and personal records.

+
+
+ + }> + No users.}> +
+ + + + + + + + + + + + + {(entry) => ( + <> + + + + + + + + + + + + + + + )} + + +
UserIdContactBornRole +
+ {entry.username} + +
+ + {[entry.name, entry.surname].filter(Boolean).join(" ")} + +
+
{entry.id} + {[entry.email, entry.phone].filter(Boolean).join(" · ") || "—"} + {entry.birth_date ?? "—"} + {entry.role} (you)} + > + + + + +
+
{ + e.preventDefault(); + void saveProfile.run(entry, e.currentTarget); + }} + > +

Leave a field empty to clear it.

+ + + + + + +
+
+
+
+
+
+ ); +}