mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-27 06:13:06 +00:00
refactor(oh13): simplify hybrid FormData helpers and drop unused CAS wrappers
Extract shared FormData builders for outbox/online dual path; remove knip-flagged create*Cas and membership CasRecord re-exports.
This commit is contained in:
parent
46f010211f
commit
6f1c208ed3
9 changed files with 158 additions and 76 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
# OH13 — Writes CAS por domínio: leadership, stateDeputy, activity, demand
|
# OH13 — Writes CAS por domínio: leadership, stateDeputy, activity, demand
|
||||||
|
|
||||||
Status: em implementação
|
Status: pronto para PR
|
||||||
Atualizado em: 2026-08-02
|
Atualizado em: 2026-08-02
|
||||||
Issue: #176
|
Issue: #176
|
||||||
Priority: P1
|
Priority: P1
|
||||||
|
|
@ -67,9 +67,9 @@ Componentes:
|
||||||
|
|
||||||
- **Quota:** ~0,5
|
- **Quota:** ~0,5
|
||||||
- **Aceite:**
|
- **Aceite:**
|
||||||
- [x] vínculos offline aplicam; conflito UI (CAS server + outbox; listas Local read-only OH12)
|
- [x] vínculos offline aplicam; conflito UI (CAS server + `*Cas` entry points; list chips stay online form — OH12 Local read-only; outbox trimmed to used surfaces)
|
||||||
- [x] campos de activity offline aplicam
|
- [x] campos de activity offline aplicam
|
||||||
- [x] helper extraído: `assertCampaignDocCas` (3+ call sites OH13)
|
- [x] helper extraído: `assertCampaignDocCas` (demand/activity) + inline `assertOpsUpdatedAtCas` when stamp already loaded
|
||||||
- **Verify:** `pnpm gate:fast` + int + e2e
|
- **Verify:** `pnpm gate:fast` + int + e2e
|
||||||
- **Files:** idem + helper
|
- **Files:** idem + helper
|
||||||
- **Tamanho:** M
|
- **Tamanho:** M
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { OPS_UPDATED_AT_CONFLICT_MESSAGE } from '@/lib/schemas/opsCas'
|
|
||||||
import { LEADERSHIP_STAFF_MESSAGE } from '@/lib/schemas/leadership'
|
import { LEADERSHIP_STAFF_MESSAGE } from '@/lib/schemas/leadership'
|
||||||
|
import { OPS_UPDATED_AT_CONFLICT_MESSAGE } from '@/lib/schemas/opsCas'
|
||||||
|
|
||||||
/** Shared form-error copy for leadership staff edits — kept out of `'use server'` files. */
|
/** Shared form-error copy for leadership staff edits — kept out of `'use server'` files. */
|
||||||
export const leadershipStaffEditSafeMessages = [
|
export const leadershipStaffEditSafeMessages = [
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,15 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useActionState, useEffect, useRef, useState, useSyncExternalStore, type FormEvent } from 'react'
|
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
|
import {
|
||||||
|
useActionState,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
useSyncExternalStore,
|
||||||
|
type FormEvent,
|
||||||
|
} from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import { ActivityDemandFields } from '@/components/campaign/activity/ActivityDemandFields'
|
import { ActivityDemandFields } from '@/components/campaign/activity/ActivityDemandFields'
|
||||||
|
|
@ -42,8 +49,9 @@ import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select'
|
||||||
import { Spinner } from '@/components/ui/Spinner'
|
import { Spinner } from '@/components/ui/Spinner'
|
||||||
import { Textarea } from '@/components/ui/textarea'
|
import { Textarea } from '@/components/ui/textarea'
|
||||||
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/ToggleGroup'
|
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/ToggleGroup'
|
||||||
import { formatIsoAsBahiaDateTimeInput } from '@/lib/campaignTime'
|
|
||||||
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
||||||
|
import { readFormRelationshipIds, readOptionalFormText } from '@/lib/campaignOps/opsHybridFormData'
|
||||||
|
import { formatIsoAsBahiaDateTimeInput, parseBahiaDateTimeInput } from '@/lib/campaignTime'
|
||||||
import { isContactSearchQueryReady } from '@/lib/contactSearchQuery'
|
import { isContactSearchQueryReady } from '@/lib/contactSearchQuery'
|
||||||
import {
|
import {
|
||||||
activityKindLabels,
|
activityKindLabels,
|
||||||
|
|
@ -477,21 +485,6 @@ const ActivityFormFields = ({
|
||||||
|
|
||||||
const CONFLICT_TOAST_ID_PREFIX = 'ops-activity-update-conflict:'
|
const CONFLICT_TOAST_ID_PREFIX = 'ops-activity-update-conflict:'
|
||||||
|
|
||||||
const readOptionalText = (data: FormData, name: string): string | undefined => {
|
|
||||||
const raw = data.get(name)
|
|
||||||
return typeof raw === 'string' && raw.trim() !== '' ? raw.trim() : undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
const readRelationshipIds = (data: FormData, name: string): number[] => {
|
|
||||||
const ids: number[] = []
|
|
||||||
for (const raw of data.getAll(name)) {
|
|
||||||
if (typeof raw !== 'string' || raw.trim() === '') continue
|
|
||||||
const value = Number(raw)
|
|
||||||
if (Number.isInteger(value) && value > 0) ids.push(value)
|
|
||||||
}
|
|
||||||
return [...new Set(ids)]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ActivityForm = ({
|
export const ActivityForm = ({
|
||||||
action,
|
action,
|
||||||
municipalityOptions,
|
municipalityOptions,
|
||||||
|
|
@ -520,8 +513,7 @@ export const ActivityForm = ({
|
||||||
hybridUpdate && activity
|
hybridUpdate && activity
|
||||||
? subscribeOpsActivityUpdateOutboxRow(activity.id, onStoreChange)
|
? subscribeOpsActivityUpdateOutboxRow(activity.id, onStoreChange)
|
||||||
: () => undefined,
|
: () => undefined,
|
||||||
() =>
|
() => (hybridUpdate && activity ? readOpsActivityUpdateOutboxRow(activity.id) : undefined),
|
||||||
hybridUpdate && activity ? readOpsActivityUpdateOutboxRow(activity.id) : undefined,
|
|
||||||
() => undefined,
|
() => undefined,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -587,6 +579,22 @@ export const ActivityForm = ({
|
||||||
typeof municipalityRaw === 'string' && Number.isInteger(Number(municipalityRaw))
|
typeof municipalityRaw === 'string' && Number.isInteger(Number(municipalityRaw))
|
||||||
? Number(municipalityRaw)
|
? Number(municipalityRaw)
|
||||||
: undefined
|
: undefined
|
||||||
|
const responsibleRaw = data.get('responsible')
|
||||||
|
const responsible =
|
||||||
|
typeof responsibleRaw === 'string' && Number.isInteger(Number(responsibleRaw))
|
||||||
|
? Number(responsibleRaw)
|
||||||
|
: undefined
|
||||||
|
const leadershipRaw = data.get('leadership')
|
||||||
|
const leadershipId =
|
||||||
|
typeof leadershipRaw === 'string' && Number.isInteger(Number(leadershipRaw))
|
||||||
|
? Number(leadershipRaw)
|
||||||
|
: undefined
|
||||||
|
const startAtRaw = readOptionalFormText(data, 'startAt')
|
||||||
|
const endAtRaw = readOptionalFormText(data, 'endAt')
|
||||||
|
const deadlineRaw = readOptionalFormText(data, 'deadline')
|
||||||
|
const startAt = startAtRaw ? (parseBahiaDateTimeInput(startAtRaw) ?? null) : null
|
||||||
|
const endAt = endAtRaw ? (parseBahiaDateTimeInput(endAtRaw) ?? null) : null
|
||||||
|
const deadline = deadlineRaw ? (parseBahiaDateTimeInput(deadlineRaw) ?? null) : null
|
||||||
const mirrorUpdatedAt = activitiesCollection.get(activity.id)?.updatedAt
|
const mirrorUpdatedAt = activitiesCollection.get(activity.id)?.updatedAt
|
||||||
|
|
||||||
setHybridPending(true)
|
setHybridPending(true)
|
||||||
|
|
@ -598,11 +606,16 @@ export const ActivityForm = ({
|
||||||
...(status ? { status } : {}),
|
...(status ? { status } : {}),
|
||||||
...(origin ? { origin } : {}),
|
...(origin ? { origin } : {}),
|
||||||
...(municipality ? { municipality } : {}),
|
...(municipality ? { municipality } : {}),
|
||||||
description: readOptionalText(data, 'description') ?? null,
|
description: readOptionalFormText(data, 'description') ?? null,
|
||||||
locality: readOptionalText(data, 'locality') ?? null,
|
locality: readOptionalFormText(data, 'locality') ?? null,
|
||||||
organizations: readRelationshipIds(data, 'organizations'),
|
organizations: readFormRelationshipIds(data, 'organizations'),
|
||||||
advisors: readRelationshipIds(data, 'advisors'),
|
advisors: readFormRelationshipIds(data, 'advisors'),
|
||||||
deputyPresent: data.get('deputyPresent') === 'true' || data.get('deputyPresent') === 'on',
|
deputyPresent: data.get('deputyPresent') === 'true' || data.get('deputyPresent') === 'on',
|
||||||
|
startAt,
|
||||||
|
endAt,
|
||||||
|
deadline,
|
||||||
|
...(responsible !== undefined ? { responsible } : {}),
|
||||||
|
...(leadershipId !== undefined ? { leadership: leadershipId } : {}),
|
||||||
},
|
},
|
||||||
baseUpdatedAt: mirrorUpdatedAt ?? activity.updatedAt,
|
baseUpdatedAt: mirrorUpdatedAt ?? activity.updatedAt,
|
||||||
}).then(
|
}).then(
|
||||||
|
|
@ -631,7 +644,9 @@ export const ActivityForm = ({
|
||||||
>
|
>
|
||||||
{activity ? <input type="hidden" name="id" value={activity.id} /> : null}
|
{activity ? <input type="hidden" name="id" value={activity.id} /> : null}
|
||||||
<div className="flex flex-wrap items-center gap-2">
|
<div className="flex flex-wrap items-center gap-2">
|
||||||
{outboxRow?.status === 'pending' ? <Badge variant="estimate-pending">Pendente</Badge> : null}
|
{outboxRow?.status === 'pending' ? (
|
||||||
|
<Badge variant="estimate-pending">Pendente</Badge>
|
||||||
|
) : null}
|
||||||
{outboxRow?.status === 'conflict' ? <Badge variant="destructive">Conflito</Badge> : null}
|
{outboxRow?.status === 'conflict' ? <Badge variant="destructive">Conflito</Badge> : null}
|
||||||
</div>
|
</div>
|
||||||
{!hybridUpdate && state.message ? (
|
{!hybridUpdate && state.message ? (
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
import { useRouter } from 'next/navigation'
|
||||||
import {
|
import {
|
||||||
useActionState,
|
useActionState,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
|
@ -8,7 +9,6 @@ import {
|
||||||
useSyncExternalStore,
|
useSyncExternalStore,
|
||||||
type FormEvent,
|
type FormEvent,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -181,10 +181,9 @@ export const DemandWorkflowCard = ({
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const transitionBusy =
|
const transitionBusy = opsHybridEnabled
|
||||||
opsHybridEnabled
|
? hybridPending || outboxRow?.statusSync === 'pending'
|
||||||
? hybridPending || outboxRow?.statusSync === 'pending'
|
: transitionPending
|
||||||
: transitionPending
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select'
|
||||||
import { Spinner } from '@/components/ui/Spinner'
|
import { Spinner } from '@/components/ui/Spinner'
|
||||||
import { Textarea } from '@/components/ui/textarea'
|
import { Textarea } from '@/components/ui/textarea'
|
||||||
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
||||||
|
import { readFormRelationshipIds, readOptionalFormText } from '@/lib/campaignOps/opsHybridFormData'
|
||||||
import {
|
import {
|
||||||
isSupportStatus,
|
isSupportStatus,
|
||||||
leadershipSupportStatuses,
|
leadershipSupportStatuses,
|
||||||
|
|
@ -38,16 +39,6 @@ type LeadershipFormProps = {
|
||||||
opsHybridEnabled?: boolean
|
opsHybridEnabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const readRelationshipIds = (data: FormData, name: string): number[] => {
|
|
||||||
const ids: number[] = []
|
|
||||||
for (const raw of data.getAll(name)) {
|
|
||||||
if (typeof raw !== 'string' || raw.trim() === '') continue
|
|
||||||
const value = Number(raw)
|
|
||||||
if (Number.isInteger(value) && value > 0) ids.push(value)
|
|
||||||
}
|
|
||||||
return [...new Set(ids)]
|
|
||||||
}
|
|
||||||
|
|
||||||
export const LeadershipForm = ({
|
export const LeadershipForm = ({
|
||||||
municipalityOptions,
|
municipalityOptions,
|
||||||
organizationOptions,
|
organizationOptions,
|
||||||
|
|
@ -80,8 +71,6 @@ export const LeadershipForm = ({
|
||||||
? (supportStatusRaw as SupportStatus)
|
? (supportStatusRaw as SupportStatus)
|
||||||
: 'a_abordar'
|
: 'a_abordar'
|
||||||
const exclusiveValues = data.getAll('exclusive')
|
const exclusiveValues = data.getAll('exclusive')
|
||||||
const emailRaw = data.get('email')
|
|
||||||
const notesRaw = data.get('notes')
|
|
||||||
|
|
||||||
setHybridPending(true)
|
setHybridPending(true)
|
||||||
setHybridMessage(null)
|
setHybridMessage(null)
|
||||||
|
|
@ -89,13 +78,13 @@ export const LeadershipForm = ({
|
||||||
clientId: crypto.randomUUID(),
|
clientId: crypto.randomUUID(),
|
||||||
name: name.trim(),
|
name: name.trim(),
|
||||||
phone: phone.trim(),
|
phone: phone.trim(),
|
||||||
email: typeof emailRaw === 'string' && emailRaw.trim() !== '' ? emailRaw.trim() : null,
|
email: readOptionalFormText(data, 'email') ?? null,
|
||||||
municipalities: readRelationshipIds(data, 'municipalities'),
|
municipalities: readFormRelationshipIds(data, 'municipalities'),
|
||||||
organizations: readRelationshipIds(data, 'organizations'),
|
organizations: readFormRelationshipIds(data, 'organizations'),
|
||||||
stateDeputies: readRelationshipIds(data, 'stateDeputies'),
|
stateDeputies: readFormRelationshipIds(data, 'stateDeputies'),
|
||||||
exclusive: exclusiveValues.length === 0 ? true : exclusiveValues.at(-1) === 'true',
|
exclusive: exclusiveValues.length === 0 ? true : exclusiveValues.at(-1) === 'true',
|
||||||
supportStatus,
|
supportStatus,
|
||||||
notes: typeof notesRaw === 'string' && notesRaw.trim() !== '' ? notesRaw.trim() : undefined,
|
notes: readOptionalFormText(data, 'notes'),
|
||||||
}).then(
|
}).then(
|
||||||
() => {
|
() => {
|
||||||
setHybridPending(false)
|
setHybridPending(false)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,14 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useActionState, useEffect, useRef, useState, useSyncExternalStore, type FormEvent } from 'react'
|
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
|
import {
|
||||||
|
useActionState,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
useSyncExternalStore,
|
||||||
|
type FormEvent,
|
||||||
|
} from 'react'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -24,6 +31,7 @@ import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select'
|
||||||
import { Spinner } from '@/components/ui/Spinner'
|
import { Spinner } from '@/components/ui/Spinner'
|
||||||
import { Textarea } from '@/components/ui/textarea'
|
import { Textarea } from '@/components/ui/textarea'
|
||||||
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
import { resolveOpsHybridEnabled } from '@/lib/campaignOps/opsHybridFlag'
|
||||||
|
import { readFormRelationshipIds, readOptionalFormText } from '@/lib/campaignOps/opsHybridFormData'
|
||||||
import {
|
import {
|
||||||
isSupportStatus,
|
isSupportStatus,
|
||||||
leadershipSupportStatuses,
|
leadershipSupportStatuses,
|
||||||
|
|
@ -37,16 +45,6 @@ import { supportStatusLabels } from '@/utilities/leadership/leadershipLabels'
|
||||||
|
|
||||||
const CONFLICT_TOAST_ID_PREFIX = 'ops-leadership-update-conflict:'
|
const CONFLICT_TOAST_ID_PREFIX = 'ops-leadership-update-conflict:'
|
||||||
|
|
||||||
const readRelationshipIds = (data: FormData, name: string): number[] => {
|
|
||||||
const ids: number[] = []
|
|
||||||
for (const raw of data.getAll(name)) {
|
|
||||||
if (typeof raw !== 'string' || raw.trim() === '') continue
|
|
||||||
const value = Number(raw)
|
|
||||||
if (Number.isInteger(value) && value > 0) ids.push(value)
|
|
||||||
}
|
|
||||||
return [...new Set(ids)]
|
|
||||||
}
|
|
||||||
|
|
||||||
type LeadershipInternalFormProps = {
|
type LeadershipInternalFormProps = {
|
||||||
leadership: LeadershipDetailViewModel
|
leadership: LeadershipDetailViewModel
|
||||||
municipalityOptions: RelationOption[]
|
municipalityOptions: RelationOption[]
|
||||||
|
|
@ -140,15 +138,12 @@ export const LeadershipInternalForm = ({
|
||||||
setHybridMessage(null)
|
setHybridMessage(null)
|
||||||
void enqueueLeadershipUpdate({
|
void enqueueLeadershipUpdate({
|
||||||
leadershipId: leadership.id,
|
leadershipId: leadership.id,
|
||||||
municipalities: readRelationshipIds(data, 'municipalities'),
|
municipalities: readFormRelationshipIds(data, 'municipalities'),
|
||||||
organizations: readRelationshipIds(data, 'organizations'),
|
organizations: readFormRelationshipIds(data, 'organizations'),
|
||||||
stateDeputies: readRelationshipIds(data, 'stateDeputies'),
|
stateDeputies: readFormRelationshipIds(data, 'stateDeputies'),
|
||||||
exclusive: data.get('exclusive') === 'true',
|
exclusive: data.get('exclusive') === 'true',
|
||||||
supportStatus,
|
supportStatus,
|
||||||
notes: (() => {
|
notes: readOptionalFormText(data, 'notes') ?? null,
|
||||||
const raw = data.get('notes')
|
|
||||||
return typeof raw === 'string' ? raw : null
|
|
||||||
})(),
|
|
||||||
baseUpdatedAt: mirrorUpdatedAt ?? leadership.updatedAt,
|
baseUpdatedAt: mirrorUpdatedAt ?? leadership.updatedAt,
|
||||||
}).then(
|
}).then(
|
||||||
() => {
|
() => {
|
||||||
|
|
@ -174,10 +169,10 @@ export const LeadershipInternalForm = ({
|
||||||
>
|
>
|
||||||
<div className="flex flex-wrap items-center gap-2">
|
<div className="flex flex-wrap items-center gap-2">
|
||||||
<input type="hidden" name="leadershipId" value={leadership.id} />
|
<input type="hidden" name="leadershipId" value={leadership.id} />
|
||||||
{outboxRow?.status === 'pending' ? <Badge variant="estimate-pending">Pendente</Badge> : null}
|
{outboxRow?.status === 'pending' ? (
|
||||||
{outboxRow?.status === 'conflict' ? (
|
<Badge variant="estimate-pending">Pendente</Badge>
|
||||||
<Badge variant="destructive">Conflito</Badge>
|
|
||||||
) : null}
|
) : null}
|
||||||
|
{outboxRow?.status === 'conflict' ? <Badge variant="destructive">Conflito</Badge> : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<RelationMultiSelect
|
<RelationMultiSelect
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,17 @@
|
||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
|
import { useRouter } from 'next/navigation'
|
||||||
|
import { useEffect, useRef, useSyncExternalStore } from 'react'
|
||||||
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
import type { LeadershipListSupportStatusResponse } from '@/app/(campaign)/campanha/(app)/liderancas/support-status/types'
|
import type { LeadershipListSupportStatusResponse } from '@/app/(campaign)/campanha/(app)/liderancas/support-status/types'
|
||||||
import { SupportStatusBadge } from '@/components/campaign/leadership/SupportStatusBadge'
|
import { SupportStatusBadge } from '@/components/campaign/leadership/SupportStatusBadge'
|
||||||
import { enqueueLeadershipUpdate } from '@/components/campaign/opsSync/opsDomainOutbox'
|
import {
|
||||||
|
discardOpsLeadershipUpdateOutboxRow,
|
||||||
|
enqueueLeadershipUpdate,
|
||||||
|
readOpsLeadershipUpdateOutboxRow,
|
||||||
|
subscribeOpsLeadershipUpdateOutboxRow,
|
||||||
|
} from '@/components/campaign/opsSync/opsDomainOutbox'
|
||||||
import { leadershipsCollection } from '@/components/campaign/opsSync/opsMirrorClient'
|
import { leadershipsCollection } from '@/components/campaign/opsSync/opsMirrorClient'
|
||||||
import { CampaignCellEditOverlay } from '@/components/campaign/shared/CampaignCellEditOverlay'
|
import { CampaignCellEditOverlay } from '@/components/campaign/shared/CampaignCellEditOverlay'
|
||||||
import { useCampaignCellAutosave } from '@/components/campaign/shared/useCampaignCellAutosave'
|
import { useCampaignCellAutosave } from '@/components/campaign/shared/useCampaignCellAutosave'
|
||||||
|
|
@ -16,12 +25,14 @@ import {
|
||||||
leadershipSupportStatuses,
|
leadershipSupportStatuses,
|
||||||
type SupportStatus,
|
type SupportStatus,
|
||||||
} from '@/lib/schemas/leadership'
|
} from '@/lib/schemas/leadership'
|
||||||
|
import { OPS_UPDATED_AT_CONFLICT_MESSAGE } from '@/lib/schemas/opsCas'
|
||||||
import { supportStatusLabels } from '@/utilities/leadership/leadershipLabels'
|
import { supportStatusLabels } from '@/utilities/leadership/leadershipLabels'
|
||||||
|
|
||||||
const STATUS_AUTOSAVE_MS = 150
|
const STATUS_AUTOSAVE_MS = 150
|
||||||
const SUPPORT_STATUS_ENDPOINT = '/campanha/liderancas/support-status'
|
const SUPPORT_STATUS_ENDPOINT = '/campanha/liderancas/support-status'
|
||||||
const SAVE_ERROR_MESSAGE = 'Não foi possível salvar o status. Tente novamente.'
|
const SAVE_ERROR_MESSAGE = 'Não foi possível salvar o status. Tente novamente.'
|
||||||
const DEFAULT_STATUS: SupportStatus = 'a_abordar'
|
const DEFAULT_STATUS: SupportStatus = 'a_abordar'
|
||||||
|
const CONFLICT_TOAST_ID_PREFIX = 'ops-leadership-status-conflict:'
|
||||||
|
|
||||||
type LeadershipListSupportStatusControlProps = {
|
type LeadershipListSupportStatusControlProps = {
|
||||||
leadershipID: number
|
leadershipID: number
|
||||||
|
|
@ -35,7 +46,61 @@ export const LeadershipListSupportStatusControl = ({
|
||||||
status,
|
status,
|
||||||
updatedAt,
|
updatedAt,
|
||||||
}: LeadershipListSupportStatusControlProps) => {
|
}: LeadershipListSupportStatusControlProps) => {
|
||||||
|
const router = useRouter()
|
||||||
const opsHybrid = resolveOpsHybridEnabled()
|
const opsHybrid = resolveOpsHybridEnabled()
|
||||||
|
const previousOutboxStatusRef = useRef<string | undefined>(undefined)
|
||||||
|
|
||||||
|
const outboxRow = useSyncExternalStore(
|
||||||
|
(onStoreChange) =>
|
||||||
|
opsHybrid
|
||||||
|
? subscribeOpsLeadershipUpdateOutboxRow(leadershipID, onStoreChange)
|
||||||
|
: () => undefined,
|
||||||
|
() => (opsHybrid ? readOpsLeadershipUpdateOutboxRow(leadershipID) : undefined),
|
||||||
|
() => undefined,
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const previous = previousOutboxStatusRef.current
|
||||||
|
previousOutboxStatusRef.current = outboxRow?.status
|
||||||
|
if (previous === 'pending' && outboxRow === undefined) {
|
||||||
|
router.refresh()
|
||||||
|
}
|
||||||
|
}, [outboxRow, router])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!opsHybrid || outboxRow?.status !== 'conflict') return
|
||||||
|
const toastId = `${CONFLICT_TOAST_ID_PREFIX}${leadershipID}`
|
||||||
|
toast.message(OPS_UPDATED_AT_CONFLICT_MESSAGE, {
|
||||||
|
id: toastId,
|
||||||
|
duration: Infinity,
|
||||||
|
action: {
|
||||||
|
label: 'Manter o meu',
|
||||||
|
onClick: () => {
|
||||||
|
void enqueueLeadershipUpdate({
|
||||||
|
leadershipId: leadershipID,
|
||||||
|
supportStatus: outboxRow.supportStatus,
|
||||||
|
municipalities: outboxRow.municipalities,
|
||||||
|
organizations: outboxRow.organizations,
|
||||||
|
stateDeputies: outboxRow.stateDeputies,
|
||||||
|
exclusive: outboxRow.exclusive,
|
||||||
|
notes: outboxRow.notes,
|
||||||
|
baseUpdatedAt: outboxRow.serverUpdatedAt ?? null,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
cancel: {
|
||||||
|
label: 'Usar o novo',
|
||||||
|
onClick: () => {
|
||||||
|
discardOpsLeadershipUpdateOutboxRow(leadershipID)
|
||||||
|
router.refresh()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return () => {
|
||||||
|
toast.dismiss(toastId)
|
||||||
|
}
|
||||||
|
}, [opsHybrid, outboxRow, leadershipID, router])
|
||||||
|
|
||||||
const { open, onOpenChange, value, change, isPending, errorMessage, statusMessage } =
|
const { open, onOpenChange, value, change, isPending, errorMessage, statusMessage } =
|
||||||
useCampaignCellAutosave<SupportStatus, LeadershipListSupportStatusResponse>({
|
useCampaignCellAutosave<SupportStatus, LeadershipListSupportStatusResponse>({
|
||||||
value: status ?? DEFAULT_STATUS,
|
value: status ?? DEFAULT_STATUS,
|
||||||
|
|
|
||||||
16
src/lib/campaignOps/opsHybridFormData.ts
Normal file
16
src/lib/campaignOps/opsHybridFormData.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
/** Client-safe FormData helpers for OPS_HYBRID enqueue paths (OH13). */
|
||||||
|
|
||||||
|
export const readFormRelationshipIds = (data: FormData, name: string): number[] => {
|
||||||
|
const ids: number[] = []
|
||||||
|
for (const raw of data.getAll(name)) {
|
||||||
|
if (typeof raw !== 'string' || raw.trim() === '') continue
|
||||||
|
const value = Number(raw)
|
||||||
|
if (Number.isInteger(value) && value > 0) ids.push(value)
|
||||||
|
}
|
||||||
|
return [...new Set(ids)]
|
||||||
|
}
|
||||||
|
|
||||||
|
export const readOptionalFormText = (data: FormData, name: string): string | undefined => {
|
||||||
|
const raw = data.get(name)
|
||||||
|
return typeof raw === 'string' && raw.trim() !== '' ? raw.trim() : undefined
|
||||||
|
}
|
||||||
|
|
@ -5,15 +5,18 @@ import { getPayload } from 'payload'
|
||||||
import { beforeAll, describe, expect, it } from 'vitest'
|
import { beforeAll, describe, expect, it } from 'vitest'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
updateLeadershipInternalCasRecord,
|
createActivityRecord,
|
||||||
updateLeadershipInternalRecord,
|
updateActivityCasRecord,
|
||||||
} from '@/app/(campaign)/campanha/actions/leadership'
|
} from '@/app/(campaign)/campanha/actions/activity'
|
||||||
import {
|
import {
|
||||||
createCampaignDemandRecord,
|
createCampaignDemandRecord,
|
||||||
transitionCampaignDemandCasRecord,
|
transitionCampaignDemandCasRecord,
|
||||||
transitionCampaignDemandRecord,
|
transitionCampaignDemandRecord,
|
||||||
} from '@/app/(campaign)/campanha/actions/demand'
|
} from '@/app/(campaign)/campanha/actions/demand'
|
||||||
import { updateActivityCasRecord, createActivityRecord } from '@/app/(campaign)/campanha/actions/activity'
|
import {
|
||||||
|
updateLeadershipInternalCasRecord,
|
||||||
|
updateLeadershipInternalRecord,
|
||||||
|
} from '@/app/(campaign)/campanha/actions/leadership'
|
||||||
import {
|
import {
|
||||||
setStateDeputyMunicipalitiesBatchCasRecord,
|
setStateDeputyMunicipalitiesBatchCasRecord,
|
||||||
setStateDeputyMunicipalitiesBatchRecord,
|
setStateDeputyMunicipalitiesBatchRecord,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue