import { useEffect, useRef, useState, useReducer } from 'react'; import { Box, chakra, Field, Input, Link, NativeSelect, SimpleGrid, Stack, Text, Textarea, useRecipe, } from '@chakra-ui/react'; import PrivacyNote from '@/components/PrivacyNote/PrivacyNote'; import { submitContactForm } from '@/contact/submitContactForm'; import { CONTACT_LIMITS, type ContactField, type ContactPayload, type ShootOption, } from '@/contact/types'; import type { EmailAddress } from '@/content/types'; import { contactFormReducer, INITIAL_CONTACT_STATE, type ContactFormEvent, } from '@/lib/contactFormReducer'; import { emailAddress } from '@/lib/emailAddress'; import { mailtoLink } from '@/lib/mailLink'; import { toContactPayload } from '@/lib/toContactPayload'; import { CONTACT_FIELDS } from '@/lib/validateContact'; type ContactFormProps = { // Prefix for the field ids, so the form can appear on more than one page idPrefix: string; options: ShootOption[]; // A package chosen via "Boek ..."; key changes on every click, so the // same package can be applied again after the visitor changed it preselect?: { shoot: string; key: number }; mailSubject: string; email: EmailAddress; // How soon Andrea answers, e.g. '1-2 werkdagen' (siteContent) replyTime: string; submit?: (payload: ContactPayload) => Promise; }; // The contact form (design .form): name and e-mail, the shoot, the question, // and the browser-side anti-spam. Its behaviour is the pure // contactFormReducer; this component wires it to the fields. export default function ContactForm({ idPrefix, options, preselect, mailSubject, email, replyTime, submit = submitContactForm, }: ContactFormProps) { const reduce = ( state: typeof INITIAL_CONTACT_STATE, event: ContactFormEvent, ) => contactFormReducer(state, event, options); const [state, dispatch] = useReducer(reduce, INITIAL_CONTACT_STATE); const formControl = useRecipe({ key: 'formControl' }); const linkButton = useRecipe({ key: 'linkButton' }); // When the form became usable, for the time check (BR-SPAM-01) const [shownAt] = useState(() => Date.now()); const honeypot = useRef(null); const sentMessage = useRef(null); // Apply a new "Boek ..." choice while rendering (React's pattern for // reacting to a changed prop), so it never shows a stale value first const [seenPreselect, setSeenPreselect] = useState(0); if (preselect && preselect.key !== seenPreselect) { setSeenPreselect(preselect.key); dispatch({ type: 'preselect', shoot: preselect.shoot }); } // After a stopped submit, focus the first field with an error useEffect(() => { if (state.invalidSubmits === 0) return; const first = CONTACT_FIELDS.find((field) => state.errors[field]); if (first) document.getElementById(`${idPrefix}-${first}`)?.focus(); // Only on a new stopped submit, not on every error change // eslint-disable-next-line react-hooks/exhaustive-deps }, [state.invalidSubmits]); // The thank-you replaces the form; move focus there (BR-SEND-02) useEffect(() => { if (state.status === 'sent') sentMessage.current?.focus(); }, [state.status]); const address = emailAddress(email); const disabled = state.status === 'sending'; if (state.status === 'sent') { return ( {`Bedankt voor je bericht! Ik reageer binnen ${replyTime}.`} ); } const onSubmit = (event: React.FormEvent) => { event.preventDefault(); const submitEvent: ContactFormEvent = { type: 'submit', honeypot: honeypot.current?.value ?? '', shownAt, now: Date.now(), }; // Sending starts here, not in an effect, so it happens exactly once const next = reduce(state, submitEvent); dispatch(submitEvent); if (next.status === 'sending') { submit(toContactPayload(state.values)).then( () => dispatch({ type: 'resolved' }), () => dispatch({ type: 'rejected' }), ); } }; // The control's id comes from Field.Root's id (Ark uses it for the // control and the label's htmlFor), so label and error stay linked const fieldProps = (field: ContactField) => ({ name: field, value: state.values[field], onChange: ( event: React.ChangeEvent< HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement >, ) => dispatch({ type: 'edit', field, value: event.target.value }), onBlur: () => dispatch({ type: 'blur', field }), css: formControl(), 'data-testid': `contact-form-${field}`, }); const label = (text: string) => ( // ink.soft instead of the design's muted grey: meets 4.5:1 {text} ); const error = (field: ContactField) => ( {state.errors[field]} ); return ( {label('Naam')} {error('naam')} {label('E-mail')} {error('email')} {label('In welke shoot heb je interesse?')} {options.map((option) => ( ))} {error('shoot')} {label('Stel hier je vraag')}