diff --git a/src/api/patchEventById.ts b/src/api/patchEventById.ts index b766f33..29c7c44 100644 --- a/src/api/patchEventById.ts +++ b/src/api/patchEventById.ts @@ -1,10 +1,11 @@ import { client } from "./client"; -import type { Event } from "./types"; +import type { Event, EventAudience } from "./types"; /** Omitted time fields keep their value; explicit null clears them. */ export type PatchEventBody = { title?: string | null; description?: string | null; + audience?: EventAudience | null; starts_at?: number | null; ends_at?: number | null; }; diff --git a/src/api/postEvent.ts b/src/api/postEvent.ts index a3c4d0e..22a0ed6 100644 --- a/src/api/postEvent.ts +++ b/src/api/postEvent.ts @@ -1,9 +1,10 @@ import { client } from "./client"; -import type { Event } from "./types"; +import type { Event, EventAudience } from "./types"; export type PostEventBody = { title: string; description?: string | null; + audience?: EventAudience | null; starts_at?: number | null; ends_at?: number | null; }; diff --git a/src/api/types.ts b/src/api/types.ts index c6c5636..ffd4cd3 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -52,11 +52,18 @@ export type NoteFile = { size: number; }; +export type EventAudience = + | { kind: "school" } + | { kind: "role"; role: Role } + | { kind: "course"; course: string } + | { kind: "registration"; capacity?: number | null }; + export type Event = { id: string; creator: string; title: string; description: string; + audience: EventAudience; starts_at: number | null; ends_at: number | null; }; diff --git a/src/components/events/event-card.tsx b/src/components/events/event-card.tsx index cb52e73..cde639a 100644 --- a/src/components/events/event-card.tsx +++ b/src/components/events/event-card.tsx @@ -1,8 +1,16 @@ import { Link } from "@tanstack/solid-router"; -import type { Event } from "@/api/types"; +import type { Event, EventAudience } from "@/api/types"; +import type { MessageKey } from "@/i18n/messages"; import { formatDateTime } from "@/lib/format"; import { usePreferences, useT } from "@/stores/preferences-context"; +function audienceLabel(audience: EventAudience, t: ReturnType): string { + if (audience.kind === "role") return `${t("events.audience.role")}: ${t(`role.${audience.role}` as MessageKey)}`; + if (audience.kind === "course") return t("events.audience.course"); + if (audience.kind === "registration") return t("events.audience.registration"); + return t("events.audience.school"); +} + export function EventCard(props: { event: Event }) { const t = useT(); const { locale } = usePreferences(); @@ -16,6 +24,7 @@ export function EventCard(props: { event: Event }) {

{props.event.description || "—"}

+

{audienceLabel(props.event.audience, t)}

diff --git a/src/components/events/event-form.tsx b/src/components/events/event-form.tsx index b221254..a043000 100644 --- a/src/components/events/event-form.tsx +++ b/src/components/events/event-form.tsx @@ -1,15 +1,21 @@ -import { createResource, createSignal, Show } from "solid-js"; +import { createResource, createSignal, For, Show } from "solid-js"; import { formatApiError } from "@/api/client"; +import { getCourses } from "@/api/getCourses"; import { getTime } from "@/api/getTime"; -import type { Event } from "@/api/types"; +import type { Event, EventAudience, Role } from "@/api/types"; +import type { MessageKey } from "@/i18n/messages"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DatePicker } from "@/components/ui/date-picker"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; +import { Select } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { useT } from "@/stores/preferences-context"; +const AUDIENCE_KINDS = ["school", "role", "course", "registration"] as const; +const AUDIENCE_ROLES: Role[] = ["student", "teacher", "manager", "admin"]; + function dateInputFromMs(ms: number | null | undefined): string { if (ms == null) return ""; const d = new Date(ms); @@ -44,6 +50,7 @@ function dateTimeInputToMs(date: string, time: string): number | null { export type EventFormValues = { title: string; description: string; + audience: EventAudience; starts_at: number | null | undefined; ends_at: number | null | undefined; }; @@ -58,6 +65,12 @@ export function EventForm(props: { const isEdit = !!props.initial?.id; const [title, setTitle] = createSignal(props.initial?.title ?? ""); const [description, setDescription] = createSignal(props.initial?.description ?? ""); + const initialAudience = () => props.initial?.audience ?? { kind: "school" as const }; + const audience = initialAudience(); + const [audienceKind, setAudienceKind] = createSignal(audience.kind); + const [audienceRole, setAudienceRole] = createSignal(audience.kind === "role" ? audience.role : "student"); + const [audienceCourse, setAudienceCourse] = createSignal(audience.kind === "course" ? audience.course : ""); + const [capacity, setCapacity] = createSignal(audience.kind === "registration" && audience.capacity != null ? String(audience.capacity) : ""); const [startsDate, setStartsDate] = createSignal(dateInputFromMs(props.initial?.starts_at)); const [startsTime, setStartsTime] = createSignal(timeInputFromMs(props.initial?.starts_at)); const [endsDate, setEndsDate] = createSignal(dateInputFromMs(props.initial?.ends_at)); @@ -69,6 +82,7 @@ export function EventForm(props: { const [confirmOpen, setConfirmOpen] = createSignal(false); const [pendingValues, setPendingValues] = createSignal(null); const [serverTime] = createResource(() => getTime().catch(() => ({ now: Date.now() }))); + const [courses] = createResource(async () => (await getCourses().catch(() => ({ items: [] }))).items); const resolveTime = (date: string, time: string, touched: boolean): number | null | undefined => { if (isEdit && !touched) return undefined; @@ -83,6 +97,8 @@ export function EventForm(props: { if (!value) return t("form.titleRequired"); if (value.length > 200) return t("form.titleMax"); if (description().length > 2000) return t("form.descriptionMax"); + if (audienceKind() === "course" && !audienceCourse()) return t("events.audienceCourseRequired"); + if (audienceKind() === "registration" && capacity().trim() && Number(capacity()) < 1) return t("events.audienceCapacityInvalid"); const s = starts === undefined ? props.initial?.starts_at ?? null : starts; const e = ends === undefined ? props.initial?.ends_at ?? null : ends; if (startsTouched() && (startsDate().trim() || startsTime().trim()) && starts == null) return t("form.timeOrder"); @@ -92,6 +108,16 @@ export function EventForm(props: { return null; }; + const audienceValue = (): EventAudience => { + if (audienceKind() === "role") return { kind: "role", role: audienceRole() }; + if (audienceKind() === "course") return { kind: "course", course: audienceCourse() }; + if (audienceKind() === "registration") { + const value = capacity().trim(); + return { kind: "registration", capacity: value ? Number(value) : null }; + } + return { kind: "school" }; + }; + const save = async (values: EventFormValues) => { setError(""); setPending(true); @@ -100,6 +126,10 @@ export function EventForm(props: { if (!isEdit) { setTitle(""); setDescription(""); + setAudienceKind("school"); + setAudienceRole("student"); + setAudienceCourse(""); + setCapacity(""); setStartsDate(""); setStartsTime(""); setEndsDate(""); @@ -126,6 +156,7 @@ export function EventForm(props: { const values = { title: title().trim(), description: description(), + audience: audienceValue(), starts_at, ends_at, }; @@ -162,6 +193,43 @@ export function EventForm(props: { onInput={(e) => setDescription(e.currentTarget.value)} />
+
+ + +
+ +
+ + +
+
+ +
+ + +
+
+ +
+ + setCapacity(e.currentTarget.value)} + /> +
+
diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index e65b831..eaf5e7c 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -211,6 +211,16 @@ export type MessageKey = | "events.noAttendees" | "events.userId" | "events.status" + | "events.audience" + | "events.audience.school" + | "events.audience.role" + | "events.audience.course" + | "events.audience.registration" + | "events.selectCourse" + | "events.capacity" + | "events.capacityOptional" + | "events.audienceCourseRequired" + | "events.audienceCapacityInvalid" | "events.helpTitle" | "events.helpBody" | "events.clearStart" @@ -742,6 +752,16 @@ const en: Dict = { "events.noAttendees": "No users found", "events.userId": "User id", "events.status": "Status", + "events.audience": "Audience", + "events.audience.school": "Whole school", + "events.audience.role": "Role", + "events.audience.course": "Course roster", + "events.audience.registration": "Registration list", + "events.selectCourse": "Select course", + "events.capacity": "Capacity", + "events.capacityOptional": "Optional seat cap", + "events.audienceCourseRequired": "Select a course for this audience.", + "events.audienceCapacityInvalid": "Capacity must be at least 1.", "events.helpTitle": "About events", "events.helpBody": "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 = { "events.noAttendees": "Kullanıcı bulunamadı", "events.userId": "Kullanıcı id", "events.status": "Durum", + "events.audience": "Hedef kitle", + "events.audience.school": "Tüm okul", + "events.audience.role": "Rol", + "events.audience.course": "Ders listesi", + "events.audience.registration": "Kayıt listesi", + "events.selectCourse": "Ders seç", + "events.capacity": "Kapasite", + "events.capacityOptional": "İsteğe bağlı kontenjan", + "events.audienceCourseRequired": "Bu hedef kitle için ders seç.", + "events.audienceCapacityInvalid": "Kapasite en az 1 olmalı.", "events.helpTitle": "Etkinlikler hakkında", "events.helpBody": "Öğ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.", diff --git a/src/pages/event-detail-page.tsx b/src/pages/event-detail-page.tsx index 13dcac8..fa1a1d0 100644 --- a/src/pages/event-detail-page.tsx +++ b/src/pages/event-detail-page.tsx @@ -7,7 +7,8 @@ import { getEventById } from "@/api/getEventById"; import { patchEventById } from "@/api/patchEventById"; import { postEventAttendance } from "@/api/postEventAttendance"; import { formatApiError } from "@/api/client"; -import type { AttendanceStatus } from "@/api/types"; +import type { AttendanceStatus, EventAudience } from "@/api/types"; +import type { MessageKey } from "@/i18n/messages"; import { AttendanceStatusPicker } from "@/components/events/attendance-status-picker"; import { AttendanceTable } from "@/components/events/attendance-table"; import { EventForm } from "@/components/events/event-form"; @@ -26,6 +27,13 @@ import { hasMinRole } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; import { usePreferences, useT } from "@/stores/preferences-context"; +function audienceLabel(audience: EventAudience, t: ReturnType): string { + if (audience.kind === "role") return `${t("events.audience.role")}: ${t(`role.${audience.role}` as MessageKey)}`; + if (audience.kind === "course") return t("events.audience.course"); + if (audience.kind === "registration") return t("events.audience.registration"); + return t("events.audience.school"); +} + export default function EventDetailPage() { return ( @@ -144,6 +152,7 @@ function EventDetailContent() {

{ev().description || "—"}

+

{audienceLabel(ev().audience, t)}

@@ -175,6 +184,7 @@ function EventDetailContent() { const body: Record = { title: values.title, description: values.description, + audience: values.audience, }; if (values.starts_at !== undefined) body.starts_at = values.starts_at; if (values.ends_at !== undefined) body.ends_at = values.ends_at; diff --git a/src/pages/events-page.tsx b/src/pages/events-page.tsx index a8dbba3..a1ea192 100644 --- a/src/pages/events-page.tsx +++ b/src/pages/events-page.tsx @@ -2,7 +2,7 @@ import { For, Show, Suspense, createMemo, createResource, createSignal } from "s import { getEvents } from "@/api/getEvents"; import { postEvent } from "@/api/postEvent"; import { formatApiError } from "@/api/client"; -import type { Event } from "@/api/types"; +import type { Event, EventAudience } from "@/api/types"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { EventCard } from "@/components/events/event-card"; @@ -111,8 +111,10 @@ function EventsContent() { description?: string; starts_at?: number; ends_at?: number; + audience?: EventAudience; } = { title: values.title }; if (values.description) body.description = values.description; + body.audience = values.audience; if (values.starts_at != null) body.starts_at = values.starts_at; if (values.ends_at != null) body.ends_at = values.ends_at; await postEvent(body);