✨ add home dashboard, profile, and admin users pages
Browse files at this commit79b5dd217ea3a2a8a59dc8bb2945bf96896f6d06Parent: 5efbaa3- feat
- ✨ add home dashboard, profile, and admin users pages
3 changed files +338 -0
- src/pages/Home.tsx +128 -0
- src/pages/Profile.tsx +68 -0
- src/pages/Users.tsx +142 -0
src/pages/Home.tsxadded+128 -0
| @@ -0,0 +1,128 @@ | |||
| 1 | +// Landing dashboard: greeting, quick stats, upcoming events, and the caller's | ||
| 2 | +// courses. Every tile links into the section it summarizes. | ||
| 3 | + | ||
| 4 | +import { A } from "@solidjs/router"; | ||
| 5 | +import { For, Show, createResource } from "solid-js"; | ||
| 6 | +import { Empty, Loading } from "../components/Feedback"; | ||
| 7 | +import { IconBook, IconCalendar, IconChart, IconNote } from "../components/Icons"; | ||
| 8 | +import { courses, events, marks, notes } from "../lib/api"; | ||
| 9 | +import { useAuth } from "../lib/auth"; | ||
| 10 | +import { formatWindow } from "../lib/format"; | ||
| 11 | + | ||
| 12 | +export default function Home() { | ||
| 13 | + const { user } = useAuth(); | ||
| 14 | + const [eventList] = createResource(events.list); | ||
| 15 | + const [myCourses] = createResource(courses.mine); | ||
| 16 | + const [report] = createResource(marks.mine); | ||
| 17 | + const [noteList] = createResource(notes.list); | ||
| 18 | + | ||
| 19 | + // Ongoing or future events, soonest first; unscheduled ones sort last. | ||
| 20 | + const upcoming = () => { | ||
| 21 | + const now = Date.now(); | ||
| 22 | + return (eventList() ?? []) | ||
| 23 | + .filter((e) => (e.ends_at ?? e.starts_at ?? now) >= now) | ||
| 24 | + .sort((a, b) => (a.starts_at ?? Infinity) - (b.starts_at ?? Infinity)); | ||
| 25 | + }; | ||
| 26 | + | ||
| 27 | + const greeting = () => { | ||
| 28 | + const hour = new Date().getHours(); | ||
| 29 | + const part = hour < 12 ? "morning" : hour < 18 ? "afternoon" : "evening"; | ||
| 30 | + return `Good ${part}, ${user()?.name ?? user()?.username}`; | ||
| 31 | + }; | ||
| 32 | + const today = new Intl.DateTimeFormat(undefined, { dateStyle: "full" }).format( | ||
| 33 | + new Date(), | ||
| 34 | + ); | ||
| 35 | + | ||
| 36 | + const average = () => { | ||
| 37 | + const value = report()?.overall_average; | ||
| 38 | + return value == null ? null : Math.round(value * 10) / 10; | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + return ( | ||
| 42 | + <section class="stack-lg"> | ||
| 43 | + <header class="page-head"> | ||
| 44 | + <div> | ||
| 45 | + <h1>{greeting()}</h1> | ||
| 46 | + <p class="sub">{today}</p> | ||
| 47 | + </div> | ||
| 48 | + </header> | ||
| 49 | + | ||
| 50 | + <div class="stats"> | ||
| 51 | + <A href="/notes" class="card link-card stat"> | ||
| 52 | + <span class="stat-label"> | ||
| 53 | + <IconNote /> Notes | ||
| 54 | + </span> | ||
| 55 | + <span class="stat-value">{noteList()?.length ?? "—"}</span> | ||
| 56 | + </A> | ||
| 57 | + <A href="/events" class="card link-card stat"> | ||
| 58 | + <span class="stat-label"> | ||
| 59 | + <IconCalendar /> Upcoming events | ||
| 60 | + </span> | ||
| 61 | + <span class="stat-value">{eventList() ? upcoming().length : "—"}</span> | ||
| 62 | + </A> | ||
| 63 | + <A href="/courses" class="card link-card stat"> | ||
| 64 | + <span class="stat-label"> | ||
| 65 | + <IconBook /> My courses | ||
| 66 | + </span> | ||
| 67 | + <span class="stat-value">{myCourses()?.length ?? "—"}</span> | ||
| 68 | + </A> | ||
| 69 | + <A href="/marks" class="card link-card stat"> | ||
| 70 | + <span class="stat-label"> | ||
| 71 | + <IconChart /> Average | ||
| 72 | + </span> | ||
| 73 | + <span class="stat-value"> | ||
| 74 | + {average() ?? "—"} | ||
| 75 | + <Show when={average() !== null}> | ||
| 76 | + <small> / 100</small> | ||
| 77 | + </Show> | ||
| 78 | + </span> | ||
| 79 | + </A> | ||
| 80 | + </div> | ||
| 81 | + | ||
| 82 | + <div class="cols"> | ||
| 83 | + <section class="stack"> | ||
| 84 | + <div class="section-head"> | ||
| 85 | + <h2>Upcoming events</h2> | ||
| 86 | + <A href="/events">View all</A> | ||
| 87 | + </div> | ||
| 88 | + <Show when={!eventList.loading} fallback={<Loading />}> | ||
| 89 | + <Show when={upcoming().length} fallback={<Empty>Nothing scheduled.</Empty>}> | ||
| 90 | + <For each={upcoming().slice(0, 4)}> | ||
| 91 | + {(event) => ( | ||
| 92 | + <A href={`/events/${event.id}`} class="card link-card stack"> | ||
| 93 | + <h3>{event.title}</h3> | ||
| 94 | + <p class="meta">{formatWindow(event.starts_at, event.ends_at)}</p> | ||
| 95 | + </A> | ||
| 96 | + )} | ||
| 97 | + </For> | ||
| 98 | + </Show> | ||
| 99 | + </Show> | ||
| 100 | + </section> | ||
| 101 | + | ||
| 102 | + <section class="stack"> | ||
| 103 | + <div class="section-head"> | ||
| 104 | + <h2>My courses</h2> | ||
| 105 | + <A href="/courses">View all</A> | ||
| 106 | + </div> | ||
| 107 | + <Show when={!myCourses.loading} fallback={<Loading />}> | ||
| 108 | + <Show | ||
| 109 | + when={myCourses()?.length} | ||
| 110 | + fallback={<Empty>Not enrolled in any course yet.</Empty>} | ||
| 111 | + > | ||
| 112 | + <For each={myCourses()?.slice(0, 4)}> | ||
| 113 | + {(course) => ( | ||
| 114 | + <A href={`/courses/${course.id}`} class="card link-card stack"> | ||
| 115 | + <h3>{course.title}</h3> | ||
| 116 | + <Show when={course.description}> | ||
| 117 | + <p class="muted clamp">{course.description}</p> | ||
| 118 | + </Show> | ||
| 119 | + </A> | ||
| 120 | + )} | ||
| 121 | + </For> | ||
| 122 | + </Show> | ||
| 123 | + </Show> | ||
| 124 | + </section> | ||
| 125 | + </div> | ||
| 126 | + </section> | ||
| 127 | + ); | ||
| 128 | +} | ||
src/pages/Profile.tsxadded+68 -0
| @@ -0,0 +1,68 @@ | |||
| 1 | +// The caller's own account: fixed facts (username, role, id) plus the editable | ||
| 2 | +// personal info. Saving patches /users/me and refreshes the session user, so | ||
| 3 | +// the sidebar chip updates instantly. | ||
| 4 | + | ||
| 5 | +import { Show, createSignal } from "solid-js"; | ||
| 6 | +import { ErrorLine } from "../components/Feedback"; | ||
| 7 | +import { ProfileFields, profilePatch } from "../components/ProfileFields"; | ||
| 8 | +import { createAction } from "../lib/action"; | ||
| 9 | +import { users } from "../lib/api"; | ||
| 10 | +import { useAuth } from "../lib/auth"; | ||
| 11 | + | ||
| 12 | +export default function Profile() { | ||
| 13 | + const { user, update } = useAuth(); | ||
| 14 | + const [saved, setSaved] = createSignal(false); | ||
| 15 | + | ||
| 16 | + const save = createAction(async (form: HTMLFormElement) => { | ||
| 17 | + setSaved(false); | ||
| 18 | + update(await users.updateMyProfile(profilePatch(form))); | ||
| 19 | + setSaved(true); | ||
| 20 | + }); | ||
| 21 | + | ||
| 22 | + return ( | ||
| 23 | + <Show when={user()}> | ||
| 24 | + {(current) => ( | ||
| 25 | + <section class="page"> | ||
| 26 | + <header class="page-head"> | ||
| 27 | + <div class="row"> | ||
| 28 | + <span class="avatar avatar-lg"> | ||
| 29 | + {(current().name ?? current().username).slice(0, 1)} | ||
| 30 | + </span> | ||
| 31 | + <div> | ||
| 32 | + <h1> | ||
| 33 | + {[current().name, current().surname].filter(Boolean).join(" ") || | ||
| 34 | + current().username} | ||
| 35 | + </h1> | ||
| 36 | + <p class="sub"> | ||
| 37 | + {current().username} · <span class="badge">{current().role}</span> ·{" "} | ||
| 38 | + <span class="mono">{current().id}</span> | ||
| 39 | + </p> | ||
| 40 | + </div> | ||
| 41 | + </div> | ||
| 42 | + </header> | ||
| 43 | + | ||
| 44 | + <form | ||
| 45 | + class="card stack" | ||
| 46 | + onSubmit={(e) => { | ||
| 47 | + e.preventDefault(); | ||
| 48 | + void save.run(e.currentTarget); | ||
| 49 | + }} | ||
| 50 | + > | ||
| 51 | + <h2>Personal info</h2> | ||
| 52 | + <p class="meta">Leave a field empty to clear it.</p> | ||
| 53 | + <ProfileFields user={current()} /> | ||
| 54 | + <ErrorLine error={save.error()} /> | ||
| 55 | + <span class="row-actions"> | ||
| 56 | + <button type="submit" disabled={save.pending()}> | ||
| 57 | + Save | ||
| 58 | + </button> | ||
| 59 | + <Show when={saved() && !save.pending()}> | ||
| 60 | + <span class="meta">Saved.</span> | ||
| 61 | + </Show> | ||
| 62 | + </span> | ||
| 63 | + </form> | ||
| 64 | + </section> | ||
| 65 | + )} | ||
| 66 | + </Show> | ||
| 67 | + ); | ||
| 68 | +} | ||
src/pages/Users.tsxadded+142 -0
| @@ -0,0 +1,142 @@ | |||
| 1 | +// Admin console: every user with their personal info, a role selector, and an | ||
| 2 | +// inline profile editor (the school-office path for maintaining records). The | ||
| 3 | +// backend forbids changing your own role, so that selector is read-only. | ||
| 4 | + | ||
| 5 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 6 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 7 | +import { ProfileFields, profilePatch } from "../components/ProfileFields"; | ||
| 8 | +import { createAction } from "../lib/action"; | ||
| 9 | +import { users } from "../lib/api"; | ||
| 10 | +import { useAuth } from "../lib/auth"; | ||
| 11 | +import { ROLES, type Role, type User } from "../lib/types"; | ||
| 12 | + | ||
| 13 | +export default function Users() { | ||
| 14 | + const { user, update, can } = useAuth(); | ||
| 15 | + const [list, { mutate }] = createResource(users.list); | ||
| 16 | + const [editing, setEditing] = createSignal<string | null>(null); | ||
| 17 | + | ||
| 18 | + const replace = (updated: User) => { | ||
| 19 | + mutate((current) => current?.map((u) => (u.id === updated.id ? updated : u))); | ||
| 20 | + // Editing yourself must also refresh the session user (top-bar chip). | ||
| 21 | + if (updated.id === user()?.id) update(updated); | ||
| 22 | + }; | ||
| 23 | + | ||
| 24 | + const setRole = createAction(async (target: User, role: Role) => { | ||
| 25 | + replace(await users.setRole(target.id, role)); | ||
| 26 | + }); | ||
| 27 | + | ||
| 28 | + const saveProfile = createAction(async (target: User, form: HTMLFormElement) => { | ||
| 29 | + replace(await users.updateProfile(target.id, profilePatch(form))); | ||
| 30 | + setEditing(null); | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + const toggleEdit = (id: string) => { | ||
| 34 | + setEditing((current) => (current === id ? null : id)); | ||
| 35 | + saveProfile.clearError(); | ||
| 36 | + }; | ||
| 37 | + | ||
| 38 | + return ( | ||
| 39 | + <Show when={can("admin")} fallback={<Empty>Admins only.</Empty>}> | ||
| 40 | + <section class="page"> | ||
| 41 | + <header class="page-head"> | ||
| 42 | + <div> | ||
| 43 | + <h1>Users</h1> | ||
| 44 | + <p class="sub">Every account, with roles and personal records.</p> | ||
| 45 | + </div> | ||
| 46 | + </header> | ||
| 47 | + <ErrorLine error={setRole.error()} /> | ||
| 48 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 49 | + <Show when={list()?.length} fallback={<Empty>No users.</Empty>}> | ||
| 50 | + <div class="table-wrap"> | ||
| 51 | + <table> | ||
| 52 | + <thead> | ||
| 53 | + <tr> | ||
| 54 | + <th>User</th> | ||
| 55 | + <th>Id</th> | ||
| 56 | + <th>Contact</th> | ||
| 57 | + <th>Born</th> | ||
| 58 | + <th>Role</th> | ||
| 59 | + <th /> | ||
| 60 | + </tr> | ||
| 61 | + </thead> | ||
| 62 | + <tbody> | ||
| 63 | + <For each={list()}> | ||
| 64 | + {(entry) => ( | ||
| 65 | + <> | ||
| 66 | + <tr> | ||
| 67 | + <td> | ||
| 68 | + {entry.username} | ||
| 69 | + <Show when={entry.name || entry.surname}> | ||
| 70 | + <br /> | ||
| 71 | + <span class="meta"> | ||
| 72 | + {[entry.name, entry.surname].filter(Boolean).join(" ")} | ||
| 73 | + </span> | ||
| 74 | + </Show> | ||
| 75 | + </td> | ||
| 76 | + <td class="mono">{entry.id}</td> | ||
| 77 | + <td class="meta"> | ||
| 78 | + {[entry.email, entry.phone].filter(Boolean).join(" · ") || "—"} | ||
| 79 | + </td> | ||
| 80 | + <td class="meta">{entry.birth_date ?? "—"}</td> | ||
| 81 | + <td> | ||
| 82 | + <Show | ||
| 83 | + when={entry.id !== user()?.id} | ||
| 84 | + fallback={<em>{entry.role} (you)</em>} | ||
| 85 | + > | ||
| 86 | + <select | ||
| 87 | + value={entry.role} | ||
| 88 | + disabled={setRole.pending()} | ||
| 89 | + onChange={(e) => | ||
| 90 | + void setRole.run(entry, e.currentTarget.value as Role) | ||
| 91 | + } | ||
| 92 | + > | ||
| 93 | + <For each={ROLES}> | ||
| 94 | + {(role) => ( | ||
| 95 | + <option value={role} selected={role === entry.role}> | ||
| 96 | + {role} | ||
| 97 | + </option> | ||
| 98 | + )} | ||
| 99 | + </For> | ||
| 100 | + </select> | ||
| 101 | + </Show> | ||
| 102 | + </td> | ||
| 103 | + <td> | ||
| 104 | + <button class="ghost" onClick={() => toggleEdit(entry.id)}> | ||
| 105 | + {editing() === entry.id ? "Close" : "Edit"} | ||
| 106 | + </button> | ||
| 107 | + </td> | ||
| 108 | + </tr> | ||
| 109 | + <Show when={editing() === entry.id}> | ||
| 110 | + <tr> | ||
| 111 | + <td colspan={6}> | ||
| 112 | + <form | ||
| 113 | + class="stack" | ||
| 114 | + onSubmit={(e) => { | ||
| 115 | + e.preventDefault(); | ||
| 116 | + void saveProfile.run(entry, e.currentTarget); | ||
| 117 | + }} | ||
| 118 | + > | ||
| 119 | + <p class="meta">Leave a field empty to clear it.</p> | ||
| 120 | + <ProfileFields user={entry} /> | ||
| 121 | + <ErrorLine error={saveProfile.error()} /> | ||
| 122 | + <span class="row-actions"> | ||
| 123 | + <button type="submit" disabled={saveProfile.pending()}> | ||
| 124 | + Save profile | ||
| 125 | + </button> | ||
| 126 | + </span> | ||
| 127 | + </form> | ||
| 128 | + </td> | ||
| 129 | + </tr> | ||
| 130 | + </Show> | ||
| 131 | + </> | ||
| 132 | + )} | ||
| 133 | + </For> | ||
| 134 | + </tbody> | ||
| 135 | + </table> | ||
| 136 | + </div> | ||
| 137 | + </Show> | ||
| 138 | + </Show> | ||
| 139 | + </section> | ||
| 140 | + </Show> | ||
| 141 | + ); | ||
| 142 | +} | ||