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:
@@ -0,0 +1,164 @@
|
||||
import { useEffect, useMemo, useRef } from 'react';
|
||||
import type { ReactNode } from 'react';
|
||||
import {
|
||||
useContactForm,
|
||||
type ContactClient,
|
||||
type ContactToken,
|
||||
type SubjectOption,
|
||||
type TokenResult,
|
||||
} from '../../../cms-client/contact';
|
||||
import { trackEvent } from '../../../shared/analytics/trackEvent';
|
||||
import { useContactPreselect } from '../../../shared/contact/useContactPreselect';
|
||||
import { contactForm, contactInfo } from '../data/content';
|
||||
import { usePackagesQuery } from '../hooks/usePackagesQuery';
|
||||
|
||||
const INPUT =
|
||||
'w-full rounded-none border-0 border-b-2 border-text bg-field px-3.5 py-[13px] text-[17px] font-normal text-text focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent disabled:opacity-70';
|
||||
|
||||
interface ContactFormViewProps {
|
||||
client: ContactClient;
|
||||
token: ContactToken;
|
||||
refreshToken: () => Promise<TokenResult>;
|
||||
}
|
||||
|
||||
function Field({
|
||||
label,
|
||||
children,
|
||||
error,
|
||||
errorId,
|
||||
}: {
|
||||
label: string;
|
||||
children: ReactNode;
|
||||
error: string | null;
|
||||
errorId: string;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<label className="flex flex-col gap-1.5 text-sm font-bold">
|
||||
{label}
|
||||
{children}
|
||||
</label>
|
||||
{error ? (
|
||||
<span id={errorId} className="text-sm font-medium text-danger" data-testid="field-error">
|
||||
{error}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** The contact form of design B (panel colours, underline inputs, brand submit button). */
|
||||
export function ContactFormView({ client, token, refreshToken }: ContactFormViewProps) {
|
||||
const { data: packages } = usePackagesQuery();
|
||||
const preselect = useContactPreselect();
|
||||
const subjectOptions = useMemo<SubjectOption[]>(
|
||||
() => [
|
||||
...(packages ?? []).map((pkg) => ({ value: pkg.slug, label: pkg.title })),
|
||||
{ value: 'other', label: contactForm.otherSubjectLabel },
|
||||
],
|
||||
[packages],
|
||||
);
|
||||
|
||||
const form = useContactForm({
|
||||
idPrefix: 'contact',
|
||||
subjectOptions,
|
||||
preselect: { subject: preselect.subject, key: preselect.key },
|
||||
client,
|
||||
token,
|
||||
refreshToken,
|
||||
fallbackAddress: contactInfo.email,
|
||||
onEvent: (event) => trackEvent(event === 'sent' ? 'contact-sent' : 'contact-failed'),
|
||||
});
|
||||
|
||||
const successRef = useRef<HTMLDivElement>(null);
|
||||
useEffect(() => {
|
||||
if (form.status === 'sent') {
|
||||
successRef.current?.focus();
|
||||
}
|
||||
}, [form.status]);
|
||||
|
||||
if (form.status === 'sent') {
|
||||
return (
|
||||
<div
|
||||
ref={successRef}
|
||||
tabIndex={-1}
|
||||
role="status"
|
||||
className="py-6 outline-none"
|
||||
data-testid="contact-success"
|
||||
>
|
||||
<p className="mb-3 text-[clamp(30px,3.4vw,44px)] font-extrabold leading-none tracking-[-0.02em] [font-stretch:112%]">
|
||||
Bedankt!
|
||||
</p>
|
||||
<p className="mb-7 text-body">{contactForm.successText}</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={form.reset}
|
||||
className="min-h-[44px] bg-text px-[22px] py-[15px] text-[15px] font-bold text-panel"
|
||||
data-testid="contact-reset"
|
||||
>
|
||||
Nog een bericht sturen
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const name = form.getFieldProps('name');
|
||||
const email = form.getFieldProps('email');
|
||||
const subject = form.getFieldProps('subject');
|
||||
const message = form.getFieldProps('message');
|
||||
const { id: hpId, ...hpProps } = form.honeypotProps;
|
||||
|
||||
return (
|
||||
<form {...form.formProps} className="flex flex-col gap-6" data-testid="contact-form">
|
||||
<div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
|
||||
<Field label="Naam" error={name.error} errorId={name.errorId}>
|
||||
<input type="text" autoComplete="name" className={INPUT} {...name.inputProps} />
|
||||
</Field>
|
||||
<Field label="E-mail" error={email.error} errorId={email.errorId}>
|
||||
<input type="email" autoComplete="email" className={INPUT} {...email.inputProps} />
|
||||
</Field>
|
||||
</div>
|
||||
<Field label="Onderwerp" error={subject.error} errorId={subject.errorId}>
|
||||
<select className={INPUT} {...subject.inputProps}>
|
||||
<option value="" disabled>
|
||||
Kies een onderwerp
|
||||
</option>
|
||||
{subjectOptions.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
<Field label="Bericht" error={message.error} errorId={message.errorId}>
|
||||
<textarea rows={5} className={`${INPUT} resize-y`} {...message.inputProps} />
|
||||
</Field>
|
||||
|
||||
{/* Honeypot: invisible to people and assistive technology; bots fill it (BR-U4-7). */}
|
||||
<div aria-hidden="true" className="absolute -left-[9999px] h-0 w-0 overflow-hidden">
|
||||
<label htmlFor={hpId}>Laat dit veld leeg</label>
|
||||
<input type="text" id={hpId} {...hpProps} />
|
||||
</div>
|
||||
|
||||
{form.failureMessage ? (
|
||||
<p role="alert" className="font-medium text-danger" data-testid="contact-failure">
|
||||
{form.failureMessage}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={form.status === 'sending'}
|
||||
className="min-h-[44px] rounded-none bg-brand px-6 py-[18px] text-[17px] font-bold text-on-brand disabled:opacity-70"
|
||||
data-testid="contact-submit"
|
||||
>
|
||||
{form.status === 'sending'
|
||||
? form.messages.sendingLabel
|
||||
: form.status === 'failed'
|
||||
? 'Opnieuw proberen'
|
||||
: 'Verstuur'}
|
||||
</button>
|
||||
<p className="-mt-2 text-[13px] text-muted">{contactForm.privacyNote}</p>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { useMemo, useRef } from 'react';
|
||||
import { createContactClient } from '../../../cms-client/contact';
|
||||
import { useContactAvailability } from '../hooks/useContactAvailability';
|
||||
import { ContactFormView } from './ContactFormView';
|
||||
import { MailtoPanel } from './MailtoPanel';
|
||||
|
||||
/**
|
||||
* Panel inside the contact block: the mailto call-to-action by default (and whenever the API is
|
||||
* closed, failing or needs a captcha), the form once the API answers (BR-U4-2). The panel keeps a
|
||||
* minimum height so the page does not jump when it is upgraded.
|
||||
*/
|
||||
export function ContactPanel() {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const client = useMemo(() => createContactClient(), []);
|
||||
const { availability, refreshToken } = useContactAvailability(ref, client);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
className="min-h-[280px] min-w-0 flex-[1.4_1_520px] bg-panel p-[clamp(24px,4vw,48px)] text-text"
|
||||
data-testid="contact-panel"
|
||||
data-availability={availability.status}
|
||||
>
|
||||
{availability.status === 'open' ? (
|
||||
<ContactFormView client={client} token={availability.token} refreshToken={refreshToken} />
|
||||
) : (
|
||||
<MailtoPanel />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
import { contactInfo } from '../data/content';
|
||||
import { ContactPanel } from './ContactPanel';
|
||||
|
||||
/**
|
||||
* Contact block of design B: brand-red section with the display heading, intro, mail link and a
|
||||
* panel on the right. Until the contact form (unit U4) the panel holds the mailto call-to-action.
|
||||
* panel on the right (mailto call-to-action or the contact form, see ContactPanel).
|
||||
*/
|
||||
export function ContactSection() {
|
||||
const mailtoHref = `mailto:${contactInfo.email}?subject=${encodeURIComponent(contactInfo.mailSubject)}`;
|
||||
|
||||
return (
|
||||
<section id="contact" aria-labelledby="contact-heading" className="bg-brand text-on-brand">
|
||||
<div className="mx-auto max-w-[1280px] px-6 pb-24 pt-20 sm:px-8 md:pb-[152px] md:pt-36">
|
||||
@@ -33,24 +32,7 @@ export function ContactSection() {
|
||||
{contactInfo.email}
|
||||
</a>
|
||||
</div>
|
||||
<div
|
||||
className="min-w-0 flex-[1.4_1_520px] bg-panel p-[clamp(24px,4vw,48px)] text-text"
|
||||
data-testid="contact-panel"
|
||||
>
|
||||
<p className="mb-3 text-[clamp(28px,3vw,40px)] font-extrabold leading-none tracking-[-0.02em] [font-stretch:112%]">
|
||||
Vertel wat je voor ogen hebt
|
||||
</p>
|
||||
<p className="mb-7 max-w-[44ch] text-body">
|
||||
Stuur een bericht met je idee of vraag. Je krijgt binnen één werkdag reactie.
|
||||
</p>
|
||||
<a
|
||||
href={mailtoHref}
|
||||
className="inline-block bg-brand px-6 py-[18px] text-[17px] font-bold text-on-brand no-underline hover:opacity-90"
|
||||
data-testid="contact-mailto-button"
|
||||
>
|
||||
Stuur een bericht
|
||||
</a>
|
||||
</div>
|
||||
<ContactPanel />
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { contactInfo } from '../data/content';
|
||||
|
||||
/** Default state of the contact panel (and fallback): the mailto call-to-action. */
|
||||
export function MailtoPanel() {
|
||||
const mailtoHref = `mailto:${contactInfo.email}?subject=${encodeURIComponent(contactInfo.mailSubject)}`;
|
||||
|
||||
return (
|
||||
<div data-testid="mailto-panel">
|
||||
<p className="mb-3 text-[clamp(28px,3vw,40px)] font-extrabold leading-none tracking-[-0.02em] [font-stretch:112%]">
|
||||
Vertel wat je voor ogen hebt
|
||||
</p>
|
||||
<p className="mb-7 max-w-[44ch] text-body">
|
||||
Stuur een bericht met je idee of vraag. Je krijgt binnen één werkdag reactie.
|
||||
</p>
|
||||
<a
|
||||
href={mailtoHref}
|
||||
className="inline-block bg-brand px-6 py-[18px] text-[17px] font-bold text-on-brand no-underline hover:opacity-90"
|
||||
data-testid="contact-mailto-button"
|
||||
>
|
||||
Stuur een bericht
|
||||
</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user