diff --git a/src/pages/EventDetail.tsx b/src/pages/EventDetail.tsx new file mode 100644 index 0000000..80970d8 --- /dev/null +++ b/src/pages/EventDetail.tsx @@ -0,0 +1,236 @@ +// One event: view-first, with an edit toggle for its creator (or manager+), +// plus the attendance roster. Anyone marks themselves; teacher+ marks and +// removes others. + +import { useNavigate, useParams } from "@solidjs/router"; +import { For, Show, createResource, createSignal } from "solid-js"; +import { Empty, ErrorLine, Loading } from "../components/Feedback"; +import { IconEdit } from "../components/Icons"; +import { createAction } from "../lib/action"; +import { ApiError, events } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import { formatWindow, fromInputValue, toInputValue } from "../lib/format"; +import { ATTENDANCE_STATUSES, LIMITS, type AttendanceStatus } from "../lib/types"; + +export default function EventDetail() { + const params = useParams<{ id: string }>(); + const navigate = useNavigate(); + const { user, can } = useAuth(); + const [editing, setEditing] = createSignal(false); + + const [event, { mutate: setEvent }] = createResource(() => params.id, events.get); + const [roster, { mutate: setRoster }] = createResource(() => params.id, events.attendance); + + const canManage = () => { + const current = event(); + return current !== undefined && (current.creator === user()?.id || can("manager")); + }; + const myStatus = () => roster()?.find((a) => a.user === user()?.id)?.status; + + const save = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + setEvent( + await events.update(params.id, { + title: String(data.get("title")), + description: String(data.get("description")), + starts_at: fromInputValue(String(data.get("starts_at"))), + ends_at: fromInputValue(String(data.get("ends_at"))), + }), + ); + setEditing(false); + }); + + const remove = createAction(async () => { + await events.remove(params.id); + navigate("/events", { replace: true }); + }); + + const mark = createAction(async (status: AttendanceStatus, userId?: string) => { + const marked = await events.mark(params.id, status, userId); + // The backend upserts one row per (event, user); mirror that. + setRoster((current) => [ + ...(current ?? []).filter((a) => a.user !== marked.user), + marked, + ]); + }); + + const unmark = createAction(async (userId: string) => { + await events.unmark(params.id, userId); + setRoster((current) => current?.filter((a) => a.user !== userId)); + }); + + return ( + : } + > + {(current) => ( +
+
+
+

{current().title}

+

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

+
+ + + +
+ + +

{current().description}

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

Attendance

+
+ + {(status) => ( + + )} + +
+ + +
{ + e.preventDefault(); + const data = new FormData(e.currentTarget); + void mark.run( + data.get("status") as AttendanceStatus, + String(data.get("user_id")), + ); + }} + > + + + +
+
+ + + }> + Nobody marked yet.}> +
+ + + + + + + + + + + {(entry) => ( + + + + + + + + )} + + +
UserStatus + +
+ {entry.user === user()?.id ? "you" : entry.user} + + {entry.status} + + +
+
+
+
+
+
+ )} +
+ ); +} + +function NotFoundMessage(props: { error: unknown }) { + const message = + props.error instanceof ApiError && props.error.status === 404 + ? "This event does not exist." + : "Failed to load the event."; + return {message}; +} diff --git a/src/pages/Events.tsx b/src/pages/Events.tsx new file mode 100644 index 0000000..c621e9f --- /dev/null +++ b/src/pages/Events.tsx @@ -0,0 +1,110 @@ +// Event list plus creation (teacher+). Cards link to the detail page where +// editing and attendance live. + +import { A } from "@solidjs/router"; +import { For, Show, createResource, createSignal } from "solid-js"; +import { Empty, ErrorLine, Loading } from "../components/Feedback"; +import { IconPlus } from "../components/Icons"; +import { createAction } from "../lib/action"; +import { events } from "../lib/api"; +import { useAuth } from "../lib/auth"; +import { formatWindow, fromInputValue } from "../lib/format"; +import { LIMITS } from "../lib/types"; + +export default function Events() { + const { can } = useAuth(); + const [list, { mutate }] = createResource(events.list); + const [creating, setCreating] = createSignal(false); + + const create = createAction(async (form: HTMLFormElement) => { + const data = new FormData(form); + const starts = fromInputValue(String(data.get("starts_at"))); + const ends = fromInputValue(String(data.get("ends_at"))); + const created = await events.create({ + title: String(data.get("title")), + description: String(data.get("description")), + starts_at: starts ?? undefined, + ends_at: ends ?? undefined, + }); + mutate((current) => [created, ...(current ?? [])]); + form.reset(); + setCreating(false); + }); + + return ( +
+
+
+

Events

+

What's happening — open one to see or mark attendance.

+
+ + + +
+ + +
{ + e.preventDefault(); + void create.run(e.currentTarget); + }} + > + queueMicrotask(() => el.focus())} + /> + + + + + + + +
+ + ); +}