'use client' import { useRouter } from 'next/navigation' import { useActionState, useEffect, useState } from 'react' import { toast } from 'sonner' import type { SupporterFormState } from '@/app/(campaign)/campanha/(app)/apoiadores/novo/formActions' import { FormattedInput } from '@/components/FormattedInput' import type { RelationOption } from '@/components/campaign/RelationMultiSelect' import { StrictCombobox } from '@/components/campaign/StrictCombobox' 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 type { SupporterVoteIntention } from '@/lib/schemas/supporter' import { fieldError, errorProps as buildErrorProps } from '@/utilities/campaignFormFields' import { formatBrazilianPhoneInput, sanitizeBrazilianPhoneInput } from '@/utilities/phone' import { supporterVoteIntentionLabels } from '@/utilities/supporterUi' import { municipalityComboboxOptions } from '@/utilities/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, plazaOptions, registrationConsentConfigured, voteIntentionConsentConfigured, requirePlaza, }: { action: SupporterFormAction plazaOptions: RelationOption[] registrationConsentConfigured: boolean voteIntentionConsentConfigured: boolean requirePlaza: 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 useEffect(() => { if (state.status !== 'success') return toast.success(state.message) if (state.supporterId) { router.push(`/campanha/apoiadores/${state.supporterId}`, { scroll: false }) } }, [router, state.message, state.status, state.supporterId]) 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 plazaField = errorProps(state.fieldErrors, 'plaza') const voteIntentionField = errorProps(state.fieldErrors, 'voteIntention') return (
{(state.message || state.fieldErrors?.form) && state.status !== 'success' ? ( Não foi possível cadastrar {state.message ?

{state.message}

: null} {state.fieldErrors?.form?.map((error) => (

{error}

))}
) : 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} Praça{requirePlaza ? ' *' : ''} {!requirePlaza ? Sem Praça : null} {plazaOptions.map((plaza) => ( {plaza.name} ))} {plazaField.error ? ( {plazaField.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}
) }