'use client' import { PlusIcon, Trash2Icon } from 'lucide-react' import { useRouter } from 'next/navigation' import { useActionState, useEffect, useState } from 'react' import { toast } from 'sonner' import { updateNucleusIntelligenceFormAction, type NucleusIntelligenceFormState, } from '@/app/(campaign)/campanha/(app)/nucleos/[slug]/nucleusIntelligenceFormActions' import { PrimaryContactCombobox } from '@/components/campaign/PrimaryContactCombobox' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSet, } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { Spinner } from '@/components/ui/Spinner' import { Textarea } from '@/components/ui/textarea' import { fieldError } from '@/utilities/campaignFormFields' import { normalizedVoterProfilesForWrite } from '@/utilities/nucleusIntelligenceUi' import { nucleusDobradinhaNotesDescription, nucleusDobradinhaNotesLabel, nucleusNextStepsDescription, nucleusNextStepsLabel, } from '@/utilities/nucleusUi' import type { StaffNucleusTabsViewModel } from '@/utilities/nucleusViewModels' import type { NucleusPrimaryContactPageData, PrimaryContactOption, } from '@/utilities/primaryContactPageData' type EditableProfile = StaffNucleusTabsViewModel['voterProfiles'][number] type NucleusIntelligenceDialogProps = { action?: typeof updateNucleusIntelligenceFormAction nucleusId: number intelligence: Pick< StaffNucleusTabsViewModel, 'strengths' | 'risks' | 'voterProfiles' | 'ticketAlliance' | 'dobradinhaNotes' | 'nextSteps' > primaryContact: PrimaryContactOption | null searchPrimaryContacts: (query: string) => Promise onClose?: () => void onPendingChange?: (pending: boolean) => void } const emptyProfile = (): EditableProfile => ({ label: '', ageRange: null, incomeBand: null, occupation: null, localTraits: null, notes: null, }) const normalizedInsights = (values: string[]) => values.map((text) => ({ text: text.trim() })).filter(({ text }) => Boolean(text)) const FormFeedback = ({ state }: { state: NucleusIntelligenceFormState }) => { if (!state.message || state.status === 'success') return null return ( Não foi possível salvar {state.message} ) } export const NucleusIntelligenceDialog = ({ action: updateAction = updateNucleusIntelligenceFormAction, nucleusId, intelligence, primaryContact, searchPrimaryContacts, onClose, onPendingChange, }: NucleusIntelligenceDialogProps) => { const router = useRouter() const [state, action, pending] = useActionState(updateAction, {}) const [strengths, setStrengths] = useState( intelligence.strengths.length ? intelligence.strengths.map(({ text }) => text) : [''], ) const [risks, setRisks] = useState( intelligence.risks.length ? intelligence.risks.map(({ text }) => text) : [''], ) const [profiles, setProfiles] = useState( intelligence.voterProfiles.length ? intelligence.voterProfiles : [emptyProfile()], ) useEffect(() => { if (state.status !== 'success') return toast.success(state.message) router.refresh() onClose?.() }, [onClose, router, state]) useEffect(() => { onPendingChange?.(pending) }, [onPendingChange, pending]) const primaryContactError = fieldError(state.fieldErrors, 'primaryContact') const updateProfile = (index: number, field: keyof EditableProfile, value: string) => { setProfiles((current) => current.map((profile, profileIndex) => profileIndex === index ? { ...profile, [field]: value || null } : profile, ), ) } return ( { if (pending) event.preventDefault() }} onPointerDownOutside={(event) => { if (pending) event.preventDefault() }} > Editar inteligência do núcleo Registre a leitura política e o contato de referência deste território.
Pontos fortes Capacidades que ajudam a mobilização no núcleo. {strengths.map((strength, index) => ( Ponto forte {index + 1} setStrengths((current) => current.map((item, itemIndex) => itemIndex === index ? event.target.value : item, ), ) } /> ))}
Riscos Gargalos que podem comprometer a mobilização. {risks.map((risk, index) => ( Risco {index + 1} setRisks((current) => current.map((item, itemIndex) => itemIndex === index ? event.target.value : item, ), ) } /> ))}
Perfis do eleitorado {profiles.map((profile, index) => (
Perfil {index + 1}
Nome do perfil * updateProfile(index, 'label', event.target.value)} />
Faixa etária updateProfile(index, 'ageRange', event.target.value)} /> Faixa de renda updateProfile(index, 'incomeBand', event.target.value)} /> Ocupação updateProfile(index, 'occupation', event.target.value)} />
Características locais