'use client' import { useActionState, useEffect, useState } from 'react' import { toast } from 'sonner' import type { SupporterVoteIntentionFormState } from '@/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' import { Field, FieldContent, FieldDescription, FieldLabel, } from '@/components/ui/field' import { Spinner } from '@/components/ui/Spinner' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/ToggleGroup' import type { SupporterVoteIntention } from '@/lib/schemas/supporter' import { supporterVoteIntentionLabels } from '@/utilities/supporterUi' export type VoteIntentionControlAction = ( state: SupporterVoteIntentionFormState, formData: FormData, ) => Promise export const VoteIntentionControl = ({ supporterId, currentValue, hasVoteIntentionConsent, voteIntentionConsentConfigured, action, }: { supporterId: number currentValue: SupporterVoteIntention | null hasVoteIntentionConsent: boolean voteIntentionConsentConfigured: boolean action: VoteIntentionControlAction }) => { const [state, formAction, pending] = useActionState(action, {}) const [localConsentAccepted, setLocalConsentAccepted] = useState(false) const [selected, setSelected] = useState(currentValue ?? '') const canEdit = hasVoteIntentionConsent || (localConsentAccepted && voteIntentionConsentConfigured) useEffect(() => { if (state.status !== 'success') return toast.success(state.message) if (state.voteIntention) setSelected(state.voteIntention) }, [state.message, state.status, state.voteIntention]) return (

Intenção de voto

Dado sensível — disponível somente após confirmação do consentimento LGPD.

{!hasVoteIntentionConsent ? ( voteIntentionConsentConfigured ? ( setLocalConsentAccepted(checked === true)} /> Confirmo o consentimento destacado para registrar intenção de voto Marque somente se o titular autorizou expressamente este tratamento. ) : ( Consentimento não configurado O texto de intenção de voto ainda não foi cadastrado no admin. ) ) : null}
{hasVoteIntentionConsent || localConsentAccepted ? ( ) : null} setSelected((value as SupporterVoteIntention) ?? '')} variant="outline" className="flex w-full flex-wrap" disabled={!canEdit || pending} > {Object.entries(supporterVoteIntentionLabels).map(([value, label]) => ( {label} ))} {!canEdit ? (

Disponível somente após confirmação do consentimento LGPD.

) : null} {state.message && state.status !== 'success' ? ( {state.message} ) : null}
) }