✨ add events and notes pages
Browse files at this commit5efbaa37755d20cc834e6b916d0d18259b2ff507Parent: dda2da6- feat
- ✨ add events and notes pages
3 changed files +517 -0
- src/pages/EventDetail.tsx +236 -0
- src/pages/Events.tsx +110 -0
- src/pages/Notes.tsx +171 -0
src/pages/EventDetail.tsxadded+236 -0
| @@ -0,0 +1,236 @@ | |||
| 1 | +// One event: view-first, with an edit toggle for its creator (or manager+), | ||
| 2 | +// plus the attendance roster. Anyone marks themselves; teacher+ marks and | ||
| 3 | +// removes others. | ||
| 4 | + | ||
| 5 | +import { useNavigate, useParams } from "@solidjs/router"; | ||
| 6 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 7 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 8 | +import { IconEdit } from "../components/Icons"; | ||
| 9 | +import { createAction } from "../lib/action"; | ||
| 10 | +import { ApiError, events } from "../lib/api"; | ||
| 11 | +import { useAuth } from "../lib/auth"; | ||
| 12 | +import { formatWindow, fromInputValue, toInputValue } from "../lib/format"; | ||
| 13 | +import { ATTENDANCE_STATUSES, LIMITS, type AttendanceStatus } from "../lib/types"; | ||
| 14 | + | ||
| 15 | +export default function EventDetail() { | ||
| 16 | + const params = useParams<{ id: string }>(); | ||
| 17 | + const navigate = useNavigate(); | ||
| 18 | + const { user, can } = useAuth(); | ||
| 19 | + const [editing, setEditing] = createSignal(false); | ||
| 20 | + | ||
| 21 | + const [event, { mutate: setEvent }] = createResource(() => params.id, events.get); | ||
| 22 | + const [roster, { mutate: setRoster }] = createResource(() => params.id, events.attendance); | ||
| 23 | + | ||
| 24 | + const canManage = () => { | ||
| 25 | + const current = event(); | ||
| 26 | + return current !== undefined && (current.creator === user()?.id || can("manager")); | ||
| 27 | + }; | ||
| 28 | + const myStatus = () => roster()?.find((a) => a.user === user()?.id)?.status; | ||
| 29 | + | ||
| 30 | + const save = createAction(async (form: HTMLFormElement) => { | ||
| 31 | + const data = new FormData(form); | ||
| 32 | + setEvent( | ||
| 33 | + await events.update(params.id, { | ||
| 34 | + title: String(data.get("title")), | ||
| 35 | + description: String(data.get("description")), | ||
| 36 | + starts_at: fromInputValue(String(data.get("starts_at"))), | ||
| 37 | + ends_at: fromInputValue(String(data.get("ends_at"))), | ||
| 38 | + }), | ||
| 39 | + ); | ||
| 40 | + setEditing(false); | ||
| 41 | + }); | ||
| 42 | + | ||
| 43 | + const remove = createAction(async () => { | ||
| 44 | + await events.remove(params.id); | ||
| 45 | + navigate("/events", { replace: true }); | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + const mark = createAction(async (status: AttendanceStatus, userId?: string) => { | ||
| 49 | + const marked = await events.mark(params.id, status, userId); | ||
| 50 | + // The backend upserts one row per (event, user); mirror that. | ||
| 51 | + setRoster((current) => [ | ||
| 52 | + ...(current ?? []).filter((a) => a.user !== marked.user), | ||
| 53 | + marked, | ||
| 54 | + ]); | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + const unmark = createAction(async (userId: string) => { | ||
| 58 | + await events.unmark(params.id, userId); | ||
| 59 | + setRoster((current) => current?.filter((a) => a.user !== userId)); | ||
| 60 | + }); | ||
| 61 | + | ||
| 62 | + return ( | ||
| 63 | + <Show | ||
| 64 | + when={event()} | ||
| 65 | + fallback={event.error ? <NotFoundMessage error={event.error} /> : <Loading />} | ||
| 66 | + > | ||
| 67 | + {(current) => ( | ||
| 68 | + <section class="page"> | ||
| 69 | + <header class="page-head"> | ||
| 70 | + <div> | ||
| 71 | + <h1>{current().title}</h1> | ||
| 72 | + <p class="sub">{formatWindow(current().starts_at, current().ends_at)}</p> | ||
| 73 | + </div> | ||
| 74 | + <Show when={canManage()}> | ||
| 75 | + <button class="ghost" onClick={() => setEditing((open) => !open)}> | ||
| 76 | + <IconEdit /> {editing() ? "Close" : "Edit"} | ||
| 77 | + </button> | ||
| 78 | + </Show> | ||
| 79 | + </header> | ||
| 80 | + | ||
| 81 | + <Show when={!editing() && current().description}> | ||
| 82 | + <p class="prewrap">{current().description}</p> | ||
| 83 | + </Show> | ||
| 84 | + | ||
| 85 | + <Show when={editing()}> | ||
| 86 | + <form | ||
| 87 | + class="card stack" | ||
| 88 | + onSubmit={(e) => { | ||
| 89 | + e.preventDefault(); | ||
| 90 | + void save.run(e.currentTarget); | ||
| 91 | + }} | ||
| 92 | + > | ||
| 93 | + <input | ||
| 94 | + name="title" | ||
| 95 | + value={current().title} | ||
| 96 | + required | ||
| 97 | + maxLength={LIMITS.eventTitle} | ||
| 98 | + /> | ||
| 99 | + <textarea name="description" rows={3} maxLength={LIMITS.eventDescription}> | ||
| 100 | + {current().description} | ||
| 101 | + </textarea> | ||
| 102 | + <div class="row"> | ||
| 103 | + <label> | ||
| 104 | + Starts | ||
| 105 | + <input | ||
| 106 | + name="starts_at" | ||
| 107 | + type="datetime-local" | ||
| 108 | + value={toInputValue(current().starts_at)} | ||
| 109 | + /> | ||
| 110 | + </label> | ||
| 111 | + <label> | ||
| 112 | + Ends | ||
| 113 | + <input | ||
| 114 | + name="ends_at" | ||
| 115 | + type="datetime-local" | ||
| 116 | + value={toInputValue(current().ends_at)} | ||
| 117 | + /> | ||
| 118 | + </label> | ||
| 119 | + </div> | ||
| 120 | + <ErrorLine error={save.error() ?? remove.error()} /> | ||
| 121 | + <span class="row-actions"> | ||
| 122 | + <button type="submit" disabled={save.pending()}> | ||
| 123 | + Save | ||
| 124 | + </button> | ||
| 125 | + <button type="button" class="ghost" onClick={() => setEditing(false)}> | ||
| 126 | + Cancel | ||
| 127 | + </button> | ||
| 128 | + <button | ||
| 129 | + type="button" | ||
| 130 | + class="ghost danger push" | ||
| 131 | + disabled={remove.pending()} | ||
| 132 | + onClick={() => void remove.run()} | ||
| 133 | + > | ||
| 134 | + Delete event | ||
| 135 | + </button> | ||
| 136 | + </span> | ||
| 137 | + </form> | ||
| 138 | + </Show> | ||
| 139 | + | ||
| 140 | + <article class="stack gap-top"> | ||
| 141 | + <h2>Attendance</h2> | ||
| 142 | + <div class="row-actions"> | ||
| 143 | + <For each={ATTENDANCE_STATUSES}> | ||
| 144 | + {(status) => ( | ||
| 145 | + <button | ||
| 146 | + class={myStatus() === status ? "" : "ghost"} | ||
| 147 | + disabled={mark.pending()} | ||
| 148 | + onClick={() => void mark.run(status)} | ||
| 149 | + > | ||
| 150 | + I'm {status} | ||
| 151 | + </button> | ||
| 152 | + )} | ||
| 153 | + </For> | ||
| 154 | + </div> | ||
| 155 | + | ||
| 156 | + <Show when={can("teacher")}> | ||
| 157 | + <form | ||
| 158 | + class="row" | ||
| 159 | + onSubmit={(e) => { | ||
| 160 | + e.preventDefault(); | ||
| 161 | + const data = new FormData(e.currentTarget); | ||
| 162 | + void mark.run( | ||
| 163 | + data.get("status") as AttendanceStatus, | ||
| 164 | + String(data.get("user_id")), | ||
| 165 | + ); | ||
| 166 | + }} | ||
| 167 | + > | ||
| 168 | + <input name="user_id" placeholder="User id" required /> | ||
| 169 | + <select name="status"> | ||
| 170 | + <For each={ATTENDANCE_STATUSES}> | ||
| 171 | + {(status) => <option value={status}>{status}</option>} | ||
| 172 | + </For> | ||
| 173 | + </select> | ||
| 174 | + <button type="submit" disabled={mark.pending()}> | ||
| 175 | + Mark user | ||
| 176 | + </button> | ||
| 177 | + </form> | ||
| 178 | + </Show> | ||
| 179 | + <ErrorLine error={mark.error() ?? unmark.error()} /> | ||
| 180 | + | ||
| 181 | + <Show when={!roster.loading} fallback={<Loading />}> | ||
| 182 | + <Show when={roster()?.length} fallback={<Empty>Nobody marked yet.</Empty>}> | ||
| 183 | + <div class="table-wrap"> | ||
| 184 | + <table> | ||
| 185 | + <thead> | ||
| 186 | + <tr> | ||
| 187 | + <th>User</th> | ||
| 188 | + <th>Status</th> | ||
| 189 | + <Show when={can("teacher")}> | ||
| 190 | + <th /> | ||
| 191 | + </Show> | ||
| 192 | + </tr> | ||
| 193 | + </thead> | ||
| 194 | + <tbody> | ||
| 195 | + <For each={roster()}> | ||
| 196 | + {(entry) => ( | ||
| 197 | + <tr> | ||
| 198 | + <td class="mono"> | ||
| 199 | + {entry.user === user()?.id ? "you" : entry.user} | ||
| 200 | + </td> | ||
| 201 | + <td> | ||
| 202 | + <span class={`badge badge-${entry.status}`}>{entry.status}</span> | ||
| 203 | + </td> | ||
| 204 | + <Show when={can("teacher")}> | ||
| 205 | + <td> | ||
| 206 | + <button | ||
| 207 | + class="ghost danger" | ||
| 208 | + disabled={unmark.pending()} | ||
| 209 | + onClick={() => void unmark.run(entry.user)} | ||
| 210 | + > | ||
| 211 | + Remove | ||
| 212 | + </button> | ||
| 213 | + </td> | ||
| 214 | + </Show> | ||
| 215 | + </tr> | ||
| 216 | + )} | ||
| 217 | + </For> | ||
| 218 | + </tbody> | ||
| 219 | + </table> | ||
| 220 | + </div> | ||
| 221 | + </Show> | ||
| 222 | + </Show> | ||
| 223 | + </article> | ||
| 224 | + </section> | ||
| 225 | + )} | ||
| 226 | + </Show> | ||
| 227 | + ); | ||
| 228 | +} | ||
| 229 | + | ||
| 230 | +function NotFoundMessage(props: { error: unknown }) { | ||
| 231 | + const message = | ||
| 232 | + props.error instanceof ApiError && props.error.status === 404 | ||
| 233 | + ? "This event does not exist." | ||
| 234 | + : "Failed to load the event."; | ||
| 235 | + return <Empty>{message}</Empty>; | ||
| 236 | +} | ||
src/pages/Events.tsxadded+110 -0
| @@ -0,0 +1,110 @@ | |||
| 1 | +// Event list plus creation (teacher+). Cards link to the detail page where | ||
| 2 | +// editing and attendance live. | ||
| 3 | + | ||
| 4 | +import { A } from "@solidjs/router"; | ||
| 5 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 6 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 7 | +import { IconPlus } from "../components/Icons"; | ||
| 8 | +import { createAction } from "../lib/action"; | ||
| 9 | +import { events } from "../lib/api"; | ||
| 10 | +import { useAuth } from "../lib/auth"; | ||
| 11 | +import { formatWindow, fromInputValue } from "../lib/format"; | ||
| 12 | +import { LIMITS } from "../lib/types"; | ||
| 13 | + | ||
| 14 | +export default function Events() { | ||
| 15 | + const { can } = useAuth(); | ||
| 16 | + const [list, { mutate }] = createResource(events.list); | ||
| 17 | + const [creating, setCreating] = createSignal(false); | ||
| 18 | + | ||
| 19 | + const create = createAction(async (form: HTMLFormElement) => { | ||
| 20 | + const data = new FormData(form); | ||
| 21 | + const starts = fromInputValue(String(data.get("starts_at"))); | ||
| 22 | + const ends = fromInputValue(String(data.get("ends_at"))); | ||
| 23 | + const created = await events.create({ | ||
| 24 | + title: String(data.get("title")), | ||
| 25 | + description: String(data.get("description")), | ||
| 26 | + starts_at: starts ?? undefined, | ||
| 27 | + ends_at: ends ?? undefined, | ||
| 28 | + }); | ||
| 29 | + mutate((current) => [created, ...(current ?? [])]); | ||
| 30 | + form.reset(); | ||
| 31 | + setCreating(false); | ||
| 32 | + }); | ||
| 33 | + | ||
| 34 | + return ( | ||
| 35 | + <section class="page"> | ||
| 36 | + <header class="page-head"> | ||
| 37 | + <div> | ||
| 38 | + <h1>Events</h1> | ||
| 39 | + <p class="sub">What's happening — open one to see or mark attendance.</p> | ||
| 40 | + </div> | ||
| 41 | + <Show when={can("teacher")}> | ||
| 42 | + <button onClick={() => setCreating((open) => !open)}> | ||
| 43 | + <IconPlus /> New event | ||
| 44 | + </button> | ||
| 45 | + </Show> | ||
| 46 | + </header> | ||
| 47 | + | ||
| 48 | + <Show when={creating()}> | ||
| 49 | + <form | ||
| 50 | + class="card stack" | ||
| 51 | + onSubmit={(e) => { | ||
| 52 | + e.preventDefault(); | ||
| 53 | + void create.run(e.currentTarget); | ||
| 54 | + }} | ||
| 55 | + > | ||
| 56 | + <input | ||
| 57 | + name="title" | ||
| 58 | + placeholder="Title" | ||
| 59 | + required | ||
| 60 | + maxLength={LIMITS.eventTitle} | ||
| 61 | + ref={(el) => queueMicrotask(() => el.focus())} | ||
| 62 | + /> | ||
| 63 | + <textarea | ||
| 64 | + name="description" | ||
| 65 | + placeholder="Description" | ||
| 66 | + rows={2} | ||
| 67 | + maxLength={LIMITS.eventDescription} | ||
| 68 | + /> | ||
| 69 | + <div class="row"> | ||
| 70 | + <label> | ||
| 71 | + Starts | ||
| 72 | + <input name="starts_at" type="datetime-local" /> | ||
| 73 | + </label> | ||
| 74 | + <label> | ||
| 75 | + Ends | ||
| 76 | + <input name="ends_at" type="datetime-local" /> | ||
| 77 | + </label> | ||
| 78 | + </div> | ||
| 79 | + <ErrorLine error={create.error()} /> | ||
| 80 | + <span class="row-actions"> | ||
| 81 | + <button type="submit" disabled={create.pending()}> | ||
| 82 | + Add event | ||
| 83 | + </button> | ||
| 84 | + <button type="button" class="ghost" onClick={() => setCreating(false)}> | ||
| 85 | + Cancel | ||
| 86 | + </button> | ||
| 87 | + </span> | ||
| 88 | + </form> | ||
| 89 | + </Show> | ||
| 90 | + | ||
| 91 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 92 | + <Show when={list()?.length} fallback={<Empty>No events yet.</Empty>}> | ||
| 93 | + <div class="grid"> | ||
| 94 | + <For each={list()}> | ||
| 95 | + {(event) => ( | ||
| 96 | + <A href={`/events/${event.id}`} class="card link-card stack"> | ||
| 97 | + <h3>{event.title}</h3> | ||
| 98 | + <Show when={event.description}> | ||
| 99 | + <p class="muted clamp">{event.description}</p> | ||
| 100 | + </Show> | ||
| 101 | + <p class="meta">{formatWindow(event.starts_at, event.ends_at)}</p> | ||
| 102 | + </A> | ||
| 103 | + )} | ||
| 104 | + </For> | ||
| 105 | + </div> | ||
| 106 | + </Show> | ||
| 107 | + </Show> | ||
| 108 | + </section> | ||
| 109 | + ); | ||
| 110 | +} | ||
src/pages/Notes.tsxadded+171 -0
| @@ -0,0 +1,171 @@ | |||
| 1 | +// Personal notes: list, create, edit in place, delete. Every mutation updates | ||
| 2 | +// the resource from the server's response — the UI never waits on a refetch. | ||
| 3 | + | ||
| 4 | +import { For, Show, createResource, createSignal } from "solid-js"; | ||
| 5 | +import { Empty, ErrorLine, Loading } from "../components/Feedback"; | ||
| 6 | +import { IconEdit, IconPlus, IconTrash } from "../components/Icons"; | ||
| 7 | +import { createAction } from "../lib/action"; | ||
| 8 | +import { notes } from "../lib/api"; | ||
| 9 | +import { LIMITS, type Note } from "../lib/types"; | ||
| 10 | + | ||
| 11 | +export default function Notes() { | ||
| 12 | + const [list, { mutate }] = createResource(notes.list); | ||
| 13 | + const [creating, setCreating] = createSignal(false); | ||
| 14 | + | ||
| 15 | + const create = createAction(async (form: HTMLFormElement) => { | ||
| 16 | + const data = new FormData(form); | ||
| 17 | + const created = await notes.create({ | ||
| 18 | + title: String(data.get("title")), | ||
| 19 | + content: String(data.get("content")), | ||
| 20 | + }); | ||
| 21 | + mutate((current) => [created, ...(current ?? [])]); | ||
| 22 | + form.reset(); | ||
| 23 | + setCreating(false); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + const replace = (updated: Note) => | ||
| 27 | + mutate((current) => current?.map((n) => (n.id === updated.id ? updated : n))); | ||
| 28 | + const drop = (id: string) => mutate((current) => current?.filter((n) => n.id !== id)); | ||
| 29 | + | ||
| 30 | + return ( | ||
| 31 | + <section class="page"> | ||
| 32 | + <header class="page-head"> | ||
| 33 | + <div> | ||
| 34 | + <h1>Notes</h1> | ||
| 35 | + <p class="sub">Your personal scratchpad — visible only to you.</p> | ||
| 36 | + </div> | ||
| 37 | + <button onClick={() => setCreating((open) => !open)}> | ||
| 38 | + <IconPlus /> New note | ||
| 39 | + </button> | ||
| 40 | + </header> | ||
| 41 | + | ||
| 42 | + <Show when={creating()}> | ||
| 43 | + <form | ||
| 44 | + class="card stack" | ||
| 45 | + onSubmit={(e) => { | ||
| 46 | + e.preventDefault(); | ||
| 47 | + void create.run(e.currentTarget); | ||
| 48 | + }} | ||
| 49 | + > | ||
| 50 | + <input | ||
| 51 | + name="title" | ||
| 52 | + placeholder="Title" | ||
| 53 | + required | ||
| 54 | + maxLength={LIMITS.noteTitle} | ||
| 55 | + ref={(el) => queueMicrotask(() => el.focus())} | ||
| 56 | + /> | ||
| 57 | + <textarea | ||
| 58 | + name="content" | ||
| 59 | + placeholder="Write something…" | ||
| 60 | + rows={3} | ||
| 61 | + maxLength={LIMITS.noteContent} | ||
| 62 | + /> | ||
| 63 | + <ErrorLine error={create.error()} /> | ||
| 64 | + <span class="row-actions"> | ||
| 65 | + <button type="submit" disabled={create.pending()}> | ||
| 66 | + Add note | ||
| 67 | + </button> | ||
| 68 | + <button type="button" class="ghost" onClick={() => setCreating(false)}> | ||
| 69 | + Cancel | ||
| 70 | + </button> | ||
| 71 | + </span> | ||
| 72 | + </form> | ||
| 73 | + </Show> | ||
| 74 | + | ||
| 75 | + <Show when={!list.loading} fallback={<Loading />}> | ||
| 76 | + <Show | ||
| 77 | + when={list()?.length} | ||
| 78 | + fallback={<Empty>No notes yet — write your first one.</Empty>} | ||
| 79 | + > | ||
| 80 | + <div class="grid"> | ||
| 81 | + <For each={list()}> | ||
| 82 | + {(note) => <NoteCard note={note} onSaved={replace} onDeleted={drop} />} | ||
| 83 | + </For> | ||
| 84 | + </div> | ||
| 85 | + </Show> | ||
| 86 | + </Show> | ||
| 87 | + </section> | ||
| 88 | + ); | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +function NoteCard(props: { | ||
| 92 | + note: Note; | ||
| 93 | + onSaved: (note: Note) => void; | ||
| 94 | + onDeleted: (id: string) => void; | ||
| 95 | +}) { | ||
| 96 | + const [editing, setEditing] = createSignal(false); | ||
| 97 | + | ||
| 98 | + const save = createAction(async (form: HTMLFormElement) => { | ||
| 99 | + const data = new FormData(form); | ||
| 100 | + props.onSaved( | ||
| 101 | + await notes.update(props.note.id, { | ||
| 102 | + title: String(data.get("title")), | ||
| 103 | + content: String(data.get("content")), | ||
| 104 | + }), | ||
| 105 | + ); | ||
| 106 | + setEditing(false); | ||
| 107 | + }); | ||
| 108 | + | ||
| 109 | + const remove = createAction(async () => { | ||
| 110 | + await notes.remove(props.note.id); | ||
| 111 | + props.onDeleted(props.note.id); | ||
| 112 | + }); | ||
| 113 | + | ||
| 114 | + return ( | ||
| 115 | + <article class="card stack"> | ||
| 116 | + <Show | ||
| 117 | + when={editing()} | ||
| 118 | + fallback={ | ||
| 119 | + <> | ||
| 120 | + <header class="row"> | ||
| 121 | + <h3>{props.note.title}</h3> | ||
| 122 | + <span class="row-actions"> | ||
| 123 | + <button | ||
| 124 | + class="ghost icon-btn" | ||
| 125 | + title="Edit" | ||
| 126 | + onClick={() => setEditing(true)} | ||
| 127 | + > | ||
| 128 | + <IconEdit /> | ||
| 129 | + </button> | ||
| 130 | + <button | ||
| 131 | + class="ghost icon-btn danger" | ||
| 132 | + title="Delete" | ||
| 133 | + disabled={remove.pending()} | ||
| 134 | + onClick={() => void remove.run()} | ||
| 135 | + > | ||
| 136 | + <IconTrash /> | ||
| 137 | + </button> | ||
| 138 | + </span> | ||
| 139 | + </header> | ||
| 140 | + <Show when={props.note.content}> | ||
| 141 | + <p class="prewrap">{props.note.content}</p> | ||
| 142 | + </Show> | ||
| 143 | + <ErrorLine error={remove.error()} /> | ||
| 144 | + </> | ||
| 145 | + } | ||
| 146 | + > | ||
| 147 | + <form | ||
| 148 | + class="stack" | ||
| 149 | + onSubmit={(e) => { | ||
| 150 | + e.preventDefault(); | ||
| 151 | + void save.run(e.currentTarget); | ||
| 152 | + }} | ||
| 153 | + > | ||
| 154 | + <input name="title" value={props.note.title} required maxLength={LIMITS.noteTitle} /> | ||
| 155 | + <textarea name="content" rows={4} maxLength={LIMITS.noteContent}> | ||
| 156 | + {props.note.content} | ||
| 157 | + </textarea> | ||
| 158 | + <ErrorLine error={save.error()} /> | ||
| 159 | + <span class="row-actions"> | ||
| 160 | + <button type="submit" disabled={save.pending()}> | ||
| 161 | + Save | ||
| 162 | + </button> | ||
| 163 | + <button type="button" class="ghost" onClick={() => setEditing(false)}> | ||
| 164 | + Cancel | ||
| 165 | + </button> | ||
| 166 | + </span> | ||
| 167 | + </form> | ||
| 168 | + </Show> | ||
| 169 | + </article> | ||
| 170 | + ); | ||
| 171 | +} | ||