forge
Explore
Log in

Don't you dare go Hollow

✨ add event audience selection

Browse files at this commit

d3edb453f8a373ee9fea907c6c024f061cb780faBurak BoduroğluParent: 708f89b
  • feat
    • ✨ add event audience selection
Show full message
feat: ✨ add event audience selection

- add event audience typing and create/edit API payload support

- let teachers choose school, role, course, or registration audiences

- show localized audience summaries on event cards and detail pages
src/api/patchEventById.tsmodified+2 -1
@@ -1,10 +1,11 @@
1 import { client } from "./client";1 import { client } from "./client";
2-import type { Event } from "./types";2+import type { Event, EventAudience } from "./types";
3 3
4 /** Omitted time fields keep their value; explicit null clears them. */4 /** Omitted time fields keep their value; explicit null clears them. */
5 export type PatchEventBody = {5 export type PatchEventBody = {
6 title?: string | null;6 title?: string | null;
7 description?: string | null;7 description?: string | null;
8+ audience?: EventAudience | null;
8 starts_at?: number | null;9 starts_at?: number | null;
9 ends_at?: number | null;10 ends_at?: number | null;
10 };11 };
src/api/postEvent.tsmodified+2 -1
@@ -1,9 +1,10 @@
1 import { client } from "./client";1 import { client } from "./client";
2-import type { Event } from "./types";2+import type { Event, EventAudience } from "./types";
3 3
4 export type PostEventBody = {4 export type PostEventBody = {
5 title: string;5 title: string;
6 description?: string | null;6 description?: string | null;
7+ audience?: EventAudience | null;
7 starts_at?: number | null;8 starts_at?: number | null;
8 ends_at?: number | null;9 ends_at?: number | null;
9 };10 };
src/api/types.tsmodified+7 -0
@@ -52,11 +52,18 @@ export type NoteFile = {
52 size: number;52 size: number;
53 };53 };
54 54
55+export type EventAudience =
56+ | { kind: "school" }
57+ | { kind: "role"; role: Role }
58+ | { kind: "course"; course: string }
59+ | { kind: "registration"; capacity?: number | null };
60+
55 export type Event = {61 export type Event = {
56 id: string;62 id: string;
57 creator: string;63 creator: string;
58 title: string;64 title: string;
59 description: string;65 description: string;
66+ audience: EventAudience;
60 starts_at: number | null;67 starts_at: number | null;
61 ends_at: number | null;68 ends_at: number | null;
62 };69 };
src/components/events/event-card.tsxmodified+10 -1
@@ -1,8 +1,16 @@
1 import { Link } from "@tanstack/solid-router";1 import { Link } from "@tanstack/solid-router";
2-import type { Event } from "@/api/types";2+import type { Event, EventAudience } from "@/api/types";
3+import type { MessageKey } from "@/i18n/messages";
3 import { formatDateTime } from "@/lib/format";4 import { formatDateTime } from "@/lib/format";
4 import { usePreferences, useT } from "@/stores/preferences-context";5 import { usePreferences, useT } from "@/stores/preferences-context";
5 6
7+function audienceLabel(audience: EventAudience, t: ReturnType<typeof useT>): string {
8+ if (audience.kind === "role") return `${t("events.audience.role")}: ${t(`role.${audience.role}` as MessageKey)}`;
9+ if (audience.kind === "course") return t("events.audience.course");
10+ if (audience.kind === "registration") return t("events.audience.registration");
11+ return t("events.audience.school");
12+}
13+
6 export function EventCard(props: { event: Event }) {14 export function EventCard(props: { event: Event }) {
7 const t = useT();15 const t = useT();
8 const { locale } = usePreferences();16 const { locale } = usePreferences();
@@ -16,6 +24,7 @@ export function EventCard(props: { event: Event }) {
16 <p class="mt-2 line-clamp-3 flex-1 text-[13px] leading-relaxed text-muted-foreground">24 <p class="mt-2 line-clamp-3 flex-1 text-[13px] leading-relaxed text-muted-foreground">
17 {props.event.description || "—"}25 {props.event.description || "—"}
18 </p>26 </p>
27+ <p class="mt-3 text-xs font-medium text-muted-foreground">{audienceLabel(props.event.audience, t)}</p>
19 </div>28 </div>
20 <div class="border-t border-border bg-muted/25 px-3 py-3">29 <div class="border-t border-border bg-muted/25 px-3 py-3">
21 <dl class="grid gap-2 text-xs text-muted-foreground">30 <dl class="grid gap-2 text-xs text-muted-foreground">
src/components/events/event-form.tsxmodified+70 -2
@@ -1,15 +1,21 @@
1-import { createResource, createSignal, Show } from "solid-js";1+import { createResource, createSignal, For, Show } from "solid-js";
2 import { formatApiError } from "@/api/client";2 import { formatApiError } from "@/api/client";
3+import { getCourses } from "@/api/getCourses";
3 import { getTime } from "@/api/getTime";4 import { getTime } from "@/api/getTime";
4-import type { Event } from "@/api/types";5+import type { Event, EventAudience, Role } from "@/api/types";
6+import type { MessageKey } from "@/i18n/messages";
5 import { Button } from "@/components/ui/button";7 import { Button } from "@/components/ui/button";
6 import { ConfirmDialog } from "@/components/ui/confirm-dialog";8 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
7 import { DatePicker } from "@/components/ui/date-picker";9 import { DatePicker } from "@/components/ui/date-picker";
8 import { Input } from "@/components/ui/input";10 import { Input } from "@/components/ui/input";
9 import { Label } from "@/components/ui/label";11 import { Label } from "@/components/ui/label";
12+import { Select } from "@/components/ui/select";
10 import { Textarea } from "@/components/ui/textarea";13 import { Textarea } from "@/components/ui/textarea";
11 import { useT } from "@/stores/preferences-context";14 import { useT } from "@/stores/preferences-context";
12 15
16+const AUDIENCE_KINDS = ["school", "role", "course", "registration"] as const;
17+const AUDIENCE_ROLES: Role[] = ["student", "teacher", "manager", "admin"];
18+
13 function dateInputFromMs(ms: number | null | undefined): string {19 function dateInputFromMs(ms: number | null | undefined): string {
14 if (ms == null) return "";20 if (ms == null) return "";
15 const d = new Date(ms);21 const d = new Date(ms);
@@ -44,6 +50,7 @@ function dateTimeInputToMs(date: string, time: string): number | null {
44 export type EventFormValues = {50 export type EventFormValues = {
45 title: string;51 title: string;
46 description: string;52 description: string;
53+ audience: EventAudience;
47 starts_at: number | null | undefined;54 starts_at: number | null | undefined;
48 ends_at: number | null | undefined;55 ends_at: number | null | undefined;
49 };56 };
@@ -58,6 +65,12 @@ export function EventForm(props: {
58 const isEdit = !!props.initial?.id;65 const isEdit = !!props.initial?.id;
59 const [title, setTitle] = createSignal(props.initial?.title ?? "");66 const [title, setTitle] = createSignal(props.initial?.title ?? "");
60 const [description, setDescription] = createSignal(props.initial?.description ?? "");67 const [description, setDescription] = createSignal(props.initial?.description ?? "");
68+ const initialAudience = () => props.initial?.audience ?? { kind: "school" as const };
69+ const audience = initialAudience();
70+ const [audienceKind, setAudienceKind] = createSignal<EventAudience["kind"]>(audience.kind);
71+ const [audienceRole, setAudienceRole] = createSignal<Role>(audience.kind === "role" ? audience.role : "student");
72+ const [audienceCourse, setAudienceCourse] = createSignal(audience.kind === "course" ? audience.course : "");
73+ const [capacity, setCapacity] = createSignal(audience.kind === "registration" && audience.capacity != null ? String(audience.capacity) : "");
61 const [startsDate, setStartsDate] = createSignal(dateInputFromMs(props.initial?.starts_at));74 const [startsDate, setStartsDate] = createSignal(dateInputFromMs(props.initial?.starts_at));
62 const [startsTime, setStartsTime] = createSignal(timeInputFromMs(props.initial?.starts_at));75 const [startsTime, setStartsTime] = createSignal(timeInputFromMs(props.initial?.starts_at));
63 const [endsDate, setEndsDate] = createSignal(dateInputFromMs(props.initial?.ends_at));76 const [endsDate, setEndsDate] = createSignal(dateInputFromMs(props.initial?.ends_at));
@@ -69,6 +82,7 @@ export function EventForm(props: {
69 const [confirmOpen, setConfirmOpen] = createSignal(false);82 const [confirmOpen, setConfirmOpen] = createSignal(false);
70 const [pendingValues, setPendingValues] = createSignal<EventFormValues | null>(null);83 const [pendingValues, setPendingValues] = createSignal<EventFormValues | null>(null);
71 const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() })));84 const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() })));
85+ const [courses] = createResource(async () => (await getCourses().catch(() => ({ items: [] }))).items);
72 86
73 const resolveTime = (date: string, time: string, touched: boolean): number | null | undefined => {87 const resolveTime = (date: string, time: string, touched: boolean): number | null | undefined => {
74 if (isEdit && !touched) return undefined;88 if (isEdit && !touched) return undefined;
@@ -83,6 +97,8 @@ export function EventForm(props: {
83 if (!value) return t("form.titleRequired");97 if (!value) return t("form.titleRequired");
84 if (value.length > 200) return t("form.titleMax");98 if (value.length > 200) return t("form.titleMax");
85 if (description().length > 2000) return t("form.descriptionMax");99 if (description().length > 2000) return t("form.descriptionMax");
100+ if (audienceKind() === "course" && !audienceCourse()) return t("events.audienceCourseRequired");
101+ if (audienceKind() === "registration" && capacity().trim() && Number(capacity()) < 1) return t("events.audienceCapacityInvalid");
86 const s = starts === undefined ? props.initial?.starts_at ?? null : starts;102 const s = starts === undefined ? props.initial?.starts_at ?? null : starts;
87 const e = ends === undefined ? props.initial?.ends_at ?? null : ends;103 const e = ends === undefined ? props.initial?.ends_at ?? null : ends;
88 if (startsTouched() && (startsDate().trim() || startsTime().trim()) && starts == null) return t("form.timeOrder");104 if (startsTouched() && (startsDate().trim() || startsTime().trim()) && starts == null) return t("form.timeOrder");
@@ -92,6 +108,16 @@ export function EventForm(props: {
92 return null;108 return null;
93 };109 };
94 110
111+ const audienceValue = (): EventAudience => {
112+ if (audienceKind() === "role") return { kind: "role", role: audienceRole() };
113+ if (audienceKind() === "course") return { kind: "course", course: audienceCourse() };
114+ if (audienceKind() === "registration") {
115+ const value = capacity().trim();
116+ return { kind: "registration", capacity: value ? Number(value) : null };
117+ }
118+ return { kind: "school" };
119+ };
120+
95 const save = async (values: EventFormValues) => {121 const save = async (values: EventFormValues) => {
96 setError("");122 setError("");
97 setPending(true);123 setPending(true);
@@ -100,6 +126,10 @@ export function EventForm(props: {
100 if (!isEdit) {126 if (!isEdit) {
101 setTitle("");127 setTitle("");
102 setDescription("");128 setDescription("");
129+ setAudienceKind("school");
130+ setAudienceRole("student");
131+ setAudienceCourse("");
132+ setCapacity("");
103 setStartsDate("");133 setStartsDate("");
104 setStartsTime("");134 setStartsTime("");
105 setEndsDate("");135 setEndsDate("");
@@ -126,6 +156,7 @@ export function EventForm(props: {
126 const values = {156 const values = {
127 title: title().trim(),157 title: title().trim(),
128 description: description(),158 description: description(),
159+ audience: audienceValue(),
129 starts_at,160 starts_at,
130 ends_at,161 ends_at,
131 };162 };
@@ -162,6 +193,43 @@ export function EventForm(props: {
162 onInput={(e) => setDescription(e.currentTarget.value)}193 onInput={(e) => setDescription(e.currentTarget.value)}
163 />194 />
164 </div>195 </div>
196+ <div class="space-y-1.5">
197+ <Label for="event-audience">{t("events.audience")}</Label>
198+ <Select id="event-audience" value={audienceKind()} onChange={(e) => setAudienceKind(e.currentTarget.value as EventAudience["kind"])}>
199+ <For each={AUDIENCE_KINDS}>{(kind) => <option value={kind}>{t(`events.audience.${kind}` as MessageKey)}</option>}</For>
200+ </Select>
201+ </div>
202+ <Show when={audienceKind() === "role"}>
203+ <div class="space-y-1.5">
204+ <Label for="event-audience-role">{t("admin.role")}</Label>
205+ <Select id="event-audience-role" value={audienceRole()} onChange={(e) => setAudienceRole(e.currentTarget.value as Role)}>
206+ <For each={AUDIENCE_ROLES}>{(role) => <option value={role}>{t(`role.${role}` as MessageKey)}</option>}</For>
207+ </Select>
208+ </div>
209+ </Show>
210+ <Show when={audienceKind() === "course"}>
211+ <div class="space-y-1.5">
212+ <Label for="event-audience-course">{t("nav.courses")}</Label>
213+ <Select id="event-audience-course" value={audienceCourse()} onChange={(e) => setAudienceCourse(e.currentTarget.value)}>
214+ <option value="">{t("events.selectCourse")}</option>
215+ <For each={courses() ?? []}>{(course) => <option value={course.id}>{course.title}</option>}</For>
216+ </Select>
217+ </div>
218+ </Show>
219+ <Show when={audienceKind() === "registration"}>
220+ <div class="space-y-1.5">
221+ <Label for="event-audience-capacity">{t("events.capacity")}</Label>
222+ <Input
223+ id="event-audience-capacity"
224+ type="number"
225+ min="1"
226+ inputMode="numeric"
227+ placeholder={t("events.capacityOptional")}
228+ value={capacity()}
229+ onInput={(e) => setCapacity(e.currentTarget.value)}
230+ />
231+ </div>
232+ </Show>
165 <div class="grid gap-3">233 <div class="grid gap-3">
166 <div class="space-y-1.5">234 <div class="space-y-1.5">
167 <Label for="event-starts">{t("events.starts")}</Label>235 <Label for="event-starts">{t("events.starts")}</Label>
src/i18n/messages.tsmodified+30 -0
@@ -211,6 +211,16 @@ export type MessageKey =
211 | "events.noAttendees"211 | "events.noAttendees"
212 | "events.userId"212 | "events.userId"
213 | "events.status"213 | "events.status"
214+ | "events.audience"
215+ | "events.audience.school"
216+ | "events.audience.role"
217+ | "events.audience.course"
218+ | "events.audience.registration"
219+ | "events.selectCourse"
220+ | "events.capacity"
221+ | "events.capacityOptional"
222+ | "events.audienceCourseRequired"
223+ | "events.audienceCapacityInvalid"
214 | "events.helpTitle"224 | "events.helpTitle"
215 | "events.helpBody"225 | "events.helpBody"
216 | "events.clearStart"226 | "events.clearStart"
@@ -742,6 +752,16 @@ const en: Dict = {
742 "events.noAttendees": "No users found",752 "events.noAttendees": "No users found",
743 "events.userId": "User id",753 "events.userId": "User id",
744 "events.status": "Status",754 "events.status": "Status",
755+ "events.audience": "Audience",
756+ "events.audience.school": "Whole school",
757+ "events.audience.role": "Role",
758+ "events.audience.course": "Course roster",
759+ "events.audience.registration": "Registration list",
760+ "events.selectCourse": "Select course",
761+ "events.capacity": "Capacity",
762+ "events.capacityOptional": "Optional seat cap",
763+ "events.audienceCourseRequired": "Select a course for this audience.",
764+ "events.audienceCapacityInvalid": "Capacity must be at least 1.",
745 "events.helpTitle": "About events",765 "events.helpTitle": "About events",
746 "events.helpBody":766 "events.helpBody":
747 "Students mark themselves present / absent / late / excused with the big status buttons, then save attendance. Teachers create events and can mark others by user id. Managers can edit any event.",767 "Students mark themselves present / absent / late / excused with the big status buttons, then save attendance. Teachers create events and can mark others by user id. Managers can edit any event.",
@@ -1284,6 +1304,16 @@ const tr: Dict = {
1284 "events.noAttendees": "Kullanıcı bulunamadı",1304 "events.noAttendees": "Kullanıcı bulunamadı",
1285 "events.userId": "Kullanıcı id",1305 "events.userId": "Kullanıcı id",
1286 "events.status": "Durum",1306 "events.status": "Durum",
1307+ "events.audience": "Hedef kitle",
1308+ "events.audience.school": "Tüm okul",
1309+ "events.audience.role": "Rol",
1310+ "events.audience.course": "Ders listesi",
1311+ "events.audience.registration": "Kayıt listesi",
1312+ "events.selectCourse": "Ders seç",
1313+ "events.capacity": "Kapasite",
1314+ "events.capacityOptional": "İsteğe bağlı kontenjan",
1315+ "events.audienceCourseRequired": "Bu hedef kitle için ders seç.",
1316+ "events.audienceCapacityInvalid": "Kapasite en az 1 olmalı.",
1287 "events.helpTitle": "Etkinlikler hakkında",1317 "events.helpTitle": "Etkinlikler hakkında",
1288 "events.helpBody":1318 "events.helpBody":
1289 "Öğrenciler büyük durum butonlarıyla var/yok/geç/mazeretli seçip yoklamayı kaydeder. Öğretmenler etkinlik oluşturur ve başkasını işaretleyebilir. Yöneticiler her etkinliği düzenleyebilir.",1319 "Öğrenciler büyük durum butonlarıyla var/yok/geç/mazeretli seçip yoklamayı kaydeder. Öğretmenler etkinlik oluşturur ve başkasını işaretleyebilir. Yöneticiler her etkinliği düzenleyebilir.",
src/pages/event-detail-page.tsxmodified+11 -1
@@ -7,7 +7,8 @@ import { getEventById } from "@/api/getEventById";
7 import { patchEventById } from "@/api/patchEventById";7 import { patchEventById } from "@/api/patchEventById";
8 import { postEventAttendance } from "@/api/postEventAttendance";8 import { postEventAttendance } from "@/api/postEventAttendance";
9 import { formatApiError } from "@/api/client";9 import { formatApiError } from "@/api/client";
10-import type { AttendanceStatus } from "@/api/types";10+import type { AttendanceStatus, EventAudience } from "@/api/types";
11+import type { MessageKey } from "@/i18n/messages";
11 import { AttendanceStatusPicker } from "@/components/events/attendance-status-picker";12 import { AttendanceStatusPicker } from "@/components/events/attendance-status-picker";
12 import { AttendanceTable } from "@/components/events/attendance-table";13 import { AttendanceTable } from "@/components/events/attendance-table";
13 import { EventForm } from "@/components/events/event-form";14 import { EventForm } from "@/components/events/event-form";
@@ -26,6 +27,13 @@ import { hasMinRole } from "@/lib/roles";
26 import { useAuth } from "@/stores/auth-context";27 import { useAuth } from "@/stores/auth-context";
27 import { usePreferences, useT } from "@/stores/preferences-context";28 import { usePreferences, useT } from "@/stores/preferences-context";
28 29
30+function audienceLabel(audience: EventAudience, t: ReturnType<typeof useT>): string {
31+ if (audience.kind === "role") return `${t("events.audience.role")}: ${t(`role.${audience.role}` as MessageKey)}`;
32+ if (audience.kind === "course") return t("events.audience.course");
33+ if (audience.kind === "registration") return t("events.audience.registration");
34+ return t("events.audience.school");
35+}
36+
29 export default function EventDetailPage() {37 export default function EventDetailPage() {
30 return (38 return (
31 <RouteGuard>39 <RouteGuard>
@@ -144,6 +152,7 @@ function EventDetailContent() {
144 <p class="mt-3 max-w-2xl whitespace-pre-wrap text-sm text-muted-foreground">152 <p class="mt-3 max-w-2xl whitespace-pre-wrap text-sm text-muted-foreground">
145 {ev().description || "—"}153 {ev().description || "—"}
146 </p>154 </p>
155+ <p class="mt-2 text-xs font-medium text-muted-foreground">{audienceLabel(ev().audience, t)}</p>
147 </PageHeader>156 </PageHeader>
148 </div>157 </div>
149 158
@@ -175,6 +184,7 @@ function EventDetailContent() {
175 const body: Record<string, unknown> = {184 const body: Record<string, unknown> = {
176 title: values.title,185 title: values.title,
177 description: values.description,186 description: values.description,
187+ audience: values.audience,
178 };188 };
179 if (values.starts_at !== undefined) body.starts_at = values.starts_at;189 if (values.starts_at !== undefined) body.starts_at = values.starts_at;
180 if (values.ends_at !== undefined) body.ends_at = values.ends_at;190 if (values.ends_at !== undefined) body.ends_at = values.ends_at;
src/pages/events-page.tsxmodified+3 -1
@@ -2,7 +2,7 @@ import { For, Show, Suspense, createMemo, createResource, createSignal } from "s
2 import { getEvents } from "@/api/getEvents";2 import { getEvents } from "@/api/getEvents";
3 import { postEvent } from "@/api/postEvent";3 import { postEvent } from "@/api/postEvent";
4 import { formatApiError } from "@/api/client";4 import { formatApiError } from "@/api/client";
5-import type { Event } from "@/api/types";5+import type { Event, EventAudience } from "@/api/types";
6 import { Alert } from "@/components/ui/alert";6 import { Alert } from "@/components/ui/alert";
7 import { Button } from "@/components/ui/button";7 import { Button } from "@/components/ui/button";
8 import { EventCard } from "@/components/events/event-card";8 import { EventCard } from "@/components/events/event-card";
@@ -111,8 +111,10 @@ function EventsContent() {
111 description?: string;111 description?: string;
112 starts_at?: number;112 starts_at?: number;
113 ends_at?: number;113 ends_at?: number;
114+ audience?: EventAudience;
114 } = { title: values.title };115 } = { title: values.title };
115 if (values.description) body.description = values.description;116 if (values.description) body.description = values.description;
117+ body.audience = values.audience;
116 if (values.starts_at != null) body.starts_at = values.starts_at;118 if (values.starts_at != null) body.starts_at = values.starts_at;
117 if (values.ends_at != null) body.ends_at = values.ends_at;119 if (values.ends_at != null) body.ends_at = values.ends_at;
118 await postEvent(body);120 await postEvent(body);