From f2f293ea58845dbb0386928443cacc1e259e1c81 Mon Sep 17 00:00:00 2001 From: ngqile Date: Thu, 3 Dec 2026 10:40:40 +0800 Subject: [PATCH] feat(pages): render card sections, CTA bands and section looks Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- openspec/changes/add-page-sections/design.md | 2 +- openspec/changes/add-page-sections/tasks.md | 2 +- .../pages/CmsBlocks.sections.test.tsx | 77 +++++++++++++++++ .../app/components/pages/CmsBlocks.tsx | 82 ++++++++++++++++++- .../app/lib/cmsPages.sections.test.ts | 32 ++++++++ solvway-admin-portal/app/lib/cmsPages.ts | 43 +++++++++- 6 files changed, 230 insertions(+), 8 deletions(-) create mode 100644 solvway-admin-portal/app/components/pages/CmsBlocks.sections.test.tsx create mode 100644 solvway-admin-portal/app/lib/cmsPages.sections.test.ts diff --git a/openspec/changes/add-page-sections/design.md b/openspec/changes/add-page-sections/design.md index 00183a0..4bba038 100644 --- a/openspec/changes/add-page-sections/design.md +++ b/openspec/changes/add-page-sections/design.md @@ -20,7 +20,7 @@ Design: `docs/superpowers/specs/2026-12-03-page-builder-design.md`, change 1. Bl ## Risks / Trade-offs - A Dark or Accent band right after another band of the same look merges visually → acceptable; staff see it in the preview. -- `HTML` blocks with a look: the band surrounds the frame; harmless. +- `HTML` blocks ignore a look: a Custom HTML block is a full design of its own, so the editor does not offer the picker for it and the renderer draws no band. - Rollback: pages saved with new types must be edited before the previous release saves them; their new sections render as nothing under it. ## Migration Plan diff --git a/openspec/changes/add-page-sections/tasks.md b/openspec/changes/add-page-sections/tasks.md index 1447063..a62c23b 100644 --- a/openspec/changes/add-page-sections/tasks.md +++ b/openspec/changes/add-page-sections/tasks.md @@ -9,7 +9,7 @@ Design: `design.md` and `docs/superpowers/specs/2026-12-03-page-builder-design.m ## 2. Portal -- [ ] 2.1 Model and renderer: types, `emptyBlock`, `BLOCK_LABELS`, icon map; `CmsBlocks` renders the four new types and the looks (full-width bands, readable text, Accent from the company colour); Default unchanged. +- [x] 2.1 Model and renderer: types, `emptyBlock`, `BLOCK_LABELS`, icon map; `CmsBlocks` renders the four new types and the looks (full-width bands, readable text, Accent from the company colour); Default unchanged. - [ ] 2.2 Editor: add buttons, fields and card lists (add, remove, move) for the new types, the look select on every block, inline card errors; labels in `defaultMessages.ts` and `zh_CN.seed.json`, `npm run i18n:sql`, `sql/01-section-labels.sql` and `sql/99-rollback.sql`. - Verify (2.1 to 2.2): `cd solvway-admin-portal && npx vitest run; echo VITEST=$? && npx tsc --noEmit && npm run lint` diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.sections.test.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.sections.test.tsx new file mode 100644 index 0000000..df69a48 --- /dev/null +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.sections.test.tsx @@ -0,0 +1,77 @@ +import { render, screen, within } from "@testing-library/react"; +import { describe, it, expect } from "vitest"; +import { LandingPage, type PublicBlock } from "./CmsBlocks"; + +// add-page-sections: card sections, the CTA band, section looks and the image-less hero. +const company = { code: "INS", name: "INS Sdn Bhd", logoImageId: null, accentColor: "#FFEE00" }; +const page = (blocks: PublicBlock[]) => ({ slug: "hotel", title: "Hotel day", blocks }); + +describe("LandingPage: sections", () => { + it("shows feature cards with their icons, titles and texts", () => { + render(); + expect(screen.getByRole("heading", { name: "What we do" })).toBeInTheDocument(); + expect(screen.getByText("One partner.")).toBeInTheDocument(); + const items = screen.getAllByRole("listitem"); + expect(items).toHaveLength(2); + expect(within(items[0]).getByRole("heading", { name: "Managed WiFi" })).toBeInTheDocument(); + expect(items[0].querySelector("svg.lucide-wifi")).not.toBeNull(); + expect(items[1].querySelector("svg.lucide-tv")).not.toBeNull(); + }); + + it("numbers steps in order", () => { + render(); + const list = screen.getByRole("list"); + expect(list.tagName).toBe("OL"); + const items = within(list).getAllByRole("listitem"); + expect(items.map((li) => li.textContent)).toEqual(["1SurveyWe visit.", "2InstallWe fit it.", "3SupportWe stay."]); + }); + + it("shows a trust strip of short items", () => { + render(); + expect(screen.getAllByRole("listitem").map((li) => li.textContent)).toEqual(["Certified with LG", "Local support"]); + }); + + it("shows a call-to-action band with its button, and drops an unsafe one", () => { + render(); + expect(screen.getByRole("heading", { name: "Ready?" })).toBeInTheDocument(); + expect(screen.getByRole("link", { name: "Get a quote" })).toHaveAttribute("href", "#signup"); + expect(screen.queryByRole("link", { name: "Evil" })).not.toBeInTheDocument(); + }); + + it("draws a non-default look as a full-width band and leaves default blocks as before", () => { + render(); + const band = (name: string) => screen.getByRole("heading", { name }).closest("[data-look]"); + expect(band("Dark")).toHaveAttribute("data-look", "DARK"); + expect(band("Dark")).toHaveClass("bg-foreground", "text-background"); + expect(band("Accent")).toHaveClass("bg-primary", "text-primary-foreground"); + expect(band("Muted")).toHaveClass("bg-muted"); + expect(band("Plain")).toBeNull(); + expect(band("Junk")).toBeNull(); + // On an accent band the button flips, so it never matches the band's colour. + expect(within(band("Accent") as HTMLElement).getByRole("link", { name: "Go" })).toHaveClass("bg-background", "text-foreground"); + }); + + it("shows a hero without an image as text on its look's background", () => { + render(); + expect(screen.getByRole("heading", { name: "No picture" })).toBeInTheDocument(); + expect(document.querySelector("main img")).toBeNull(); + expect(screen.getByRole("link", { name: "Join" })).toBeInTheDocument(); + // On a band the hero takes the band's colours; its own grey box would put light text on a light background. + expect(screen.getByRole("heading", { name: "No picture" }).closest("section")).not.toHaveClass("bg-muted"); + }); +}); diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx index 5cdd65f..95ab27e 100644 --- a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx @@ -1,5 +1,5 @@ import type { CSSProperties, ReactNode } from "react"; -import { anchorId, contrastText, imageSrc, isSafeLink, type CmsBlock } from "@/app/lib/cmsPages"; +import { ICONS, LOOKS, anchorId, contrastText, imageSrc, isSafeLink, type CmsBlock, type Look } from "@/app/lib/cmsPages"; import type { RegistrationInfo } from "@/app/lib/services/publicRegistration"; import { SafeHtml } from "./SafeHtml"; import { LandingForm } from "./LandingForm"; @@ -20,12 +20,21 @@ export interface Branding { const PROSE = "space-y-3 text-base leading-relaxed [&_a]:text-primary [&_a]:underline [&_blockquote]:border-l-2 [&_blockquote]:pl-3 [&_h2]:text-2xl [&_h2]:font-semibold [&_h3]:text-xl [&_h3]:font-semibold [&_ol]:list-decimal [&_ol]:pl-5 [&_ul]:list-disc [&_ul]:pl-5"; +// add-page-sections: non-default looks are full-width bands. On an accent band the button flips to stay visible. +const BANDS: Record, string> = { + MUTED: "bg-muted text-foreground", + DARK: "bg-foreground text-background", + ACCENT: "bg-primary text-primary-foreground", +}; +const bandOf = (look: unknown) => (LOOKS.includes(look as Look) && look !== "DEFAULT" ? (look as Exclude) : null); + function ButtonLink({ block }: { block: CmsBlock }) { if (!block.buttonLabel || !isSafeLink(block.buttonUrl)) return null; const external = /^https:/i.test(block.buttonUrl!); + const colours = bandOf(block.look) === "ACCENT" ? "bg-background text-foreground" : "bg-primary text-primary-foreground"; return ( + className={`inline-flex h-11 items-center rounded-md px-5 text-sm font-medium hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none ${colours}`}> {block.buttonLabel} ); @@ -40,7 +49,7 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i switch (block.type) { case "HERO": return ( -
+
{block.heading ?

{block.heading}

: null} @@ -83,6 +92,60 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
); + case "FEATURES": + return ( +
+
+ {block.heading ?

{block.heading}

: null} + {block.text ?

{block.text}

: null} +
+
    + {(block.cards ?? []).map((card, i) => { + const Icon = card.icon ? ICONS[card.icon] : undefined; + return ( +
  • + {Icon ?
  • + ); + })} +
+
+ ); + case "STEPS": + return ( +
+ {block.heading ?

{block.heading}

: null} +
    + {(block.cards ?? []).map((card, i) => ( +
  1. + +
    +

    {card.title}

    +

    {card.text}

    +
    +
  2. + ))} +
+
+ ); + case "TRUST": + return ( +
+
    + {(block.cards ?? []).map((card, i) =>
  • {card.title}
  • )} +
+
+ ); + case "CTA": + return ( +
+

{block.heading}

+ {block.text ?

{block.text}

: null} + +
+ ); default: return null; } @@ -90,6 +153,17 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i const WIDTH = "mx-auto w-full max-w-5xl px-4"; +/** A block in the page width; with a non-default look, inside a full-width band of that colour. */ +function Section({ block, children }: { block: PublicBlock; children: ReactNode }) { + const band = bandOf(block.look); + if (!band) return
{children}
; + return ( +
+
{children}
+
+ ); +} + /** * A whole landing page: the company header (logo, accent), the title as the only h1, then the blocks. Custom HTML * blocks run full width; with hideHeader the header goes and the title is for screen readers only. @@ -124,7 +198,7 @@ export function LandingPage({ company, page, banner, preview, frameBase }: {
{page.blocks.map((block, i) => block.type === "HTML" ? - :
)} + :
)}
diff --git a/solvway-admin-portal/app/lib/cmsPages.sections.test.ts b/solvway-admin-portal/app/lib/cmsPages.sections.test.ts new file mode 100644 index 0000000..453fb60 --- /dev/null +++ b/solvway-admin-portal/app/lib/cmsPages.sections.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect } from "vitest"; +import { BLOCK_TYPES, ICONS, LOOKS, emptyBlock, emptyPage, pageBody } from "./cmsPages"; + +// add-page-sections: the new block types in the editor model and the save body. +describe("cmsPages: sections", () => { + it("offers the four new types, the server's icon list and looks", () => { + expect(BLOCK_TYPES).toEqual(expect.arrayContaining(["FEATURES", "STEPS", "TRUST", "CTA"])); + expect(Object.keys(ICONS).sort()).toEqual(["calendar", "cloud", "headset", "lock", "monitor", "phone", "shield-check", "star", "tv", "users", "wifi", "wrench"]); + expect(LOOKS).toEqual(["DEFAULT", "MUTED", "DARK", "ACCENT"]); + }); + + it("starts each card section with the minimum cards and only the fields its type uses", () => { + expect(emptyBlock("FEATURES")).toEqual({ type: "FEATURES", heading: "", text: "", cards: [{ icon: "star", title: "", text: "" }, { icon: "star", title: "", text: "" }] }); + expect(emptyBlock("STEPS")).toEqual({ type: "STEPS", heading: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] }); + expect(emptyBlock("TRUST")).toEqual({ type: "TRUST", cards: [{ title: "" }, { title: "" }] }); + expect(emptyBlock("CTA")).toEqual({ type: "CTA", heading: "", text: "", buttonLabel: "", buttonUrl: "" }); + }); + + it("trims card fields and drops a default look on save", () => { + const body = pageBody({ + ...emptyPage(), title: "T", slug: "t", + blocks: [ + { type: "STEPS", heading: " How ", look: "DEFAULT", cards: [{ title: " Survey ", text: " We visit. " }, { title: "Fit", text: "" }] }, + { type: "CTA", heading: "Go", look: "DARK", buttonLabel: "Join", buttonUrl: "#signup" }, + ], + }); + expect(body.blocks).toEqual([ + { type: "STEPS", heading: "How", cards: [{ title: "Survey", text: "We visit." }, { title: "Fit", text: "" }] }, + { type: "CTA", heading: "Go", look: "DARK", buttonLabel: "Join", buttonUrl: "#signup" }, + ]); + }); +}); diff --git a/solvway-admin-portal/app/lib/cmsPages.ts b/solvway-admin-portal/app/lib/cmsPages.ts index 1c06793..e4aadd9 100644 --- a/solvway-admin-portal/app/lib/cmsPages.ts +++ b/solvway-admin-portal/app/lib/cmsPages.ts @@ -1,9 +1,21 @@ // Landing-page blocks and the editor's pure helpers (add-landing-pages), ported from babyboo's cmsPages.ts // minus the product blocks. Images are page_image ids, served at /api/public/page-images/. -export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML"; +import { Calendar, Cloud, Headset, Lock, Monitor, Phone, ShieldCheck, Star, Tv, Users, Wifi, Wrench, type LucideIcon } from "lucide-react"; -export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FAQ", "FORM", "HTML"]; +export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FAQ" | "FORM" | "HTML" | "FEATURES" | "STEPS" | "TRUST" | "CTA"; + +export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURES", "STEPS", "TRUST", "CTA", "FAQ", "FORM", "HTML"]; + +/** Feature-card icons by the name services-counter accepts (add-page-sections). */ +export const ICONS: Record = { + wifi: Wifi, phone: Phone, tv: Tv, monitor: Monitor, lock: Lock, "shield-check": ShieldCheck, + cloud: Cloud, wrench: Wrench, headset: Headset, users: Users, calendar: Calendar, star: Star, +}; + +/** Section looks: DEFAULT renders as before; the others are full-width bands. */ +export const LOOKS = ["DEFAULT", "MUTED", "DARK", "ACCENT"] as const; +export type Look = (typeof LOOKS)[number]; export const BLOCK_LABELS: Record = { HERO: "PagesBlockHero", @@ -12,8 +24,19 @@ export const BLOCK_LABELS: Record = { FAQ: "PagesBlockFaq", FORM: "PagesBlockForm", HTML: "PagesBlockHtml", + FEATURES: "PagesBlockFeatures", + STEPS: "PagesBlockSteps", + TRUST: "PagesBlockTrust", + CTA: "PagesBlockCta", }; +/** A FEATURES (icon, title, text), STEPS (title, text) or TRUST (title) card. */ +export interface CmsCard { + icon?: string; + title: string; + text?: string; +} + export interface CmsFaqItem { question: string; answer: string; @@ -31,6 +54,8 @@ export interface CmsBlock { items?: CmsFaqItem[]; /** Optional link name: the block's id on the page, the target of a #name link (add-page-anchor-links). */ anchor?: string; + look?: Look; + cards?: CmsCard[]; /** The FORM block's event (a Snowflake uuid, kept as a string). */ eventUuid?: string | null; } @@ -80,6 +105,14 @@ export function emptyBlock(type: BlockType): CmsBlock { return { type, heading: "", eventUuid: null }; case "HTML": return { type, html: "" }; + case "FEATURES": + return { type, heading: "", text: "", cards: [{ icon: "star", title: "", text: "" }, { icon: "star", title: "", text: "" }] }; + case "STEPS": + return { type, heading: "", cards: [{ title: "", text: "" }, { title: "", text: "" }] }; + case "TRUST": + return { type, cards: [{ title: "" }, { title: "" }] }; + case "CTA": + return { type, heading: "", text: "", buttonLabel: "", buttonUrl: "" }; default: return { type }; } @@ -113,6 +146,12 @@ export function pageBody(draft: CmsPageDraft) { out.items = (val as CmsFaqItem[]).map((it) => ({ question: it.question.trim(), answer: it.answer.trim() })); continue; } + if (key === "cards") { + // Trimmed but kept, so a blank card field is refused at its own path. + out.cards = (val as CmsCard[]).map((c) => Object.fromEntries(Object.entries(c).map(([k, x]) => [k, typeof x === "string" ? x.trim() : x]))); + continue; + } + if (key === "look" && val === "DEFAULT") continue; const v = typeof val === "string" ? val.trim() : val; if (!blank(v)) out[key] = v; }