'use client' import { useState, useTransition } from 'react' import { MUNICIPALITY_EXPECTED_VOTES_ENDPOINT, MUNICIPALITY_EXPECTED_VOTES_SAVE_ERROR_MESSAGE, type MunicipalityListExpectedVotesResponse, } from '@/app/(campaign)/campanha/(app)/municipios/expected-votes/types' import { CampaignWizardNavLink } from '@/components/campaign/shared/CampaignWizardNavLink' import { CampaignWizardShell } from '@/components/campaign/shared/CampaignWizardShell' import { VoteEstimateScenarioInputs } from '@/components/campaign/votePledge/VoteEstimateScenarioInputs' import { Alert, AlertDescription } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Spinner } from '@/components/ui/Spinner' import { wizardActionHref } from '@/lib/campaignActionRoutes' import { recordLastActedMunicipality } from '@/lib/campaignLastActedMunicipality' import { postCampaignJson } from '@/lib/campaignJsonRequest' import { CAMPAIGN_HOME } from '@/lib/campaignPaths' import { WIZARD_VOTES_FINAL_CTA_LABEL, WIZARD_VOTES_SAVED_MESSAGE, wizardFlowTitleForSlug, wizardNextStepPlaceholder, wizardNextStepTitle, } from '@/lib/campaignWizardCopy' import { type VoteEstimateScenario, type VoteEstimateScenarioViewModel } from '@/lib/voteEstimate' import { applyVoteShortcut, getWizardVoteViolation, VOTE_SHORTCUTS, voteShortcutLabels, type VoteShortcut, type WizardVoteViolation, } from '@/lib/wizardVoteEstimate' type WizardExpectedVotesStepProps = { actionSlug: string municipalityId: number municipalityName: string municipalitySlug: string initialExpectedVotes: VoteEstimateScenarioViewModel } export const WizardExpectedVotesStep = ({ actionSlug, municipalityId, municipalityName, municipalitySlug, initialExpectedVotes, }: WizardExpectedVotesStepProps) => { const [isPending, startTransition] = useTransition() const [estimates, setEstimates] = useState(initialExpectedVotes) const [focusedScenario, setFocusedScenario] = useState('central') const [violation, setViolation] = useState(null) const [saveError, setSaveError] = useState(null) const [phase, setPhase] = useState<'editing' | 'saved'>('editing') const clearViolationState = () => { if (violation) setViolation(null) } const handleValuesChange = (next: VoteEstimateScenarioViewModel) => { setEstimates(next) clearViolationState() setSaveError(null) } const handleShortcut = (shortcut: VoteShortcut) => { const nextValue = applyVoteShortcut(estimates[focusedScenario], shortcut) handleValuesChange({ ...estimates, [focusedScenario]: nextValue }) } const handleConfirm = () => { if (isPending) return const nextViolation = getWizardVoteViolation(estimates) if (nextViolation) { setViolation(nextViolation) setSaveError(null) return } clearViolationState() startTransition(async () => { const { ok, payload } = await postCampaignJson( MUNICIPALITY_EXPECTED_VOTES_ENDPOINT, { municipalityId, expectedVotes: estimates, }, ) if (!ok || payload.status !== 'success') { setSaveError( payload.status === 'error' ? payload.message : MUNICIPALITY_EXPECTED_VOTES_SAVE_ERROR_MESSAGE, ) return } setEstimates(payload.savedExpectedVotes) recordLastActedMunicipality(municipalitySlug) setPhase('saved') }) } if (phase === 'saved') { return (

{WIZARD_VOTES_SAVED_MESSAGE}

{wizardNextStepPlaceholder(actionSlug)}

) } return (
{VOTE_SHORTCUTS.map((shortcut) => ( ))}
{violation ? ( {violation.message} ) : null} {saveError ? ( {saveError} ) : null}
{isPending ? 'Salvando votos estimados.' : null}
) }