From c908fbe9fcdbc611a42c3af1171776e6857cbca1 Mon Sep 17 00:00:00 2001 From: ngqile Date: Fri, 27 Nov 2026 00:23:56 +0800 Subject: [PATCH] feat(admin-portal): port babyboo's date picker and calendar Adds react-day-picker 10.0.1 (it brings date-fns). The portal bans native date inputs, and the events and leads forms need a date field. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- openspec/changes/add-admin-events/design.md | 2 +- openspec/changes/add-admin-events/proposal.md | 14 +- openspec/changes/add-admin-events/tasks.md | 4 +- .../app/components/ui/calendar.test.tsx | 23 +++ .../app/components/ui/calendar.tsx | 21 +++ .../app/components/ui/date-picker.test.tsx | 122 ++++++++++++++++ .../app/components/ui/date-picker.tsx | 138 ++++++++++++++++++ .../app/lib/date-input.test.ts | 24 +++ solvway-admin-portal/app/lib/date-input.ts | 24 +++ .../app/lib/i18n/defaultMessages.ts | 4 + solvway-admin-portal/package-lock.json | 43 ++++++ solvway-admin-portal/package.json | 1 + solvway-admin-portal/test/pickDate.ts | 32 ++++ 13 files changed, 443 insertions(+), 9 deletions(-) create mode 100644 solvway-admin-portal/app/components/ui/calendar.test.tsx create mode 100644 solvway-admin-portal/app/components/ui/calendar.tsx create mode 100644 solvway-admin-portal/app/components/ui/date-picker.test.tsx create mode 100644 solvway-admin-portal/app/components/ui/date-picker.tsx create mode 100644 solvway-admin-portal/app/lib/date-input.test.ts create mode 100644 solvway-admin-portal/app/lib/date-input.ts create mode 100644 solvway-admin-portal/test/pickDate.ts diff --git a/openspec/changes/add-admin-events/design.md b/openspec/changes/add-admin-events/design.md index 69b41a6..ad327db 100644 --- a/openspec/changes/add-admin-events/design.md +++ b/openspec/changes/add-admin-events/design.md @@ -67,7 +67,7 @@ Alternative: one `services-counter` endpoint that creates both in a transaction. ### 9. Date picker -Port babyboo's `app/components/ui/calendar.tsx` and `date-picker.tsx` (with their tests), adding `react-day-picker` 10.0.1 and `date-fns` ^4.4.0, the versions babyboo uses. Required by the native-date-input ban; change 4 reuses the picker on phones. +Port babyboo's `app/components/ui/calendar.tsx` and `date-picker.tsx` (with their tests), adding `react-day-picker` 10.0.1, the version babyboo uses (it depends on `date-fns` itself, so no direct `date-fns` dependency). Required by the native-date-input ban; change 4 reuses the picker on phones. ### 10. Module diff --git a/openspec/changes/add-admin-events/proposal.md b/openspec/changes/add-admin-events/proposal.md index faae25e..158f742 100644 --- a/openspec/changes/add-admin-events/proposal.md +++ b/openspec/changes/add-admin-events/proposal.md @@ -7,12 +7,13 @@ Lead capture (change 4) records every lead against an event, so staff need to cr ## What Changes - **Admin portal Events screens**: a paged list (search by name, filter by enabled), a create page and an edit page. Desktop-first, like Users and Roles. -- **`mobile-api` gains `/api/admin/events`**: list, get, create and update, guarded by a new `BO_EVENTS` module. They call the existing `services-counter` event endpoints (`findByPropertyAndPageable`, `findByUuid`, `save`); `services-counter` and its schema do not change. +- **`mobile-api` gains `/api/admin/events`**: list, get, create and update, guarded by a new `BO_EVENTS` module. They call the existing `services-counter` event endpoints (`findByPropertyAndPageable`, `findByUuid`, `save`); the `event` table does not change. - **Event fields kept** from Tastefully: name, location, start date, end date, enabled. Tastefully's backoffice rules carry over: name and location required, both dates required, end date not before start date, dates are whole days, new events start enabled. +- **Event team**: each event has a team of salespeople (active admin users), chosen on create and edit and stored in a new `services-counter` table `event_salesperson`. Change 4 limits a lead's salesperson to the event's team. - **Not kept**: the four Tastefully upload fields (floor plan, stage activities, lucky draw, taste rewards). Their columns stay in the table and are left untouched on update. - **No delete**, as in Tastefully. Leads will reference events, so an event is disabled instead. - **Back-office module** `BO_EVENTS` (`/events`) added to `service-system`.`module`; SQL in this change folder, mirrored in `local/seed/`. -- **Date picker**: the portal forbids native date inputs (an existing test), so babyboo's `DatePicker` and `Calendar` components are ported, adding `react-day-picker` and `date-fns`. Change 4 reuses them for the follow-up date. +- **Date picker**: the portal forbids native date inputs (an existing test), so babyboo's `DatePicker` and `Calendar` components are ported, adding `react-day-picker`. Change 4 reuses them for the follow-up date. Not in this change: event booths, products, receipts, lucky draw and the other Tastefully event sub-screens; event company scoping (the design keeps `user` as the only existing table that gains `company_id`). @@ -26,8 +27,9 @@ None. ## Impact -- `solvway-admin-portal`: new `app/events/**` pages, `app/api/events/**` route handlers, an events service, the ported date picker and calendar, `/events` in the route-module map. New dependencies `react-day-picker` and `date-fns`. +- `solvway-admin-portal`: new `app/events/**` pages, `app/api/events/**` route handlers, an events service, the ported date picker and calendar, `/events` in the route-module map. New dependency `react-day-picker`. - `solvway-mobile-api`: new `AdminEventController`, `AdminEventService`, DTOs and counter endpoint constants. -- Database `service-system`: one new `module` row. Additive only. -- `local/seed/03-colite.sql`: mirrors the module row. -- No change to `services-counter`, the gateway, or the `event` table. +- `solvway-services-counter`: `EventSalesperson` entity and team endpoints. +- Database: new table `service-counter`.`event_salesperson` and one new `service-system`.`module` row. Additive only. +- `local/seed/03-colite.sql`: mirrors both. +- No change to the gateway or the `event` table. diff --git a/openspec/changes/add-admin-events/tasks.md b/openspec/changes/add-admin-events/tasks.md index 604a0ab..46c3130 100644 --- a/openspec/changes/add-admin-events/tasks.md +++ b/openspec/changes/add-admin-events/tasks.md @@ -19,8 +19,8 @@ Babyboo source: `~/Projects/babyboo` (read only). Tastefully backoffice rules: ` ## 4. Admin portal -- [ ] 4.1 Port babyboo's `calendar.tsx` and `date-picker.tsx` (and their tests) into `app/components/ui/`, adding `react-day-picker` 10.0.1 and `date-fns` ^4.4.0 to `package.json` (`npm install`, commit the lockfile). Verify: `npm run lint && npx tsc --noEmit && npx vitest run` exit 0. - - Scope: `solvway-admin-portal/app/components/ui/**`, `solvway-admin-portal/package.json`, `solvway-admin-portal/package-lock.json` +- [x] 4.1 Port babyboo's `calendar.tsx` and `date-picker.tsx` (and their tests) into `app/components/ui/`, with their helpers `app/lib/date-input.ts` (and test), `test/pickDate.ts` and the four `Ui*` i18n keys, adding `react-day-picker` 10.0.1 to `package.json` (`npm install`, commit the lockfile; it brings `date-fns` itself). Verify: `npm run lint && npx tsc --noEmit && npx vitest run` exit 0. + - Scope: `solvway-admin-portal/app/components/ui/**`, `solvway-admin-portal/app/lib/date-input*`, `solvway-admin-portal/test/pickDate.ts`, `solvway-admin-portal/app/lib/i18n/defaultMessages.ts`, `solvway-admin-portal/package.json`, `solvway-admin-portal/package-lock.json` - [ ] 4.2 Add the Events screens on the Users pattern: `app/events/page.tsx` (list: search, enabled filter, sortable name/start/end, columns name, location, start, end, enabled; row Edit), `app/events/new/page.tsx`, `app/events/[uuid]/page.tsx`, an `EventForm` (name, location, start and end `DatePicker`, enabled on edit only, team multi-select from salesperson-options with inactive members shown marked), route handlers `app/api/events/**`, `app/lib/services/adminEvents.ts`, types, i18n defaults, and `/events` → `BO_EVENTS` in `routeModules`. Form checks required fields and end not before start before submitting; API errors show on the field. `TEAM_NOT_SAVED` lands on the edit page with its message. Tests first (vitest) for each portal-visible scenario. Verify: `npm run lint && npx tsc --noEmit && npx vitest run && npm run build` exit 0. - Scope: `solvway-admin-portal/app/**` diff --git a/solvway-admin-portal/app/components/ui/calendar.test.tsx b/solvway-admin-portal/app/components/ui/calendar.test.tsx new file mode 100644 index 0000000..26e41f4 --- /dev/null +++ b/solvway-admin-portal/app/components/ui/calendar.test.tsx @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest"; +import { render } from "@testing-library/react"; +import { Calendar } from "./calendar"; + +describe("Calendar", () => { + it("marks today, and only today, with rdp-today", () => { + const { container } = render( + , + ); + const marked = Array.from(container.querySelectorAll(".rdp-today")); + expect(marked).toHaveLength(1); + expect(marked[0]).toBe(container.querySelector('[data-day="2026-10-09"]')); + }); + + it("keeps the today marker on a day in the middle of a selected range", () => { + const { container } = render( + , + ); + const cell = container.querySelector('[data-day="2026-10-09"]')!; + expect(cell).toHaveClass("rdp-today", "rdp-selected", "rdp-range_middle"); + }); +}); diff --git a/solvway-admin-portal/app/components/ui/calendar.tsx b/solvway-admin-portal/app/components/ui/calendar.tsx new file mode 100644 index 0000000..e8e0064 --- /dev/null +++ b/solvway-admin-portal/app/components/ui/calendar.tsx @@ -0,0 +1,21 @@ +"use client" + +import * as React from "react" +import { DayPicker } from "react-day-picker" +import "react-day-picker/style.css" + +import { cn } from "@/app/lib/utils" + +/** shadcn-style Calendar on react-day-picker v10, themed by `.admin-calendar` in globals.css. */ +function Calendar({ className, captionLayout = "dropdown", ...props }: React.ComponentProps) { + return ( + + ) +} + +export { Calendar } diff --git a/solvway-admin-portal/app/components/ui/date-picker.test.tsx b/solvway-admin-portal/app/components/ui/date-picker.test.tsx new file mode 100644 index 0000000..1b55864 --- /dev/null +++ b/solvway-admin-portal/app/components/ui/date-picker.test.tsx @@ -0,0 +1,122 @@ +import { useState } from "react"; +import { describe, expect, it, vi } from "vitest"; +import { fireEvent, render, screen, within } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { DatePicker } from "./date-picker"; +import { pickDate } from "@/test/pickDate"; + +function Harness(props: { initial?: string; onChange?: (v: string) => void; min?: string; max?: string; disabled?: boolean }) { + const [value, setValue] = useState(props.initial ?? ""); + return ( + <> + { setValue(v); props.onChange?.(v); }} /> + + + ); +} + +describe("DatePicker", () => { + it("opens the calendar from the keyboard", () => { + render(); + fireEvent.keyDown(screen.getByLabelText("Expiry date"), { key: "Enter" }); + expect(screen.getByRole("dialog")).toBeInTheDocument(); + }); + + it("stays open when the box is clicked again", async () => { + const user = userEvent.setup(); + render(); + await user.click(screen.getByLabelText("Expiry date")); + await user.click(screen.getByLabelText("Expiry date")); + expect(screen.getByRole("dialog")).toBeInTheDocument(); + }); + + it("the pickDate helper refuses a disabled day instead of silently doing nothing", () => { + render(); + expect(() => pickDate("Expiry date", "2026-09-01")).toThrow(/not an enabled day/); + }); + + it("opens the calendar when the box is clicked", () => { + render(); + fireEvent.click(screen.getByLabelText("Expiry date")); + expect(screen.getByRole("dialog")).toBeInTheDocument(); + }); + + it("ignores typing", () => { + const onChange = vi.fn(); + render(); + expect(screen.getByLabelText("Expiry date")).toHaveAttribute("readonly"); + fireEvent.change(screen.getByLabelText("Expiry date"), { target: { value: "01/09/2026" } }); + expect(onChange).not.toHaveBeenCalled(); + expect(screen.getByLabelText("Expiry date")).toHaveValue(""); + }); + + it("picks a date through the helper", () => { + const onChange = vi.fn(); + render(); + pickDate("Expiry date", "2026-09-01"); + expect(onChange).toHaveBeenLastCalledWith("2026-09-01"); + expect(screen.getByLabelText("Expiry date")).toHaveValue("01/09/2026"); + }); + + it("Clear empties an optional field; a required one has no Clear", () => { + const onChange = vi.fn(); + const { unmount } = render(); + fireEvent.click(screen.getByLabelText("Expiry date")); + fireEvent.click(screen.getByRole("button", { name: "Clear" })); + expect(onChange).toHaveBeenLastCalledWith(""); + unmount(); + render( {}} />); + fireEvent.click(screen.getByLabelText("Due")); + expect(screen.queryByRole("button", { name: "Clear" })).not.toBeInTheDocument(); + }); + + it("fills the field from a picked day", async () => { + const onChange = vi.fn(); + render(); + await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); + await userEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: /September 1st, 2026/ })); + expect(onChange).toHaveBeenLastCalledWith("2026-09-01"); + expect(screen.getByLabelText("Expiry date")).toHaveValue("01/09/2026"); + }); + + it("does not wipe the value when the selected day is clicked again", async () => { + const onChange = vi.fn(); + render(); + await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); + await userEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: /September 15th, 2026/ })); + expect(onChange).not.toHaveBeenCalledWith(""); + }); + + it("disables days outside min and max", async () => { + render(); + await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); + const dialog = screen.getByRole("dialog"); + expect(within(dialog).getByRole("button", { name: /September 9th, 2026/ })).toBeDisabled(); + expect(within(dialog).getByRole("button", { name: /September 21st, 2026/ })).toBeDisabled(); + expect(within(dialog).getByRole("button", { name: /September 10th, 2026/ })).toBeEnabled(); + }); + + it("disables the calendar button when the field is disabled", () => { + render(); + expect(screen.getByLabelText("Expiry date")).toBeDisabled(); + expect(screen.getByRole("button", { name: "Open calendar for Expiry date" })).toBeDisabled(); + }); + + it("passes id and required to the text box so