'use client' import { useRouter } from 'next/navigation' import { useActionState, useState } from 'react' import type { SupporterFormState } from '@/app/(campaign)/campanha/(app)/apoiadores/novo/formActions' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import type { RelationOption } from '@/components/campaign/shared/RelationMultiSelect' import { StrictCombobox } from '@/components/campaign/shared/StrictCombobox' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' import { FormattedInput } from '@/components/FormattedInput' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel, } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Spinner } from '@/components/ui/Spinner' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/ToggleGroup' import { formatBrazilianPhoneInput, sanitizeBrazilianPhoneInput } from '@/lib/phone' import type { SupporterVoteIntention } from '@/lib/schemas/supporter' import { errorProps as buildErrorProps, fieldError } from '@/utilities/campaignFormFields' import { supporterVoteIntentionLabels } from '@/utilities/supporter/supporterUi' import { municipalityComboboxOptions } from '@/utilities/territory/territoryComboboxOptions' export type SupporterFormAction = ( state: SupporterFormState, formData: FormData, ) => Promise const errorProps = (fieldErrors: Record | undefined, field: string) => buildErrorProps(fieldErrors, field, 'supporter') const ConsentBlock = ({ id, name, title, description, configured, error, required = true, }: { id: string name: string title: string description: string configured: boolean error?: string required?: boolean }) => (

{title}

{description}

{!configured ? ( Consentimento não configurado O texto ainda não foi cadastrado no admin. Não é possível concluir o cadastro. ) : ( Li e confirmo o consentimento do titular * O operador atesta que o apoiador concordou com o uso dos dados conforme o texto versionado no admin. {error ? {error} : null} )}
) export const SupporterForm = ({ action, municipalityOptions, registrationConsentConfigured, voteIntentionConsentConfigured, requireMunicipality, }: { action: SupporterFormAction municipalityOptions: RelationOption[] registrationConsentConfigured: boolean voteIntentionConsentConfigured: boolean requireMunicipality: boolean }) => { const router = useRouter() const [state, formAction, pending] = useActionState(action, {}) const [voteIntention, setVoteIntention] = useState( (state.values?.voteIntention as SupporterVoteIntention | undefined) ?? '', ) const [city, setCity] = useState((state.values?.city as string | undefined) ?? '') const values = state.values useCampaignFormSuccessToast(state, () => { if (state.supporterId) { router.push(`/campanha/apoiadores/${state.supporterId}`, { scroll: false }) } }) const nameField = errorProps(state.fieldErrors, 'name') const phoneField = errorProps(state.fieldErrors, 'phone') const emailField = errorProps(state.fieldErrors, 'email') const cityField = errorProps(state.fieldErrors, 'city') const municipalityField = errorProps(state.fieldErrors, 'municipality') const voteIntentionField = errorProps(state.fieldErrors, 'voteIntention') return (
{state.status !== 'success' ? ( ) : null} Nome * {nameField.error ? ( {nameField.error} ) : null} Celular * {phoneField.error ? ( {phoneField.error} ) : null} E-mail {emailField.error ? ( {emailField.error} ) : null} Município {cityField.error ? ( {cityField.error} ) : null} Município{requireMunicipality ? ' *' : ''} {!requireMunicipality ? ( Sem município ) : null} {municipalityOptions.map((municipality) => ( {municipality.name} ))} {municipalityField.error ? ( {municipalityField.error} ) : null} Intenção de voto (opcional) setVoteIntention((value as SupporterVoteIntention) ?? '')} variant="outline" className="flex w-full flex-wrap" > {Object.entries(supporterVoteIntentionLabels).map(([value, label]) => ( {label} ))} {voteIntentionField.error ? ( {voteIntentionField.error} ) : null} {voteIntention ? ( ) : null}
) }