'use client' import { startTransition, useActionState, useEffect, useState, type FormEvent } from 'react' import { searchDemandResponsibleOptions } from '@/app/(campaign)/campanha/(app)/demandas/responsibleSearchActions' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { Button } from '@/components/ui/button' import { Spinner } from '@/components/ui/Spinner' import { CAMPAIGN_DEMAND_RESPONSIBLES_LABEL } from '@/lib/schemas/campaignDemand' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { DemandResponsibleMultiSelect, type DemandResponsibleOption, } from './DemandResponsibleMultiSelect' type DemandResponsiblesCardProps = { demandId: number municipalityId: number | null /** The creator id, shown as "(criador)" on the chip (removal stays allowed — hand-off). */ creatorUserId: number | null initialResponsibles: DemandResponsibleOption[] formAction: ( state: CampaignFormActionState, formData: FormData, ) => Promise /** C142 — read-only presentation (advisor with Edição `somente_leitura`): the responsibles render as chips with no editor. */ readOnly?: boolean } /** * C143 — responsible management on the demand detail. Everyone who can read * the demand can update it (same row scope), so the card's edit controls are * available to whoever opens the page; the save submits the full list, the * action revalidates the page and the local state keeps the saved chips * (the success message is the confirmation, so no remount key is used). */ export const DemandResponsiblesCard = ({ demandId, municipalityId, creatorUserId, initialResponsibles, formAction, readOnly = false, }: DemandResponsiblesCardProps) => { const [state, submitAction, isPending] = useActionState(formAction, {}) const [dirty, setDirty] = useState(false) useEffect(() => { if (state.status === 'success') setDirty(false) }, [state.status]) const search = (query: string) => searchDemandResponsibleOptions(query, municipalityId) if (readOnly) { return (

{CAMPAIGN_DEMAND_RESPONSIBLES_LABEL}

Só quem é responsável vê esta demanda.

{initialResponsibles.length > 0 ? ( ) : (

Sem responsáveis.

)}
) } // C140 — manual dispatch (no `action={submitAction}`): React 19 resets // uncontrolled fields after any settled form action, wiping typed values // on a validation error — the card stays open, so the wipe showed. const handleSubmit = (event: FormEvent) => { event.preventDefault() startTransition(() => submitAction(new FormData(event.currentTarget))) } return (

{CAMPAIGN_DEMAND_RESPONSIBLES_LABEL}

Só quem é responsável vê esta demanda.

setDirty(true)} />

O criador já entra como responsável automaticamente. Assessores do município aparecem como sugestão, mas só quem for marcado enxerga.

) }