From d50ee5bceabbda242bd7c177a9827397e22b961e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 16:24:12 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=93=85=20appointment=20booking,?= =?UTF-8?q?=20calendar=20sync,=20cancel/reject=20records=20-=20mirror=20th?= =?UTF-8?q?e=20backend=20appointments=20contract:=2012=20endpoints=20under?= =?UTF-8?q?=20=20=20src/api/appointments=20(publish/list/delete=20slots,?= =?UTF-8?q?=20book,=20list,=20=20=20approve/reject/cancel,=20reschedule=20?= =?UTF-8?q?+=20accept/decline),=20types=20+=20LIMITS,=20=20=20full=20API?= =?UTF-8?q?=20tests=20-=20/appointments=20page,=20role-scoped:=20teachers?= =?UTF-8?q?=20publish=20one-off/weekly-repeat=20=20=20slots=20and=20manage?= =?UTF-8?q?=20requests;=20students/parents=20book,=20cancel,=20accept=20or?= =?UTF-8?q?=20=20=20decline=20a=20reschedule=20-=20publish/book/reschedule?= =?UTF-8?q?=20side=20panels;=20client-side=2052-occurrence=20cap=20and=20?= =?UTF-8?q?=20=20future-time=20validation;=20errors=20surface=20inside=20t?= =?UTF-8?q?he=20panel=20-=20calendar=20sync:=20appointments=20layered=20in?= =?UTF-8?q?to=20the=20month=20calendar=20grid=20and=20the=20=20=20right-na?= =?UTF-8?q?v=20calendar=20panel=20(violet=20accent,=20.latest=20reads,=203?= =?UTF-8?q?0s=20poll)=20-=20cancel=20+=20reject=20capture=20an=20optional?= =?UTF-8?q?=20reason;=20ConfirmDialog=20gains=20a=20reason=20=20=20prompt,?= =?UTF-8?q?=20a=20configurable=20icon,=20and=20opt-in=20backdrop=20dismiss?= =?UTF-8?q?=20-=20cancellation/rejection=20record=20(who=20+=20reason)=20s?= =?UTF-8?q?hown=20in=20a=20Details=20dialog=20=20=20from=20the=20row's=20t?= =?UTF-8?q?hree-dot=20menu=20-=20appointment-status=20+=20datetime-input?= =?UTF-8?q?=20lib=20helpers;=20EN/TR=20i18n;=20nav=20item=20in=20=20=20the?= =?UTF-8?q?=20classes=20and=20parent=20groups;=20role-scope-matrix=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 2 +- docs/role-scope-matrix.md | 4 + .../appointments/appointments.test.ts | 190 +++++++ src/api/appointments/deleteSlotById.ts | 5 + src/api/appointments/deleteSlotSeries.ts | 5 + src/api/appointments/getAppointments.ts | 8 + src/api/appointments/getSlots.ts | 8 + src/api/appointments/index.ts | 12 + src/api/appointments/patchAcceptReschedule.ts | 6 + .../appointments/patchApproveAppointment.ts | 6 + .../appointments/patchCancelAppointment.ts | 11 + .../appointments/patchDeclineReschedule.ts | 6 + .../appointments/patchRejectAppointment.ts | 11 + .../patchRescheduleAppointment.ts | 11 + src/api/appointments/postAppointment.ts | 11 + src/api/appointments/postSlots.ts | 14 + src/api/client/types.ts | 37 ++ .../appointments/book-appointment-form.tsx | 82 +++ .../appointments/publish-slots-form.tsx | 163 ++++++ .../appointments/reschedule-form.tsx | 99 ++++ src/components/layout/nav-items.ts | 4 +- src/components/layout/right-nav.tsx | 178 ++++-- src/components/ui/confirm-dialog.tsx | 43 +- src/components/ui/dialog.tsx | 14 +- src/i18n/messages.ts | 203 ++++++- src/lib/appointment-status.test.ts | 22 + src/lib/appointment-status.ts | 21 + src/lib/datetime-input.test.ts | 18 + src/lib/datetime-input.ts | 44 ++ src/pages/appointments-page.tsx | 533 ++++++++++++++++++ src/pages/calendar-page.tsx | 64 ++- src/routes/router.tsx | 8 + 32 files changed, 1782 insertions(+), 61 deletions(-) create mode 100644 src/api/__tests__/appointments/appointments.test.ts create mode 100644 src/api/appointments/deleteSlotById.ts create mode 100644 src/api/appointments/deleteSlotSeries.ts create mode 100644 src/api/appointments/getAppointments.ts create mode 100644 src/api/appointments/getSlots.ts create mode 100644 src/api/appointments/index.ts create mode 100644 src/api/appointments/patchAcceptReschedule.ts create mode 100644 src/api/appointments/patchApproveAppointment.ts create mode 100644 src/api/appointments/patchCancelAppointment.ts create mode 100644 src/api/appointments/patchDeclineReschedule.ts create mode 100644 src/api/appointments/patchRejectAppointment.ts create mode 100644 src/api/appointments/patchRescheduleAppointment.ts create mode 100644 src/api/appointments/postAppointment.ts create mode 100644 src/api/appointments/postSlots.ts create mode 100644 src/components/appointments/book-appointment-form.tsx create mode 100644 src/components/appointments/publish-slots-form.tsx create mode 100644 src/components/appointments/reschedule-form.tsx create mode 100644 src/lib/appointment-status.test.ts create mode 100644 src/lib/appointment-status.ts create mode 100644 src/lib/datetime-input.test.ts create mode 100644 src/lib/datetime-input.ts create mode 100644 src/pages/appointments-page.tsx diff --git a/AGENTS.md b/AGENTS.md index 8653c7c..9f89e22 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,7 +38,7 @@ instructions. Always written and read in English. `components/events/`, `components/exams/`, `components/users/`, `components/attendance/`, `components/courses/`, `components/marks/`, `components/sessions/`, `components/messages/`, `components/homework/`, - `components/layout/`, `components/ui/`. + `components/appointments/`, `components/layout/`, `components/ui/`. - Cross-cutting app state lives in `src/stores/` (`auth-context.tsx`, `preferences-context.tsx`) — context providers only, never domain components. School-policy lists (exam kinds, attendance statuses, grade bands) come from diff --git a/docs/role-scope-matrix.md b/docs/role-scope-matrix.md index ae7b3ad..44d4bf8 100644 --- a/docs/role-scope-matrix.md +++ b/docs/role-scope-matrix.md @@ -16,6 +16,7 @@ | Live monitor `/exams/:id/live` | ❌ | ✅ | ✅ | ✅ | | Events `/events` | ✅ | ✅ | ✅ | ✅ | | Event detail `/events/:id` | ✅ | ✅ | ✅ | ✅ | +| Appointments `/appointments` | ✅ (book + own) | ✅ (publish/manage own) | ✅ (manage all) | ✅ (manage all) | | **Report card `/marks`** | ✅ | ❌ (FE) | ❌ (FE) | ❌ (FE) | | My attendance `/attendance` | ✅ | ✅ | ✅ | ✅ | | Student marks lookup `/management/student-marks` | ❌ | ✅ | ✅ | ✅ | @@ -64,6 +65,9 @@ | Mark own attendance on events | ✅ | ✅ | ✅ | ✅ | | Mark other users' event attendance | ❌ | ✅ | ✅ | ✅ | | Create / edit / delete events | ❌ | ✅ (creator or manager+) | ✅ | ✅ | +| Publish / delete appointment slots | ❌ | ✅ (own slots) | ✅ (any) | ✅ (any) | +| Approve / reject / reschedule / cancel bookings | ❌ | ✅ (own slots) | ✅ (any) | ✅ (any) | +| Book an appointment (+ cancel / accept reschedule) | ✅ (student & parent) | ❌ | ❌ | ❌ | | Enroll students / unenroll any enrollment | ❌ | ✅ (course creator) | ✅ | ✅ | | Edit / delete courses | ❌ | ✅ (course creator) | ✅ | ✅ | | Create / edit / delete course sessions | ❌ | ✅ (course manager) | ✅ | ✅ | diff --git a/src/api/__tests__/appointments/appointments.test.ts b/src/api/__tests__/appointments/appointments.test.ts new file mode 100644 index 0000000..67472fa --- /dev/null +++ b/src/api/__tests__/appointments/appointments.test.ts @@ -0,0 +1,190 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { postSlots } from "../../appointments"; +import { getSlots } from "../../appointments"; +import { deleteSlotById } from "../../appointments"; +import { deleteSlotSeries } from "../../appointments"; +import { postAppointment } from "../../appointments"; +import { getAppointments } from "../../appointments"; +import { patchApproveAppointment } from "../../appointments"; +import { patchRejectAppointment } from "../../appointments"; +import { patchCancelAppointment } from "../../appointments"; +import { patchRescheduleAppointment } from "../../appointments"; +import { patchAcceptReschedule } from "../../appointments"; +import { patchDeclineReschedule } from "../../appointments"; +import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; + +describe("appointments API", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("postSlots calls /appointments/slots with data", async () => { + const mockSlots = [{ id: "s1" }]; + mockFetchSuccess(mockSlots); + + const data = { starts_at: 1000, ends_at: 2000, note: "hi", repeat_weekly: true, until: 5000 }; + const result = await postSlots(data); + expect(result).toEqual(mockSlots); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("getSlots calls /appointments/slots with pagination", async () => { + const mockPage = { items: [{ id: "s1" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getSlots({ limit: 10 }); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots?limit=10"); + expect(init?.method).toBe("GET"); + }); + + it("deleteSlotById calls /appointments/slots/:id", async () => { + mockFetch204(); + + await deleteSlotById("s1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots/s1"); + expect(init?.method).toBe("DELETE"); + }); + + it("deleteSlotSeries calls /appointments/slots/series/:series", async () => { + mockFetch204(); + + await deleteSlotSeries("ser1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/slots/series/ser1"); + expect(init?.method).toBe("DELETE"); + }); + + it("postAppointment calls /appointments with data", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const data = { slot: "s1", reason: "need help" }; + const result = await postAppointment(data); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("getAppointments calls /appointments with pagination", async () => { + const mockPage = { items: [{ id: "a1" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getAppointments({ limit: 10 }); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments?limit=10"); + expect(init?.method).toBe("GET"); + }); + + it("patchApproveAppointment calls /appointments/:id/approve", async () => { + const mockAppt = { id: "a1", status: "approved" }; + mockFetchSuccess(mockAppt); + + const result = await patchApproveAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/approve"); + expect(init?.method).toBe("PATCH"); + }); + + it("patchRejectAppointment calls /appointments/:id/reject", async () => { + const mockAppt = { id: "a1", status: "rejected" }; + mockFetchSuccess(mockAppt); + + const result = await patchRejectAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reject"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBeUndefined(); + }); + + it("patchRejectAppointment sends an optional reason body", async () => { + mockFetchSuccess({ id: "a1", status: "rejected" }); + + await patchRejectAppointment("a1", { reason: "slot no longer available" }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reject"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify({ reason: "slot no longer available" })); + }); + + it("patchCancelAppointment calls /appointments/:id/cancel", async () => { + const mockAppt = { id: "a1", status: "cancelled" }; + mockFetchSuccess(mockAppt); + + const result = await patchCancelAppointment("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/cancel"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBeUndefined(); + }); + + it("patchCancelAppointment sends an optional reason body", async () => { + mockFetchSuccess({ id: "a1", status: "cancelled" }); + + await patchCancelAppointment("a1", { reason: "double booked" }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/cancel"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify({ reason: "double booked" })); + }); + + it("patchRescheduleAppointment calls /appointments/:id/reschedule with data", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const data = { starts_at: 3000, ends_at: 4000 }; + const result = await patchRescheduleAppointment("a1", data); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("patchAcceptReschedule calls /appointments/:id/reschedule/accept", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const result = await patchAcceptReschedule("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule/accept"); + expect(init?.method).toBe("PATCH"); + }); + + it("patchDeclineReschedule calls /appointments/:id/reschedule/decline", async () => { + const mockAppt = { id: "a1" }; + mockFetchSuccess(mockAppt); + + const result = await patchDeclineReschedule("a1"); + expect(result).toEqual(mockAppt); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/appointments/a1/reschedule/decline"); + expect(init?.method).toBe("PATCH"); + }); +}); diff --git a/src/api/appointments/deleteSlotById.ts b/src/api/appointments/deleteSlotById.ts new file mode 100644 index 0000000..cd83331 --- /dev/null +++ b/src/api/appointments/deleteSlotById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteSlotById(id: string): Promise { + return client(`/appointments/slots/${id}`, { method: "DELETE" }); +} diff --git a/src/api/appointments/deleteSlotSeries.ts b/src/api/appointments/deleteSlotSeries.ts new file mode 100644 index 0000000..b39e1e9 --- /dev/null +++ b/src/api/appointments/deleteSlotSeries.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteSlotSeries(series: string): Promise { + return client(`/appointments/slots/series/${series}`, { method: "DELETE" }); +} diff --git a/src/api/appointments/getAppointments.ts b/src/api/appointments/getAppointments.ts new file mode 100644 index 0000000..493fc88 --- /dev/null +++ b/src/api/appointments/getAppointments.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { Appointment } from "../client"; + +export async function getAppointments(params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/appointments${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/appointments/getSlots.ts b/src/api/appointments/getSlots.ts new file mode 100644 index 0000000..1b2878b --- /dev/null +++ b/src/api/appointments/getSlots.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { AppointmentSlot } from "../client"; + +export async function getSlots(params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/appointments/slots${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/appointments/index.ts b/src/api/appointments/index.ts new file mode 100644 index 0000000..be1fb3f --- /dev/null +++ b/src/api/appointments/index.ts @@ -0,0 +1,12 @@ +export * from "./postSlots"; +export * from "./getSlots"; +export * from "./deleteSlotById"; +export * from "./deleteSlotSeries"; +export * from "./postAppointment"; +export * from "./getAppointments"; +export * from "./patchApproveAppointment"; +export * from "./patchRejectAppointment"; +export * from "./patchCancelAppointment"; +export * from "./patchRescheduleAppointment"; +export * from "./patchAcceptReschedule"; +export * from "./patchDeclineReschedule"; diff --git a/src/api/appointments/patchAcceptReschedule.ts b/src/api/appointments/patchAcceptReschedule.ts new file mode 100644 index 0000000..3f2322a --- /dev/null +++ b/src/api/appointments/patchAcceptReschedule.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchAcceptReschedule(id: string): Promise { + return client(`/appointments/${id}/reschedule/accept`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchApproveAppointment.ts b/src/api/appointments/patchApproveAppointment.ts new file mode 100644 index 0000000..8979fd3 --- /dev/null +++ b/src/api/appointments/patchApproveAppointment.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchApproveAppointment(id: string): Promise { + return client(`/appointments/${id}/approve`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchCancelAppointment.ts b/src/api/appointments/patchCancelAppointment.ts new file mode 100644 index 0000000..9e9ee7f --- /dev/null +++ b/src/api/appointments/patchCancelAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type CancelAppointmentBody = { + /** Optional free-text reason; blank/omitted records no reason. */ + reason?: string; +}; + +export function patchCancelAppointment(id: string, body?: CancelAppointmentBody): Promise { + return client(`/appointments/${id}/cancel`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/patchDeclineReschedule.ts b/src/api/appointments/patchDeclineReschedule.ts new file mode 100644 index 0000000..0da8bb5 --- /dev/null +++ b/src/api/appointments/patchDeclineReschedule.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export function patchDeclineReschedule(id: string): Promise { + return client(`/appointments/${id}/reschedule/decline`, { method: "PATCH" }); +} diff --git a/src/api/appointments/patchRejectAppointment.ts b/src/api/appointments/patchRejectAppointment.ts new file mode 100644 index 0000000..5a34348 --- /dev/null +++ b/src/api/appointments/patchRejectAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type RejectAppointmentBody = { + /** Optional free-text reason; blank/omitted records no reason. */ + reason?: string; +}; + +export function patchRejectAppointment(id: string, body?: RejectAppointmentBody): Promise { + return client(`/appointments/${id}/reject`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/patchRescheduleAppointment.ts b/src/api/appointments/patchRescheduleAppointment.ts new file mode 100644 index 0000000..a44e918 --- /dev/null +++ b/src/api/appointments/patchRescheduleAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type RescheduleBody = { + starts_at: number; + ends_at: number; +}; + +export function patchRescheduleAppointment(id: string, body: RescheduleBody): Promise { + return client(`/appointments/${id}/reschedule`, { method: "PATCH", body }); +} diff --git a/src/api/appointments/postAppointment.ts b/src/api/appointments/postAppointment.ts new file mode 100644 index 0000000..0b0dd74 --- /dev/null +++ b/src/api/appointments/postAppointment.ts @@ -0,0 +1,11 @@ +import { client } from "../client"; +import type { Appointment } from "../client"; + +export type BookAppointmentBody = { + slot: string; + reason: string; +}; + +export function postAppointment(body: BookAppointmentBody): Promise { + return client("/appointments", { method: "POST", body }); +} diff --git a/src/api/appointments/postSlots.ts b/src/api/appointments/postSlots.ts new file mode 100644 index 0000000..c155809 --- /dev/null +++ b/src/api/appointments/postSlots.ts @@ -0,0 +1,14 @@ +import { client } from "../client"; +import type { AppointmentSlot } from "../client"; + +export type PublishSlotsBody = { + starts_at: number; + ends_at: number; + note?: string | null; + repeat_weekly?: boolean; + until?: number | null; +}; + +export function postSlots(body: PublishSlotsBody): Promise { + return client("/appointments/slots", { method: "POST", body }); +} diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 83ca367..78e3783 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -88,6 +88,43 @@ export type EventRegistration = { registered_by: PersonRef; }; +export type AppointmentStatus = "pending" | "approved" | "rejected" | "cancelled"; + +export type AppointmentSlot = { + id: string; + teacher: PersonRef; + starts_at: number; // unix ms UTC + ends_at: number; // unix ms UTC, half-open + note: string | null; + series: string | null; // null = one-off; shared id across a weekly series + created_at: number; +}; + +export type Appointment = { + id: string; + slot: string; + teacher: PersonRef | null; // null only if slot vanished + requester: PersonRef; + status: AppointmentStatus; + reason: string; + starts_at: number | null; // effective window (proposal if accepted, else slot's) + ends_at: number | null; + proposed_starts_at: number | null; + proposed_ends_at: number | null; + proposed_by: PersonRef | null; + decided_by: PersonRef | null; // null while pending + cancelled_by: PersonRef | null; // set only when status === "cancelled" + cancel_reason: string | null; // optional reason, null when none given + reject_reason: string | null; // optional reason when status === "rejected" (rejecter is decided_by) + created_at: number; +}; + +export const APPOINTMENT_LIMITS = { + noteMaxLen: 500, // MAX_APPOINTMENT_NOTE_LEN, empty allowed + reasonMaxLen: 1000, // MAX_APPOINTMENT_REASON_LEN, non-blank required + maxSlotOccurrences: 52, // MAX_SLOT_OCCURRENCES for repeat_weekly +} as const; + export type Course = { id: string; creator: PersonRef; diff --git a/src/components/appointments/book-appointment-form.tsx b/src/components/appointments/book-appointment-form.tsx new file mode 100644 index 0000000..57d5bcb --- /dev/null +++ b/src/components/appointments/book-appointment-form.tsx @@ -0,0 +1,82 @@ +import { createSignal } from "solid-js"; +import { APPOINTMENT_LIMITS, formatApiError, type AppointmentSlot } from "@/api/client"; +import { Button } from "@/components/ui/button"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { formatDateTime } from "@/lib/format"; +import { personLabel } from "@/lib/person"; +import { usePreferences, useT } from "@/stores/preferences-context"; + +export function BookAppointmentForm(props: { + slot: AppointmentSlot; + onSubmit: (reason: string) => Promise; + onCancel?: () => void; +}) { + const t = useT(); + const { locale } = usePreferences(); + const [reason, setReason] = createSignal(""); + const [error, setError] = createSignal(""); + const [pending, setPending] = createSignal(false); + + const handleSubmit = async (e: SubmitEvent) => { + e.preventDefault(); + const value = reason().trim(); + if (!value) { + setError(t("appointments.reasonRequired")); + return; + } + if (value.length > APPOINTMENT_LIMITS.reasonMaxLen) { + setError(t("form.descriptionMax")); + return; + } + setError(""); + setPending(true); + try { + await props.onSubmit(value); + } catch (err) { + setError(formatApiError(err)); + } finally { + setPending(false); + } + }; + + return ( +
+
+

+ {t("appointments.teacher")}:{" "} + {personLabel(props.slot.teacher)} +

+

+ {formatDateTime(props.slot.starts_at, locale())} — {formatDateTime(props.slot.ends_at, locale())} +

+
+ +
+ +