'use client' import { startTransition, useActionState, useEffect, useMemo, useState, type FormEvent, } from 'react' import { createCampaignUpdatesFormAction } from '@/app/(campaign)/campanha/(app)/atualizacoes/createFormActions' import { MunicipalityUpdateFields } from '@/components/campaign/municipality/MunicipalityUpdateFields' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { StrictCombobox } from '@/components/campaign/shared/StrictCombobox' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/components/ui/dialog' import { Drawer, DrawerContent, DrawerDescription, DrawerTitle } from '@/components/ui/Drawer' import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field' import { Spinner } from '@/components/ui/Spinner' import { useIsMobile } from '@/hooks/use-mobile' import { getMunicipalityCatalogEntry } from '@/lib/municipalityCatalog' import { cn } from '@/lib/utils' import { fieldError } from '@/utilities/campaignFormFields' import type { CampaignUpdatesFeedFacets } from '@/utilities/municipality/campaignUpdatesFeedData' type CampaignUpdatesCreateModalProps = { open: boolean onOpenChange: (open: boolean) => void municipalities: CampaignUpdatesFeedFacets['municipalities'] isStaff: boolean /** Municipality slug to prefill when exactly one is active in the filter. */ prefillSlug?: string } const MUNICIPALITY_COMBOBOX_ID = 'campaign-updates-create-municipality' /** * The modal/sheet create surface for the updates feed (C89). One component * owns `useActionState` and renders the `
`, mirroring the proven * dialog-action pattern (ActivityLifecycleDialog) — the server action needs to * be bound where the form mounts for progressive enhancement to attach. */ export const CampaignUpdatesCreateModal = ({ open, onOpenChange, municipalities, isStaff, prefillSlug, }: CampaignUpdatesCreateModalProps) => { const isMobile = useIsMobile() const [state, submitAction, isPending] = useActionState(createCampaignUpdatesFormAction, {}) const options = useMemo( () => municipalities.map(({ id, slug }) => ({ value: String(id), label: getMunicipalityCatalogEntry(slug)?.name ?? slug, })), [municipalities], ) const initialMunicipalityId = useMemo(() => { if (!prefillSlug) return '' const match = municipalities.find((municipality) => municipality.slug === prefillSlug) return match ? String(match.id) : '' }, [municipalities, prefillSlug]) const [municipalityId, setMunicipalityId] = useState(initialMunicipalityId) // The modal stays mounted (sibling of the filters form), so the municipality // must re-sync to the active filter's prefill on every open — otherwise a // stale selection would be silently submitted. useEffect(() => { if (open) setMunicipalityId(initialMunicipalityId) }, [open, initialMunicipalityId]) useCampaignFormSuccessToast(state, () => onOpenChange(false)) const municipalityError = fieldError(state.fieldErrors, 'municipalityId') // C140 — manual dispatch (no `action={submitAction}`): React 19 resets // uncontrolled fields after any settled form action, wiping typed values // on a validation error — the modal stays open, so the wipe showed. const handleSubmit = (event: FormEvent) => { event.preventDefault() startTransition(() => submitAction(new FormData(event.currentTarget))) } // Mobile sheet: list-style form — no visible labels/borders, full-bleed // divider rows, descriptive placeholders (C107). The label stays for the // accessible name; the sheet auto-sizes to the content with its ceiling at // the top of the screen (`max-h-dvh`), scrolling internally only when the // content outgrows the viewport (e.g. open keyboard). Desktop keeps the // labeled dialog chrome. const createForm = ( {isMobile ? ( <>
Município {municipalityError ? ( {municipalityError} ) : null}
) : ( Município O registro fica vinculado a um município da sua carteira. {municipalityError ? ( {municipalityError} ) : null} )} {state.status !== 'success' && state.message ? (
) : null}
) if (isMobile) { return ( Nova atualização Registre um novo fato de campo em um município
{createForm}
) } return ( Nova atualização Registre um novo fato de campo em um município. {createForm} ) }