Files
SlpSoftware/src/variants/e/TerminalForm.tsx
T

228 lines
7.1 KiB
TypeScript

import { lazy, Suspense, useEffect, useMemo, useRef, useState } 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 { t } from '../../i18n';
import { trackEvent } from '../../shared/analytics/trackEvent';
import { useContactPreselect } from '../../shared/contact/useContactPreselect';
import { buildSubjectOptions, subjectForSlug } from '../../shared/contact/subjects';
import { terminalText } from './eraContent';
const CaptchaWidget = lazy(() => import('../../shared/contact/CaptchaWidget'));
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[]>(
() => buildSubjectOptions(packages, contactForm.otherSubjectLabel),
[packages],
);
const form = useContactForm({
idPrefix: 'contact',
subjectOptions,
preselect: { subject: subjectForSlug(packages, preselect.subject), key: preselect.key },
initialSubject: subjectForSlug(packages, preselect.subject),
client,
token,
refreshToken,
fallbackAddress: contactInfo.email,
onEvent: (event) => trackEvent(event === 'sent' ? 'contact-sent' : 'contact-failed'),
});
const [captchaProblem, setCaptchaProblem] = useState(false);
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={t('era.terminalForm.labels.name')}
dots="....:"
error={name.error}
errorId={name.errorId}
>
<input type="text" autoComplete="name" className="e-term-input" {...name.inputProps} />
</Row>
<Row
label={t('era.terminalForm.labels.email')}
dots="..:"
error={email.error}
errorId={email.errorId}
>
<input type="email" autoComplete="email" className="e-term-input" {...email.inputProps} />
</Row>
<Row
label={t('era.terminalForm.labels.subject')}
dots=":"
error={subject.error}
errorId={subject.errorId}
>
<select className="e-term-box flex-[1_1_240px] min-w-0" {...subject.inputProps}>
<option value="" disabled>
{t('era.terminalForm.subjectPlaceholder')}
</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>
{t('era.terminalForm.labels.message')}
<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}>{t('era.terminalForm.honeypot')}</label>
<input type="text" id={hpId} {...hpProps} />
</div>
{/* Robot check (Friendly Captcha) when the server asks for it; client only, own chunk. */}
{form.captcha ? (
<div style={{ minHeight: 76 }}>
<Suspense fallback={null}>
<CaptchaWidget
captcha={form.captcha}
resetCount={form.captchaResetCount}
onToken={form.setCaptchaToken}
onProblem={setCaptchaProblem}
theme="dark"
/>
</Suspense>
</div>
) : null}
{captchaProblem ? (
<p role="alert" className="e-term-error m-0" data-testid="contact-captcha-problem">
{contactForm.captchaProblem}{' '}
<a href={`mailto:${contactInfo.email}`} className="underline">
{contactInfo.email}
</a>
</p>
) : null}
{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>
);
}