teqo/src/components/campaign/supporter/VoteIntentionControl.tsx
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

123 lines
4.7 KiB
TypeScript

'use client'
import { useActionState, useState } from 'react'
import type { SupporterVoteIntentionFormState } from '@/app/(campaign)/campanha/(app)/apoiadores/[id]/formActions'
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast'
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/supporter/supporterUi'
export type VoteIntentionControlAction = (
state: SupporterVoteIntentionFormState,
formData: FormData,
) => Promise<SupporterVoteIntentionFormState>
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<SupporterVoteIntention | ''>(currentValue ?? '')
const canEdit =
hasVoteIntentionConsent || (localConsentAccepted && voteIntentionConsentConfigured)
useCampaignFormSuccessToast(state, () => {
if (state.voteIntention) setSelected(state.voteIntention)
})
return (
<section className="rounded-[6px] border bg-card p-4" aria-labelledby="vote-intention-title">
<h2 id="vote-intention-title" className="font-medium">
Intenção de voto
</h2>
<p className="mt-1 text-sm text-muted-foreground">
Dado sensível — disponível somente após confirmação do consentimento LGPD.
</p>
{!hasVoteIntentionConsent ? (
voteIntentionConsentConfigured ? (
<Field orientation="horizontal" className="mt-4">
<Checkbox
id="vote-intention-consent-accepted"
checked={localConsentAccepted}
onCheckedChange={(checked) => setLocalConsentAccepted(checked === true)}
/>
<FieldContent>
<FieldLabel htmlFor="vote-intention-consent-accepted">
Confirmo o consentimento destacado para registrar intenção de voto
</FieldLabel>
<FieldDescription>
Marque somente se o titular autorizou expressamente este tratamento.
</FieldDescription>
</FieldContent>
</Field>
) : (
<Alert variant="destructive" className="mt-4">
<AlertTitle>Consentimento não configurado</AlertTitle>
<AlertDescription>
O texto de intenção de voto ainda não foi cadastrado no admin.
</AlertDescription>
</Alert>
)
) : null}
<form action={formAction} className="mt-4 flex flex-col gap-4">
<input type="hidden" name="id" value={supporterId} />
<input type="hidden" name="voteIntention" value={selected} />
{hasVoteIntentionConsent || localConsentAccepted ? (
<input type="hidden" name="voteIntentionConsentAccepted" value="true" />
) : null}
<ToggleGroup
type="single"
value={selected}
onValueChange={(value) => setSelected((value as SupporterVoteIntention) ?? '')}
variant="outline"
className="flex w-full flex-wrap"
disabled={!canEdit || pending}
>
{Object.entries(supporterVoteIntentionLabels).map(([value, label]) => (
<ToggleGroupItem key={value} value={value} className="min-h-11 flex-1">
{label}
</ToggleGroupItem>
))}
</ToggleGroup>
{!canEdit ? (
<p className="text-sm text-muted-foreground">
Disponível somente após confirmação do consentimento LGPD.
</p>
) : null}
{state.status !== 'success' ? <CampaignFormActionMessage state={state} /> : null}
<Button
type="submit"
className="min-h-11 w-fit"
disabled={!canEdit || !selected || pending}
>
{pending ? <Spinner data-icon="inline-start" /> : null}
{pending ? 'Salvando…' : 'Salvar intenção'}
</Button>
</form>
</section>
)
}