'use client' import Link from 'next/link' import { useState, useTransition } from 'react' import { MUNICIPALITY_EXPECTED_VOTES_ENDPOINT, type MunicipalityListExpectedVotesResponse, } from '@/app/(campaign)/campanha/(app)/municipios/expected-votes/types' 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 { postCampaignJson } from '@/lib/campaignJsonRequest' import { CAMPAIGN_HOME } from '@/lib/campaignPaths' import { WIZARD_VOTES_NEXT_FLOW_PLACEHOLDER, WIZARD_VOTES_SAVE_ERROR_MESSAGE, WIZARD_VOTES_SAVED_MESSAGE, wizardFlowTitleForSlug, wizardNextStepTitle, } from '@/lib/campaignWizardCopy' import { type VoteEstimateScenario, type VoteEstimateScenarioViewModel } from '@/lib/voteEstimate' import { applyVoteShortcut, getWizardVoteViolation, getWizardVoteViolationHighlights, VOTE_SHORTCUTS, voteShortcutLabels, wizardVoteFinalCtaLabel, type VoteShortcut, } from '@/lib/wizardVoteEstimate' type WizardExpectedVotesStepProps = { actionSlug: string municipalityId: number municipalityName: string municipalitySlug: string initialExpectedVotes: VoteEstimateScenarioViewModel } export const WizardExpectedVotesStep = ({ actionSlug, municipalityId, municipalityName, municipalitySlug: _municipalitySlug, initialExpectedVotes, }: WizardExpectedVotesStepProps) => { const [isPending, startTransition] = useTransition() const [estimates, setEstimates] = useState(initialExpectedVotes) const [focusedScenario, setFocusedScenario] = useState('central') const [violationMessage, setViolationMessage] = useState(null) const [highlightScenarios, setHighlightScenarios] = useState>( new Set(), ) const [saveError, setSaveError] = useState(null) const [phase, setPhase] = useState<'editing' | 'saved'>('editing') const clearViolationState = () => { setViolationMessage(null) setHighlightScenarios(new Set()) } 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 violation = getWizardVoteViolation(estimates) if (violation) { setViolationMessage(violation.message) setHighlightScenarios(new Set(getWizardVoteViolationHighlights(estimates))) 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 : WIZARD_VOTES_SAVE_ERROR_MESSAGE) return } setEstimates(payload.savedExpectedVotes) setPhase('saved') }) } if (phase === 'saved') { return (

{WIZARD_VOTES_SAVED_MESSAGE}

{WIZARD_VOTES_NEXT_FLOW_PLACEHOLDER}

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