teqo-backup/src/components/campaign/municipality/WizardTrendNoteStep.tsx

109 lines
3.9 KiB
TypeScript

'use client'
import { useRouter } from 'next/navigation'
import { useActionState, useState } from 'react'
import { setMunicipalityPoliticalTrendFormAction } from '@/app/(campaign)/campanha/(app)/municipios/municipalityStaffFormActions'
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
import { CampaignWizardShell } from '@/components/campaign/shared/CampaignWizardShell'
import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast'
import { WizardStepFormChrome } from '@/components/campaign/shared/WizardStepFormChrome'
import { Button } from '@/components/ui/button'
import { Textarea } from '@/components/ui/textarea'
import { wizardPreviousHref, wizardReturnHref } from '@/lib/campaignActionRoutes'
import { recordLastActedMunicipality } from '@/lib/campaignLastActedMunicipality'
import { wizardFlowTitleForSlug } from '@/lib/campaignWizardCopy'
import { WIZARD_TREND_CLEAR_LABEL, WIZARD_TREND_SAVE_LABEL } from '@/lib/politicalTrendWizardUi'
import type { PoliticalTrendStatusValue } from '@/lib/schemas/municipality'
import { politicalTrendLabels } from '@/utilities/municipality/municipalityLabels'
type WizardTrendNoteStepProps = {
actionSlug: string
municipalityId: number
municipalityName: string
municipalitySlug: string
trendStatus: PoliticalTrendStatusValue
returnPath?: string
}
export const WizardTrendNoteStep = ({
actionSlug,
municipalityId,
municipalityName,
municipalitySlug,
trendStatus,
returnPath,
}: WizardTrendNoteStepProps) => {
const router = useRouter()
const [note, setNote] = useState('')
const [state, submitAction, isPending] = useActionState(
setMunicipalityPoliticalTrendFormAction,
{},
)
const stepTitle = `Mudar tendência para ${politicalTrendLabels[trendStatus]}`
useCampaignFormSuccessToast(state, () => {
recordLastActedMunicipality(municipalitySlug)
router.push(wizardReturnHref(returnPath))
})
return (
<CampaignWizardShell
flowTitle={wizardFlowTitleForSlug(actionSlug)}
isEntryStep={false}
stepTitle={stepTitle}
previousHref={wizardPreviousHref({
actionSlug,
stepKind: 'trend-note',
municipalitySlug,
returnPath,
})}
dismissHref={wizardReturnHref(returnPath)}
municipalityLabel={municipalityName}
contentFocus="none"
>
<WizardStepFormChrome
action={submitAction}
isPending={isPending}
pendingAnnouncement="Salvando tendência."
ctaLabel={WIZARD_TREND_SAVE_LABEL}
leadingSubmit={
<Button
type="button"
variant="outline"
disabled={isPending || note.length === 0}
className="min-h-11"
onClick={() => setNote('')}
>
{WIZARD_TREND_CLEAR_LABEL}
</Button>
}
submitBarClassName="flex-wrap gap-3"
>
<input type="hidden" name="municipalityId" value={municipalityId} />
<input type="hidden" name="municipalitySlug" value={municipalitySlug} />
<input type="hidden" name="trendStatus" value={trendStatus} />
<div className="flex flex-col gap-2">
<label htmlFor="wizard-trend-note" className="text-sm font-medium">
Por que mudar a tendência? (opcional)
</label>
<Textarea
id="wizard-trend-note"
name="trendNote"
value={note}
onChange={(event) => setNote(event.target.value)}
rows={5}
maxLength={2000}
disabled={isPending}
autoFocus
className="min-h-28"
placeholder="Descreva o que mudou na leitura política do município."
/>
</div>
{state.status !== 'success' ? <CampaignFormActionMessage state={state} /> : null}
</WizardStepFormChrome>
</CampaignWizardShell>
)
}