Contactformulier: cms-client (validatie, reducer, client, hook), formulier in stijl B en beschikbaarheid

Headless pakket src/cms-client met importgrens, formulier dat pas verschijnt als de API antwoordt
(anders blijft de mailto-knop), honeypot, idempotentie, token-vernieuwing en property-based tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 13:13:24 +01:00
co-authored by Claude Sonnet 5.5
parent 9661b64635
commit aa843a5675
45 changed files with 2609 additions and 22 deletions
@@ -0,0 +1,30 @@
# Code Summary — contact-form (U4)
## What was built
- **Movable package `src/cms-client/`** (no imports from the app; ESLint `no-restricted-imports` plus a boundary test): `http/` (`apiUrl`, `fetchJson` with 10 s timeout, problem+json parsing), `contact/` (`validateContact`, `contactFormReducer`, `createContactClient`, `messages.nl`, `useContactForm`, types), `README.md`.
- **Validation** equal to the API (trim; 100/254/100/2000; email regex; control characters rejected except line breaks in the message; first failing rule per field). **Reducer**: `sent` only leaves through `reset`; edits and a second submit are ignored while `sending`; input is kept on `failed`.
- **Client**: `getToken` (200 → token, 404 → closed, anything else → failed, malformed body → failed) and `submit` (202 ok; 400 → field errors or token expired; 413; 404; 429 with `Retry-After`; 5xx/network → failed). Never throws, never logs.
- **Hook `useContactForm`**: prop getters with ids from `idPrefix`, `aria-invalid`/`aria-describedby`, honeypot props (silent success without a request when filled), one idempotency key per attempt series (reused on retry, renewed after success/reset), a single automatic token refresh on `expired`, preselect with change counter (unknown values ignored), focus on the first invalid field after the render, telemetry only through `onEvent('sent' | 'failed')`.
- **UI**: `ContactPanel` (mailto panel by default, upgrades to the form when `GET …/token` answers 200 without captcha; min height 280px), `MailtoPanel`, `ContactFormView` (design B: panel colours, underline inputs on `bg-field`, brand submit button, errors outside the labels, `role=alert` failure with the mail address, `role=status` success with focus, privacy sentence), hidden honeypot field. `useContactAvailability` starts the token request when the section nears the viewport (IntersectionObserver, `rootMargin 300px`), or on first focus without IntersectionObserver. Subject options = offerings (slug/title) + "Overig". `trackEvent('contact-sent' | 'contact-failed')` without data.
- **Tokens**: new `--color-danger` (error text) in all four sets, contrast-tested against `panel` and `field`.
- **Dependencies**: `fast-check` (dev) for property-based tests; no runtime dependency.
## Verification (Step 13.5, real output)
- Before this unit (after U3): 14 files / 154 tests. After: **19 files / 239 tests passed**; `pnpm lint` 0 errors (same 2 existing warnings, boundary rule active); `pnpm build` OK (JS 466 kB, CSS 58.8 kB).
- Found and fixed by the tests: (1) field errors inside the `<label>` polluted the accessible name of the input → errors now sit outside the label; (2) focus after server field errors did not move because inputs are disabled while sending → focus is applied after the render.
- New tests: validation examples and properties (trim invariance, inclusive limits, control characters, known codes), reducer examples and properties (absorbing `sent`, ignored edits while sending, values kept on failure), client against a contract-following mocked `fetch` (all status mappings, timeout, serialization and problem-parsing round trips), boundary, availability (200/404/500/429/captcha/malformed/network, once only, first-focus fallback), full UI flows (success with body/headers, validation + focus, blur errors, honeypot, server field errors, token refresh with same key, 413/429 minutes, failure + retry with same key, double submit, reset with new key, preselect, no PII in console/analytics).
## Notes and deviations
- **Mail address** is not assembled after mount (BR-U4-10): the address is already present as a literal in the section (visible mailto link) and the site is a client-rendered SPA, so the extra step would add nothing; kept simple.
- **No end-to-end test** against the real API is possible yet: the contact routes exist only on the unmerged CMS branch and answer 404 in production until released and switched on, which keeps the mailto panel (by design).
- `style-src 'unsafe-inline'` / headers: see the infrastructure design (Operations list).
## Enabling end to end later (checklist)
1. CMS branch rebased/merged and deployed; per-client rate limiting active on the API host.
2. `Contact:Enabled=true` on the host; then the CMS switch (`PUT /api/v1/contact-settings` or the Berichten screen).
3. Confirm the header/CSP setup of the delivering web server (SECURITY-04) and `connect-src 'self'`.
4. Check on the test environment first: token 200 → form appears; submit → message in the CMS.
## Hand-offs
- U5: build `ContactFormTerminal` on `useContactForm` (same options); reuse `ContactPanel` logic (availability) for the 1989 terminal; texts are injectable via `messages`.
- CMS: type generation from `openapi/slpsoftware.v1.json` and moving `src/cms-client` into SlpModularCms are follow-ups.