mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-15 18:53:06 +00:00
Registra catálogo contextual no drawer B79 para /campanha/liderancas: lista com update-leadership; detalhe com cinco wizards e prefill de município quando N=1. Adiciona query leadershipId no wizard A4 e CampaignQuickActionContextSync na ficha. Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
210 lines
6.3 KiB
TypeScript
210 lines
6.3 KiB
TypeScript
/** Client-safe entry paths for UX-1 action wizards under `/campanha/acoes`. */
|
|
|
|
import {
|
|
parsePoliticalTrendStatusFormValue,
|
|
type PoliticalTrendStatusValue,
|
|
} from '@/lib/schemas/municipality'
|
|
import {
|
|
parseMunicipalitySignalType,
|
|
type MunicipalitySignalType,
|
|
} from '@/lib/schemas/municipalityUpdate'
|
|
|
|
export const CAMPAIGN_ACTIONS_HOME = '/campanha/acoes' as const
|
|
|
|
export type CampaignWizardActionId =
|
|
| 'update-votes'
|
|
| 'register-signal'
|
|
| 'change-trend'
|
|
| 'update-leadership'
|
|
| 'register-demand'
|
|
|
|
export const CAMPAIGN_WIZARD_ACTION_SLUGS: Record<CampaignWizardActionId, string> = {
|
|
'update-votes': 'atualizar-votos',
|
|
'register-signal': 'registrar-sinal',
|
|
'change-trend': 'mudar-tendencia',
|
|
'update-leadership': 'atualizar-lideranca',
|
|
'register-demand': 'registrar-pedido',
|
|
}
|
|
|
|
const wizardActionSlugs = new Set(Object.values(CAMPAIGN_WIZARD_ACTION_SLUGS))
|
|
|
|
const campaignWizardSlugToId = Object.fromEntries(
|
|
Object.entries(CAMPAIGN_WIZARD_ACTION_SLUGS).map(([id, actionSlug]) => [
|
|
actionSlug,
|
|
id as CampaignWizardActionId,
|
|
]),
|
|
) as Record<string, CampaignWizardActionId>
|
|
|
|
export const isCampaignWizardActionSlug = (slug: string): boolean => wizardActionSlugs.has(slug)
|
|
|
|
export const campaignWizardActionIdForSlug = (slug: string): CampaignWizardActionId | undefined =>
|
|
campaignWizardSlugToId[slug]
|
|
|
|
export const isCampaignWizardActionId = (id: string): id is CampaignWizardActionId =>
|
|
id in CAMPAIGN_WIZARD_ACTION_SLUGS
|
|
|
|
export const campaignActionEntryHref = (id: CampaignWizardActionId): string =>
|
|
`${CAMPAIGN_ACTIONS_HOME}/${CAMPAIGN_WIZARD_ACTION_SLUGS[id]}`
|
|
|
|
export const WIZARD_MUNICIPIO_QUERY_KEY = 'municipio' as const
|
|
|
|
/** Legacy B61 ritual param — canonical URLs omit it (B77). */
|
|
export const WIZARD_SCENARIO_QUERY_KEY = 'cenario' as const
|
|
|
|
export const WIZARD_SIGNAL_TYPE_QUERY_KEY = 'signalType' as const
|
|
|
|
export const WIZARD_ENTRY_ACTION_QUERY_KEY = 'entry' as const
|
|
|
|
export const WIZARD_TREND_STATUS_QUERY_KEY = 'trendStatus' as const
|
|
|
|
export const WIZARD_NOTE_PREFILL_QUERY_KEY = 'notePrefill' as const
|
|
|
|
export const WIZARD_SIGNAL_BODY_QUERY_KEY = 'signalBody' as const
|
|
|
|
export const WIZARD_VOTE_FROM_QUERY_KEY = 'voteFrom' as const
|
|
|
|
export const WIZARD_VOTE_TO_QUERY_KEY = 'voteTo' as const
|
|
|
|
export const WIZARD_LEADERSHIP_ID_QUERY_KEY = 'leadershipId' as const
|
|
|
|
export type WizardActionHrefOptions = {
|
|
entryAction?: CampaignWizardActionId
|
|
leadershipId?: number
|
|
}
|
|
|
|
export const wizardActionHref = (
|
|
actionSlug: string,
|
|
municipalitySlug?: string,
|
|
options?: WizardActionHrefOptions,
|
|
): string => {
|
|
const base = `${CAMPAIGN_ACTIONS_HOME}/${actionSlug}`
|
|
if (!municipalitySlug && !options?.leadershipId && !options?.entryAction) {
|
|
return base
|
|
}
|
|
const params = new URLSearchParams()
|
|
if (municipalitySlug) {
|
|
params.set(WIZARD_MUNICIPIO_QUERY_KEY, municipalitySlug)
|
|
}
|
|
if (options?.entryAction) {
|
|
params.set(WIZARD_ENTRY_ACTION_QUERY_KEY, options.entryAction)
|
|
}
|
|
if (options?.leadershipId !== undefined) {
|
|
params.set(WIZARD_LEADERSHIP_ID_QUERY_KEY, String(options.leadershipId))
|
|
}
|
|
return `${base}?${params.toString()}`
|
|
}
|
|
|
|
export const parseWizardMunicipioParam = (
|
|
value: string | string[] | undefined,
|
|
): string | undefined => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
return trimmed ? trimmed : undefined
|
|
}
|
|
|
|
export const parseWizardLeadershipIdParam = (
|
|
value: string | string[] | undefined,
|
|
): number | undefined => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed || !/^[1-9]\d*$/.test(trimmed)) {
|
|
return undefined
|
|
}
|
|
return Number(trimmed)
|
|
}
|
|
|
|
export const hasWizardScenarioParam = (value: string | string[] | undefined): boolean =>
|
|
parseWizardMunicipioParam(value) !== undefined
|
|
|
|
export const parseWizardEntryActionParam = (
|
|
value: string | string[] | undefined,
|
|
): CampaignWizardActionId | undefined => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed || !isCampaignWizardActionId(trimmed)) {
|
|
return undefined
|
|
}
|
|
return trimmed
|
|
}
|
|
|
|
export const wizardSignalHref = (
|
|
actionSlug: string,
|
|
municipalitySlug?: string,
|
|
signalType?: MunicipalitySignalType,
|
|
entryAction?: CampaignWizardActionId,
|
|
): string => {
|
|
const base = `${CAMPAIGN_ACTIONS_HOME}/${actionSlug}`
|
|
if (!municipalitySlug) {
|
|
return base
|
|
}
|
|
|
|
const params = new URLSearchParams({ [WIZARD_MUNICIPIO_QUERY_KEY]: municipalitySlug })
|
|
if (signalType) {
|
|
params.set(WIZARD_SIGNAL_TYPE_QUERY_KEY, signalType)
|
|
}
|
|
if (entryAction) {
|
|
params.set(WIZARD_ENTRY_ACTION_QUERY_KEY, entryAction)
|
|
}
|
|
return `${base}?${params.toString()}`
|
|
}
|
|
|
|
export const resolveWizardSignalTypeParam = (
|
|
value: string | string[] | undefined,
|
|
): { signalType?: MunicipalitySignalType; invalid: boolean } => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed) {
|
|
return { signalType: undefined, invalid: false }
|
|
}
|
|
|
|
const signalType = parseMunicipalitySignalType(trimmed)
|
|
if (signalType) {
|
|
return { signalType, invalid: false }
|
|
}
|
|
|
|
return { signalType: undefined, invalid: true }
|
|
}
|
|
|
|
export const wizardTrendHref = (
|
|
actionSlug: string,
|
|
municipalitySlug?: string,
|
|
trendStatus?: PoliticalTrendStatusValue,
|
|
entryAction?: CampaignWizardActionId,
|
|
extraParams?: Record<string, string>,
|
|
): string => {
|
|
const base = `${CAMPAIGN_ACTIONS_HOME}/${actionSlug}`
|
|
if (!municipalitySlug) {
|
|
return base
|
|
}
|
|
|
|
const params = new URLSearchParams({ [WIZARD_MUNICIPIO_QUERY_KEY]: municipalitySlug })
|
|
if (trendStatus) {
|
|
params.set(WIZARD_TREND_STATUS_QUERY_KEY, trendStatus)
|
|
}
|
|
if (entryAction) {
|
|
params.set(WIZARD_ENTRY_ACTION_QUERY_KEY, entryAction)
|
|
}
|
|
if (extraParams) {
|
|
for (const [key, value] of Object.entries(extraParams)) {
|
|
if (value) params.set(key, value)
|
|
}
|
|
}
|
|
return `${base}?${params.toString()}`
|
|
}
|
|
|
|
export const resolveWizardTrendStatusParam = (
|
|
value: string | string[] | undefined,
|
|
): { trendStatus?: PoliticalTrendStatusValue; invalid: boolean } => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed) {
|
|
return { trendStatus: undefined, invalid: false }
|
|
}
|
|
|
|
const trendStatus = parsePoliticalTrendStatusFormValue(trimmed)
|
|
if (trendStatus) {
|
|
return { trendStatus, invalid: false }
|
|
}
|
|
|
|
return { trendStatus: undefined, invalid: true }
|
|
}
|