teqo-backup/src/components/campaign/votePledge/DeclareVotesForm.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

68 lines
2.5 KiB
TypeScript

'use client'
import { useActionState } from 'react'
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
import { Button } from '@/components/ui/button'
import { Field, FieldError, FieldLabel } from '@/components/ui/field'
import { Input } from '@/components/ui/input'
import { Spinner } from '@/components/ui/Spinner'
import type { CampaignFormActionState } from '@/utilities/campaignFormActionError'
import { fieldError } from '@/utilities/campaignFormFields'
type DeclareVotesFormProps = {
municipalityID: number
/** Present when staff declare on behalf of a leadership. */
leadershipID?: number
currentDeclaredVotes: number | null
formAction: (
state: CampaignFormActionState,
formData: FormData,
) => Promise<CampaignFormActionState>
}
export const DeclareVotesForm = ({
municipalityID,
leadershipID,
currentDeclaredVotes,
formAction,
}: DeclareVotesFormProps) => {
const [state, submitAction, isPending] = useActionState(formAction, {})
return (
<form action={submitAction} className="flex flex-col gap-3">
<input type="hidden" name="municipalityId" value={municipalityID} />
{leadershipID !== undefined ? (
<input type="hidden" name="leadershipId" value={leadershipID} />
) : null}
<Field>
<FieldLabel htmlFor={`declare-votes-${municipalityID}-${leadershipID ?? 'own'}`}>
{leadershipID !== undefined
? 'Quantos votos a liderança traz neste município?'
: 'Quantos votos você está trazendo neste município?'}
</FieldLabel>
<div className="flex gap-2">
<Input
id={`declare-votes-${municipalityID}-${leadershipID ?? 'own'}`}
name="declaredVotes"
type="number"
min={0}
max={1000000}
required
inputMode="numeric"
defaultValue={currentDeclaredVotes ?? undefined}
className="min-h-11 w-36"
/>
<Button type="submit" disabled={isPending} className="min-h-11">
{isPending ? <Spinner data-icon="inline-start" aria-hidden="true" /> : null}
{currentDeclaredVotes == null ? 'Declarar' : 'Atualizar'}
</Button>
</div>
{fieldError(state.fieldErrors, 'declaredVotes') ? (
<FieldError>{fieldError(state.fieldErrors, 'declaredVotes')}</FieldError>
) : null}
</Field>
<CampaignFormActionMessage state={state} successFallbackMessage="Declaração registrada." />
</form>
)
}