186 lines
5.7 KiB
TypeScript
186 lines
5.7 KiB
TypeScript
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 { contactForm, contactInfo } from '../../features/landing/data/content';
|
|
import { usePackagesQuery } from '../../features/landing/hooks/usePackagesQuery';
|
|
import { trackEvent } from '../../shared/analytics/trackEvent';
|
|
import { useContactPreselect } from '../../shared/contact/useContactPreselect';
|
|
import { terminalText } from './eraContent';
|
|
|
|
interface TerminalFormProps {
|
|
client: ContactClient;
|
|
token: ContactToken;
|
|
refreshToken: () => Promise<TokenResult>;
|
|
}
|
|
|
|
function Row({
|
|
label,
|
|
dots,
|
|
error,
|
|
errorId,
|
|
children,
|
|
}: {
|
|
label: string;
|
|
dots: string;
|
|
error: string | null;
|
|
errorId: string;
|
|
children: ReactNode;
|
|
}) {
|
|
return (
|
|
<div>
|
|
<label className="flex flex-wrap items-baseline gap-x-3.5 gap-y-1 uppercase">
|
|
<span className="min-w-[11ch]">
|
|
{label}
|
|
<span aria-hidden="true">{dots}</span>
|
|
</span>
|
|
{children}
|
|
</label>
|
|
{error ? (
|
|
<span id={errorId} className="e-term-error mt-1 block uppercase" data-testid="field-error">
|
|
{error}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** The 1989 terminal skin of the contact form, on the same logic as design B (useContactForm). */
|
|
export function TerminalForm({ client, token, refreshToken }: TerminalFormProps) {
|
|
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="outline-none"
|
|
data-testid="contact-success"
|
|
>
|
|
<p className="m-0">{terminalText.sentLine1}</p>
|
|
<p className="m-0 mb-5">{terminalText.sentLine2}</p>
|
|
<button
|
|
type="button"
|
|
onClick={form.reset}
|
|
className="e-term-btn e-term-btn-ghost uppercase"
|
|
data-testid="contact-reset"
|
|
>
|
|
[ {terminalText.newMessage} ]
|
|
</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-[18px] border-4 border-double border-[#ffb000] p-[clamp(18px,3vw,32px)]"
|
|
data-testid="contact-form"
|
|
>
|
|
<Row label="Naam" dots="....:" error={name.error} errorId={name.errorId}>
|
|
<input type="text" autoComplete="name" className="e-term-input" {...name.inputProps} />
|
|
</Row>
|
|
<Row label="E-mail" dots="..:" error={email.error} errorId={email.errorId}>
|
|
<input type="email" autoComplete="email" className="e-term-input" {...email.inputProps} />
|
|
</Row>
|
|
<Row label="Onderwerp" dots=":" error={subject.error} errorId={subject.errorId}>
|
|
<select className="e-term-box flex-[1_1_240px] min-w-0" {...subject.inputProps}>
|
|
<option value="" disabled>
|
|
-- KIES --
|
|
</option>
|
|
{subjectOptions.map((option) => (
|
|
<option key={option.value} value={option.value}>
|
|
{option.label.toUpperCase()}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Row>
|
|
<div>
|
|
<label className="flex flex-col gap-1.5 uppercase">
|
|
<span>
|
|
Bericht<span aria-hidden="true">:</span>
|
|
</span>
|
|
<textarea rows={5} className="e-term-box resize-y" {...message.inputProps} />
|
|
</label>
|
|
{message.error ? (
|
|
<span
|
|
id={message.errorId}
|
|
className="e-term-error mt-1 block uppercase"
|
|
data-testid="field-error"
|
|
>
|
|
{message.error}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
|
|
{/* Honeypot: invisible to people and assistive technology; bots fill it. */}
|
|
<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="e-term-error m-0" data-testid="contact-failure">
|
|
{form.failureMessage}
|
|
</p>
|
|
) : null}
|
|
|
|
<div className="mt-1 flex flex-wrap items-center gap-x-6 gap-y-2.5">
|
|
<button
|
|
type="submit"
|
|
disabled={form.status === 'sending'}
|
|
className="e-term-btn uppercase"
|
|
data-testid="contact-submit"
|
|
>
|
|
[{' '}
|
|
{form.status === 'sending'
|
|
? form.messages.sendingLabel
|
|
: form.status === 'failed'
|
|
? terminalText.retry
|
|
: terminalText.send}{' '}
|
|
]
|
|
</button>
|
|
<span className="text-xl text-[#d99600]">{contactForm.privacyNote.toUpperCase()}</span>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|