mirror of
https://github.com/fsolla/teqo.git
synced 2026-12-03 05:43:04 +00:00
Coluna "Dobradinhas" em /campanha/liderancas com chips clicáveis para /campanha/dobradinhas/<slug> e edição via Popover+Command (padrão B27, escrita via server action delta + lock, como B19). - LeadershipRowViewModel.stateDeputies (StateDeputySummary[]) via loadStateDeputySummaries, substituindo stateDeputyNames. - setLeadershipStateDeputyMembership(Record): delta com withPayloadTransaction + acquireTextAdvisoryLocks + revalidatePath. - nextStateDeputyIdsAfterMembership (lib/leadershipStateDeputyMembership.ts), espelha nextAdvisorIdsAfterMembership. - loadStateDeputyOptions estendido com plainName/party/slug. - Catálogo entregue uma vez por tabela via leadershipColumns(options) (columns factory, mesmo precedente de municipalityListColumns). /simplify (1ª rodada): corrige revert otimista por delta (não pelo snapshot inteiro), remove fallback silencioso na adição, extrai sameIdSet compartilhado com MunicipalityListAdvisorsControl (B27), e evita leitura+revalidação supérfluas no caminho no-op da action. /simplify (2ª rodada, pós-rebase): troca o StateDeputyOptionsProvider (Context) inicial pela columns factory acima — a 1ª rodada tinha validado o Context por falta da evidência de que MunicipalityList.tsx já resolve o mesmo problema sem Context; corrige revalidatePath para o id concreto em vez do template dinâmico '/campanha/liderancas/[id]' (que invalidava toda ficha a cada toggle); remove non-null assertion movendo a guarda para dentro do setCurrent updater. Gate verde: tsc, lint --max-warnings=0, format:check, unit 530/530, int 406/406 (incl. os 6 de campaignLeadershipStateDeputyMembership), build. Aikido: 0 achados nos arquivos novos/editados.
224 lines
7.7 KiB
TypeScript
224 lines
7.7 KiB
TypeScript
import config from '@payload-config'
|
|
import { MessageCircleIcon, PlusIcon, SearchXIcon } from 'lucide-react'
|
|
import Link from 'next/link'
|
|
import { redirect } from 'next/navigation'
|
|
import { getPayload } from 'payload'
|
|
|
|
import { LeadershipStateDeputiesCell } from '@/components/campaign/leadership/LeadershipStateDeputiesCell'
|
|
import { SupportStatusBadge } from '@/components/campaign/leadership/SupportStatusBadge'
|
|
import { CampaignCopyableCell } from '@/components/campaign/shared/CampaignCopyableCell'
|
|
import { CampaignListEmptyState } from '@/components/campaign/shared/CampaignListEmptyState'
|
|
import { CampaignListFooter } from '@/components/campaign/shared/CampaignListFooter'
|
|
import {
|
|
CampaignListPendingBoundary,
|
|
CampaignListResults,
|
|
} from '@/components/campaign/shared/CampaignListPending'
|
|
import { CampaignSearchForm } from '@/components/campaign/shared/CampaignSearchForm'
|
|
import {
|
|
CampaignTable,
|
|
CampaignTableHead,
|
|
type CampaignTableColumn,
|
|
} from '@/components/campaign/shared/CampaignTable'
|
|
import { CampaignPageShell } from '@/components/campaign/shell/CampaignPageShell'
|
|
import { Badge } from '@/components/ui/Badge'
|
|
import { Button } from '@/components/ui/button'
|
|
import { formatBrazilianPhoneInput, whatsAppHrefForPhone } from '@/lib/phone'
|
|
import { isCampaignStaff } from '@/utilities/campaignAccess'
|
|
import { getCampaignUser } from '@/utilities/campaignAuth'
|
|
import {
|
|
loadStateDeputyOptions,
|
|
type StateDeputyRelationOption,
|
|
} from '@/utilities/campaignRelationOptions'
|
|
import {
|
|
buildLeadershipListHref,
|
|
loadLeadershipListPageData,
|
|
parseLeadershipListParams,
|
|
type LeadershipRowViewModel,
|
|
} from '@/utilities/leadershipData'
|
|
|
|
import { setLeadershipStateDeputyMembershipFormAction } from './formActions'
|
|
|
|
type LeadershipsPageProps = {
|
|
searchParams: Promise<Record<string, string | string[] | undefined>>
|
|
}
|
|
|
|
const leadershipColumns = (
|
|
stateDeputyOptions: StateDeputyRelationOption[],
|
|
): Array<CampaignTableColumn<LeadershipRowViewModel>> => [
|
|
{
|
|
id: 'name',
|
|
mandatory: true,
|
|
head: <CampaignTableHead>Nome</CampaignTableHead>,
|
|
cell: (row) => (
|
|
<Link
|
|
href={`/campanha/liderancas/${row.id}`}
|
|
className="inline-flex min-h-11 items-center font-medium text-primary underline-offset-4 hover:underline"
|
|
>
|
|
{row.name}
|
|
</Link>
|
|
),
|
|
},
|
|
{
|
|
id: 'email',
|
|
head: <CampaignTableHead>E-mail</CampaignTableHead>,
|
|
cellClassName: 'max-w-56',
|
|
cell: (row) => <CampaignCopyableCell value={row.email} label="E-mail" />,
|
|
},
|
|
{
|
|
id: 'phone',
|
|
head: <CampaignTableHead>Celular</CampaignTableHead>,
|
|
cell: (row) => (
|
|
<CampaignCopyableCell
|
|
value={row.phone}
|
|
label="Celular"
|
|
formatDisplay={formatBrazilianPhoneInput}
|
|
className="tabular-nums"
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
id: 'supportStatus',
|
|
head: <CampaignTableHead>Status</CampaignTableHead>,
|
|
cell: (row) => (row.supportStatus ? <SupportStatusBadge status={row.supportStatus} /> : '—'),
|
|
},
|
|
{
|
|
id: 'municipalities',
|
|
head: <CampaignTableHead>Municípios</CampaignTableHead>,
|
|
cellClassName: 'max-w-64 whitespace-normal text-muted-foreground',
|
|
cell: (row) => row.municipalityNames.join(', ') || '—',
|
|
},
|
|
{
|
|
id: 'organizations',
|
|
head: <CampaignTableHead>Organizações</CampaignTableHead>,
|
|
cellClassName: 'max-w-56 whitespace-normal text-muted-foreground',
|
|
cell: (row) => row.organizationNames.join(', ') || '—',
|
|
},
|
|
{
|
|
id: 'stateDeputies',
|
|
head: <CampaignTableHead>Dobradinhas</CampaignTableHead>,
|
|
cellClassName: 'max-w-56 whitespace-normal',
|
|
cell: (row) => (
|
|
<LeadershipStateDeputiesCell
|
|
leadershipId={row.id}
|
|
stateDeputies={row.stateDeputies}
|
|
options={stateDeputyOptions}
|
|
membershipAction={setLeadershipStateDeputyMembershipFormAction}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
id: 'appAccess',
|
|
head: <CampaignTableHead>Acesso ao app</CampaignTableHead>,
|
|
cell: (row) => (
|
|
<Badge variant={row.hasAppAccess ? 'estimate-confirmed' : 'outline'}>
|
|
{row.hasAppAccess ? 'Com acesso' : 'Sem acesso'}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
id: 'actions',
|
|
head: (
|
|
<CampaignTableHead align="right">
|
|
<span className="sr-only">Ações</span>
|
|
</CampaignTableHead>
|
|
),
|
|
cellClassName: 'text-right',
|
|
cell: (row) => {
|
|
const whatsAppHref = whatsAppHrefForPhone(row.phone)
|
|
return whatsAppHref ? (
|
|
<Button asChild variant="ghost" size="icon" className="size-10">
|
|
<a
|
|
href={whatsAppHref}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
aria-label={`Enviar WhatsApp para ${row.name}`}
|
|
>
|
|
<MessageCircleIcon className="size-4" aria-hidden="true" />
|
|
</a>
|
|
</Button>
|
|
) : (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon"
|
|
className="size-10"
|
|
disabled
|
|
aria-label={`WhatsApp indisponível — ${row.name} sem celular`}
|
|
>
|
|
<MessageCircleIcon className="size-4" aria-hidden="true" />
|
|
</Button>
|
|
)
|
|
},
|
|
},
|
|
]
|
|
|
|
export default async function LeadershipsPage({ searchParams }: LeadershipsPageProps) {
|
|
const rawSearchParams = await searchParams
|
|
const [user, payload] = await Promise.all([getCampaignUser(), getPayload({ config })])
|
|
if (!user) redirect('/campanha/login')
|
|
if (!isCampaignStaff(user)) redirect('/campanha')
|
|
|
|
const state = parseLeadershipListParams(rawSearchParams)
|
|
const [{ rows, totalDocs, totalPages }, stateDeputyOptions] = await Promise.all([
|
|
loadLeadershipListPageData(payload, user, state),
|
|
loadStateDeputyOptions(payload, user),
|
|
])
|
|
const columns = leadershipColumns(stateDeputyOptions)
|
|
|
|
return (
|
|
<CampaignPageShell>
|
|
<header className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
|
|
<div className="flex flex-col gap-2">
|
|
<h1 className="text-2xl font-semibold tracking-tight">Lideranças</h1>
|
|
<p className="text-muted-foreground">
|
|
Uma ficha por pessoa — cada liderança pode atuar em vários municípios e organizações.
|
|
</p>
|
|
</div>
|
|
<Button asChild className="min-h-11">
|
|
<Link href="/campanha/liderancas/nova">
|
|
<PlusIcon data-icon="inline-start" aria-hidden="true" />
|
|
Nova liderança
|
|
</Link>
|
|
</Button>
|
|
</header>
|
|
|
|
<CampaignListPendingBoundary>
|
|
<CampaignSearchForm
|
|
ariaLabel="Buscar liderança por nome"
|
|
placeholder="Buscar por nome…"
|
|
initialQuery={state.q ?? ''}
|
|
basePath="/campanha/liderancas"
|
|
/>
|
|
|
|
<CampaignListResults>
|
|
{rows.length ? (
|
|
<>
|
|
<CampaignTable columns={columns} rows={rows} rowKey={(row) => row.id} />
|
|
<CampaignListFooter
|
|
totalDocs={totalDocs}
|
|
singular="liderança"
|
|
plural="lideranças"
|
|
page={state.page}
|
|
totalPages={totalPages}
|
|
hrefForPage={(page) => buildLeadershipListHref(state, page)}
|
|
/>
|
|
</>
|
|
) : (
|
|
<CampaignListEmptyState
|
|
icon={SearchXIcon}
|
|
title="Nenhuma liderança encontrada"
|
|
description="Cadastre a primeira liderança ou ajuste a busca. Você só vê lideranças dos seus municípios."
|
|
>
|
|
<Button asChild className="min-h-11">
|
|
<Link href="/campanha/liderancas/nova">
|
|
<PlusIcon data-icon="inline-start" aria-hidden="true" />
|
|
Nova liderança
|
|
</Link>
|
|
</Button>
|
|
</CampaignListEmptyState>
|
|
)}
|
|
</CampaignListResults>
|
|
</CampaignListPendingBoundary>
|
|
</CampaignPageShell>
|
|
)
|
|
}
|