'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 { 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 { formatBrazilianPhoneInput, sanitizeBrazilianPhoneInput } from '@/utilities/phone' import { supporterVoteIntentionLabels } from '@/utilities/supporterUi' import type { SupporterNucleusOption } from '@/utilities/supporterViewModels' export type SupporterFormAction = ( state: SupporterFormState, formData: FormData, ) => Promise const fieldError = ( fieldErrors: Record | undefined, field: string, ): string | undefined => fieldErrors?.[field]?.[0] const errorProps = (fieldErrors: Record | undefined, field: string) => { const error = fieldError(fieldErrors, field) return { error, invalid: Boolean(error), describedBy: error ? `supporter-${field}-error` : undefined, } } 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, nucleusOptions, registrationConsentConfigured, voteIntentionConsentConfigured, requireNucleus, }: { action: SupporterFormAction nucleusOptions: SupporterNucleusOption[] registrationConsentConfigured: boolean voteIntentionConsentConfigured: boolean requireNucleus: boolean }) => { const router = useRouter() const [state, formAction, pending] = useActionState(action, {}) const [voteIntention, setVoteIntention] = useState( (state.values?.voteIntention as SupporterVoteIntention | 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 nucleusField = errorProps(state.fieldErrors, 'nucleus') 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} Núcleo{requireNucleus ? ' *' : ''} {!requireNucleus ? Sem núcleo : null} {nucleusOptions.map((nucleus) => ( {nucleus.name} ))} {nucleusField.error ? ( {nucleusField.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}
) }