'use client' import { startTransition, useState, type FormEvent } from 'react' import { PhonesFieldEditor } from '@/components/campaign/shared/PhonesFieldEditor' import { FieldError, FieldLabel } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { CitiesByState } from '@/lib/cities' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { errorProps } from '@/utilities/campaignFormFields' import { contactGenderLabels } from '@/utilities/contacts/contactListUrl' export type ContactFormDefaults = { name: string email: string phones: string[] gender: string state: string city: string postalCode: string } const stateOptions = Object.keys(CitiesByState) /** * C139 — the mobile sheet's ficha form (edit and create share it; the create * sheet passes empty defaults). Full-bleed fields with dividers; phones use * `PhonesFieldEditor` in FORM mode (no `saveAction`), so the repeated `phones` * inputs are collected by the enclosing form. Field errors come from the * ladder's `fieldErrors` (name/email/phones/gender/state/city/postalCode); * top-level ladder messages (name conflict, out of scope) render as a banner. * * The fields are CONTROLLED and the submit goes through a manual `onSubmit` * dispatch instead of `
`: React 19 resets * uncontrolled form fields after any settled form action, wiping typed values * on validation/conflict errors (the sheet stays open, so the wipe is visible). * `preventDefault` keeps the native submit event away from that plumbing. */ export const ContactFormFields = ({ formId, state, sessionId, submitAction, defaults, contactId, }: { formId: string state: CampaignFormActionState /** * Bumps on every sheet open; the fields drop the state from any PREVIOUS * session with it (the sheet wrapper stays mounted, so `useActionState` * state would otherwise leak a stale conflict banner into a fresh open). */ sessionId: number submitAction: (formData: FormData) => void defaults: ContactFormDefaults /** Edit mode: the record being updated (submitted as the `id` field). */ contactId?: string }) => { const [lastSessionId, setLastSessionId] = useState(sessionId) if (lastSessionId !== sessionId) setLastSessionId(sessionId) const visibleState = lastSessionId === sessionId ? state : {} const [name, setName] = useState(defaults.name) const [email, setEmail] = useState(defaults.email) const [gender, setGender] = useState(defaults.gender) const [stateValue, setStateValue] = useState(defaults.state) const [city, setCity] = useState(defaults.city) const [postalCode, setPostalCode] = useState(defaults.postalCode) const nameProps = errorProps(visibleState.fieldErrors, 'name', formId) const emailProps = errorProps(visibleState.fieldErrors, 'email', formId) const cityProps = errorProps(visibleState.fieldErrors, 'city', formId) const postalCodeProps = errorProps(visibleState.fieldErrors, 'postalCode', formId) const stateProps = errorProps(visibleState.fieldErrors, 'state', formId) const genderProps = errorProps(visibleState.fieldErrors, 'gender', formId) const handleSubmit = (event: FormEvent) => { event.preventDefault() // `startTransition` keeps `isPending` correct for the imperative dispatch // (React warns otherwise) without re-entering the `` plumbing. startTransition(() => submitAction(new FormData(event.currentTarget))) } return ( {contactId !== undefined ? : null} {visibleState.fieldErrors?.id ? ( {visibleState.fieldErrors.id[0]} ) : null} {visibleState.message ? (

{visibleState.message}

) : null}
Nome * setName(event.target.value)} placeholder="Nome e sobrenome" aria-invalid={nameProps.invalid} aria-describedby={nameProps.describedBy} /> {nameProps.error ? ( {nameProps.error} ) : null}
E-mail setEmail(event.target.value)} placeholder="nome@email.com" aria-invalid={emailProps.invalid} aria-describedby={emailProps.describedBy} /> {emailProps.error ? ( {emailProps.error} ) : null}
Gênero {genderProps.error ? ( {genderProps.error} ) : null}
Cidade setCity(event.target.value)} placeholder="Cidade" aria-invalid={cityProps.invalid} aria-describedby={cityProps.describedBy} /> {cityProps.error ? ( {cityProps.error} ) : null}
Estado * {stateProps.error ? ( {stateProps.error} ) : null}
CEP setPostalCode(event.target.value)} placeholder="00000000" aria-invalid={postalCodeProps.invalid} aria-describedby={postalCodeProps.describedBy} /> {postalCodeProps.error ? ( {postalCodeProps.error} ) : null}
) }