'use client' import type { MunicipalityListLeadershipsResponse } from '@/app/(campaign)/campanha/(app)/municipios/leaderships/types' import { useMunicipalityLeadershipCreate } from '@/components/campaign/municipality/MunicipalityLeadershipCreateProvider' import type { CampaignCellEditOverlayVariant } from '@/components/campaign/shared/CampaignCellEditOverlay' import type { MunicipalityRelationEntry } from '@/components/campaign/shared/MunicipalityRelationAvatarStack' import { MunicipalityRelationEditor, type MunicipalityRelationMutationResult, type MunicipalityRelationTriggerProps, } from '@/components/campaign/shared/MunicipalityRelationEditor' import { postCampaignJson } from '@/lib/campaignJsonRequest' import type { EligibleLeadershipOption, MunicipalityLeadershipSummary, } from '@/utilities/municipality/municipalityViewModels' const LEADERSHIPS_ENDPOINT = '/campanha/municipios/leaderships' const SAVE_ERROR_MESSAGE = 'Não foi possível atualizar as lideranças. Tente novamente.' type MunicipalityListLeadershipsControlProps = MunicipalityRelationTriggerProps & { municipalityID: number municipalityName: string currentLeadershipIDs: number[] leadershipNamesById: ReadonlyMap options: EligibleLeadershipOption[] variant: CampaignCellEditOverlayVariant /** C142 — read-only presentation (advisor with Edição `somente_leitura`). */ readOnly?: boolean } const entryOf = (option: { id: number; name: string }): MunicipalityRelationEntry => ({ id: option.id, label: option.name, }) export const MunicipalityListLeadershipsControl = ({ municipalityID, municipalityName, currentLeadershipIDs, leadershipNamesById, options, variant, trigger, triggerClassName, readOnly = false, }: MunicipalityListLeadershipsControlProps) => { const createBridge = useMunicipalityLeadershipCreate() const createdOptions = createBridge?.createdOptions ?? [] const toggle = async ( leadershipID: number, assigned: boolean, ): Promise => { const { ok, payload } = await postCampaignJson( LEADERSHIPS_ENDPOINT, { municipalityId: municipalityID, leadershipId: leadershipID, assigned }, ) if (!ok || payload.status !== 'success') { return { status: 'error', message: payload.status === 'error' ? payload.message : SAVE_ERROR_MESSAGE, } } return { status: 'success', selectedIDs: payload.leadershipIDs } } const create = async (name: string): Promise => { const { ok, payload } = await postCampaignJson( LEADERSHIPS_ENDPOINT, { municipalityId: municipalityID, name }, ) if (!ok || payload.status !== 'success' || !payload.createdLeadership) { return { status: 'error', message: payload.status === 'error' ? payload.message : SAVE_ERROR_MESSAGE, } } return { status: 'success', selectedIDs: payload.leadershipIDs, createdEntry: entryOf(payload.createdLeadership), } } return ( `Editar lideranças em ${municipalityName} — ${ entries.length ? entries.map((entry) => entry.label).join(', ') : 'nenhuma' }` } emptyState={Nenhuma} trigger={trigger} triggerClassName={triggerClassName} createLabel={(name) => `Criar liderança “${name}”`} readOnly={readOnly} onToggle={toggle} onCreate={create} onCreated={(entry) => createBridge?.registerCreatedLeadership({ id: entry.id, name: entry.label }) } /> ) }