diff --git a/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts b/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts index e6695ed5..55d4682d 100644 --- a/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts +++ b/src/app/(campaign)/campanha/(app)/acoes/wizardLeadershipFormActions.ts @@ -10,7 +10,7 @@ import { LEADERSHIP_DUPLICATE_MESSAGE, LEADERSHIP_INVALID_CONTACT_MESSAGE, LEADERSHIP_MUNICIPALITY_SCOPE_MESSAGE, - leadershipSupportStatuses, + isSupportStatus, } from '@/lib/schemas/leadership' import { runCampaignFormAction, @@ -38,11 +38,7 @@ const parseExclusive = (formData: FormData): boolean | undefined => { const parseSupportStatus = (formData: FormData) => { const supportStatus = optionalFormText(formData, 'supportStatus') - return leadershipSupportStatuses.includes( - supportStatus as (typeof leadershipSupportStatuses)[number], - ) - ? (supportStatus as (typeof leadershipSupportStatuses)[number]) - : 'a_abordar' + return isSupportStatus(supportStatus) ? supportStatus : 'a_abordar' } const wizardFieldsFromForm = (formData: FormData) => ({ diff --git a/src/app/(campaign)/campanha/actions/leadership.ts b/src/app/(campaign)/campanha/actions/leadership.ts index 39e290cb..081cc521 100644 --- a/src/app/(campaign)/campanha/actions/leadership.ts +++ b/src/app/(campaign)/campanha/actions/leadership.ts @@ -363,16 +363,9 @@ export const createLeadershipWizard = async ( municipalitySlug: string, ) => { const { payload, actor } = await getCampaignActionContext() - try { - const leadership = await createLeadershipWizardRecord(payload, actor, input) - revalidateLeadershipWizardPaths(leadership.id, municipalitySlug) - return leadership - } catch (error) { - if (isUniqueLeadershipConflict(error)) { - throw new Error(LEADERSHIP_DUPLICATE_MESSAGE) - } - throw error - } + const leadership = await createLeadershipWizardRecord(payload, actor, input) + revalidateLeadershipWizardPaths(leadership.id, municipalitySlug) + return leadership } /** diff --git a/src/components/campaign/leadership/WizardLeadershipStep.tsx b/src/components/campaign/leadership/WizardLeadershipStep.tsx index 9bc9f46c..c350c722 100644 --- a/src/components/campaign/leadership/WizardLeadershipStep.tsx +++ b/src/components/campaign/leadership/WizardLeadershipStep.tsx @@ -3,7 +3,7 @@ import { InfoIcon, PlusIcon } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/navigation' -import { useCallback, useMemo, useState, useTransition } from 'react' +import { useCallback, useState, useTransition } from 'react' import { toast } from 'sonner' import { SupportStatusBadge } from '@/components/campaign/leadership/SupportStatusBadge' @@ -76,7 +76,7 @@ export const WizardLeadershipStep = ({ const showSkip = showLeadershipWizardSkip(entryAction) - const skip = showSkip + const skipConfig = showSkip ? { label: WIZARD_LEADERSHIP_SKIP_LABEL, href: CAMPAIGN_HOME } : undefined @@ -87,9 +87,9 @@ export const WizardLeadershipStep = ({ ? WIZARD_LEADERSHIP_FORM_EDIT_TITLE : WIZARD_LEADERSHIP_FORM_CREATE_TITLE - const trailingAction = showSkip ? ( + const trailingAction = skipConfig ? ( ) : undefined @@ -106,11 +106,6 @@ export const WizardLeadershipStep = ({ }) } - const sortedTiles = useMemo( - () => [...initialTiles].sort((left, right) => left.name.localeCompare(right.name, 'pt-BR')), - [initialTiles], - ) - return ( {mode.kind === 'form' ? ( @@ -133,11 +128,11 @@ export const WizardLeadershipStep = ({ ) : (
- {sortedTiles.length === 0 ? ( + {initialTiles.length === 0 ? (

{WIZARD_LEADERSHIP_EMPTY_GRID}

) : null}
    - {sortedTiles.map((tile) => ( + {initialTiles.map((tile) => (
  • - -
-
-

- {truncateNameAtWordBoundary(tile.name, 28)} -

- {secondary ? ( -

{secondary}

- ) : null} - {!tile.exclusive ? ( - - Não exclusivo - - ) : null} -
- - - - {noteText} - - +
+

+ {truncateNameAtWordBoundary(tile.name, 28)} +

+ {secondary ? ( +

{secondary}

+ ) : null} + {!tile.exclusive ? ( + + Não exclusivo + + ) : null} +
+ + + + {noteText} + + + + ) } diff --git a/src/lib/wizardLeadershipContract.ts b/src/lib/wizardLeadershipContract.ts index e3caa971..e1958a62 100644 --- a/src/lib/wizardLeadershipContract.ts +++ b/src/lib/wizardLeadershipContract.ts @@ -4,7 +4,6 @@ import type { SupportStatus } from '@/lib/schemas/leadership' /** Client-safe tile payload for the B70 leadership wizard grid. */ export type WizardLeadershipTileViewModel = { id: number - contactID: number name: string phone: string | null email: string | null diff --git a/src/utilities/leadership/leadershipData.ts b/src/utilities/leadership/leadershipData.ts index 8bb973b1..f3aa5c95 100644 --- a/src/utilities/leadership/leadershipData.ts +++ b/src/utilities/leadership/leadershipData.ts @@ -54,6 +54,38 @@ const contactSummary = ( return { id: Number(contact), name: 'Contato', phone: null, email: null } } +const findMunicipalityLeadershipDocs = async ( + payload: Payload, + user: CampaignUser, + municipalityID: number, + sort = 'createdAt', +): Promise => { + const result = await payload.find({ + collection: 'leadership', + where: { municipalities: { in: [municipalityID] } }, + depth: 1, + limit: 0, + pagination: false, + sort, + user, + overrideAccess: false, + }) + return result.docs as Leadership[] +} + +const toWizardLeadershipTile = (doc: Leadership): WizardLeadershipTileViewModel => { + const contact = contactSummary(doc.contact) + return { + id: doc.id, + name: contact.name, + phone: contact.phone, + email: contact.email, + supportStatus: isSupportStatus(doc.supportStatus) ? doc.supportStatus : null, + exclusive: doc.exclusive ?? true, + notes: doc.notes ?? null, + } +} + const organizationNamesForIds = (payload: Payload, ids: number[]): Promise> => loadOrganizationNamesByIds(payload, ids) @@ -113,17 +145,8 @@ export const loadMunicipalityLeaderships = async ( user: CampaignUser, municipalityID: number, ): Promise => { - const result = await payload.find({ - collection: 'leadership', - where: { municipalities: { in: [municipalityID] } }, - depth: 1, - limit: 0, - pagination: false, - sort: 'createdAt', - user, - overrideAccess: false, - }) - return toLeadershipRows(payload, result.docs as Leadership[]) + const docs = await findMunicipalityLeadershipDocs(payload, user, municipalityID) + return toLeadershipRows(payload, docs) } export const loadWizardLeadershipTiles = async ( @@ -131,32 +154,10 @@ export const loadWizardLeadershipTiles = async ( user: CampaignUser, municipalityID: number, ): Promise => { - const result = await payload.find({ - collection: 'leadership', - where: { municipalities: { in: [municipalityID] } }, - depth: 1, - limit: 0, - pagination: false, - sort: 'createdAt', - user, - overrideAccess: false, - }) - - const rows = await toLeadershipRows(payload, result.docs as Leadership[]) - const notesById = new Map( - (result.docs as Leadership[]).map((doc) => [doc.id, doc.notes ?? null] as const), - ) - - return rows.map((row) => ({ - id: row.id, - contactID: row.contactID, - name: row.name, - phone: row.phone, - email: row.email, - supportStatus: row.supportStatus, - exclusive: row.exclusive, - notes: notesById.get(row.id) ?? null, - })) + const docs = await findMunicipalityLeadershipDocs(payload, user, municipalityID) + return docs + .map(toWizardLeadershipTile) + .sort((left, right) => left.name.localeCompare(right.name, 'pt-BR')) } /**