mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-26 21:53:06 +00:00
refactor(OH7): extrair opsVotePledgeMirror para quebrar ciclo de imports
This commit is contained in:
parent
c7a8dc7f64
commit
387cf90d0f
5 changed files with 73 additions and 59 deletions
|
|
@ -19,7 +19,7 @@ import {
|
|||
applyOpsVotePledgeEstimateWrite,
|
||||
patchOpsVotePledgeEstimateOptimistic,
|
||||
readOpsVotePledge,
|
||||
} from '@/components/campaign/opsSync/opsMirrorClient'
|
||||
} from '@/components/campaign/opsSync/opsVotePledgeMirror'
|
||||
import { opsOutboxKey, type OpsOutboxKey } from '@/lib/campaignOps/opsContract'
|
||||
import {
|
||||
isOpsEstimateConflictMessage,
|
||||
|
|
|
|||
|
|
@ -8,10 +8,7 @@ import {
|
|||
type OpsMirrorPersistenceMode,
|
||||
type OpsMirrorStore,
|
||||
} from '@/components/campaign/opsSync/opsMirrorPersistence'
|
||||
import {
|
||||
mapVotePledgeWriteToOps,
|
||||
type VotePledgeEstimateWriteResult,
|
||||
} from '@/lib/campaignOps/mapVotePledgeWriteToOps'
|
||||
import { votePledgesCollection } from '@/components/campaign/opsSync/opsVotePledgeMirror'
|
||||
import {
|
||||
createEmptyOpsSnapshot,
|
||||
type OpsActivity,
|
||||
|
|
@ -24,12 +21,10 @@ import {
|
|||
type OpsOutboxKey,
|
||||
type OpsSnapshot,
|
||||
type OpsStateDeputy,
|
||||
type OpsVotePledge,
|
||||
} from '@/lib/campaignOps/opsContract'
|
||||
import { mergeOpsSnapshot } from '@/lib/campaignOps/opsMerge'
|
||||
import { OPS_MIRROR_SCHEMA_VERSION } from '@/lib/campaignOps/opsMirrorVersion'
|
||||
import type { OpsSyncState } from '@/lib/campaignOps/opsSyncMeta'
|
||||
import type { VoteEstimateScenarioViewModel } from '@/lib/voteEstimate'
|
||||
|
||||
export const OPS_SYNC_PATH = '/campanha/api/ops-sync'
|
||||
|
||||
|
|
@ -43,7 +38,7 @@ const createRowCollection = <T extends { id: number }>(id: string) =>
|
|||
|
||||
export const municipalitiesCollection = createRowCollection<OpsMunicipality>('ops-municipalities')
|
||||
const leadershipsCollection = createRowCollection<OpsLeadership>('ops-leaderships')
|
||||
export const votePledgesCollection = createRowCollection<OpsVotePledge>('ops-vote-pledges')
|
||||
export { votePledgesCollection } from '@/components/campaign/opsSync/opsVotePledgeMirror'
|
||||
const activitiesCollection = createRowCollection<OpsActivity>('ops-activities')
|
||||
const stateDeputiesCollection = createRowCollection<OpsStateDeputy>('ops-state-deputies')
|
||||
const organizationsCollection = createRowCollection<OpsOrganization>('ops-organizations')
|
||||
|
|
@ -261,51 +256,3 @@ export const syncOpsMirror = async (options: SyncOpsMirrorOptions = {}): Promise
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const readOpsVotePledge = (pledgeId: number): OpsVotePledge | undefined =>
|
||||
votePledgesCollection.get(pledgeId)
|
||||
|
||||
export const subscribeOpsVotePledge = (
|
||||
pledgeId: number,
|
||||
onStoreChange: () => void,
|
||||
): (() => void) => {
|
||||
let previous = votePledgesCollection.get(pledgeId)
|
||||
const subscription = votePledgesCollection.subscribeChanges(() => {
|
||||
const next = votePledgesCollection.get(pledgeId)
|
||||
if (next === previous) return
|
||||
previous = next
|
||||
onStoreChange()
|
||||
})
|
||||
return () => {
|
||||
subscription.unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
/** Optimistic estimate patch while the outbox row is pending. */
|
||||
export const patchOpsVotePledgeEstimateOptimistic = (
|
||||
pledgeId: number,
|
||||
estimatedVotes: VoteEstimateScenarioViewModel,
|
||||
estimateNote: string | null,
|
||||
): void => {
|
||||
if (!votePledgesCollection.has(pledgeId)) return
|
||||
votePledgesCollection.update(pledgeId, (draft) => {
|
||||
draft.estimatedVotes = estimatedVotes
|
||||
draft.estimateNote = estimateNote
|
||||
})
|
||||
}
|
||||
|
||||
/** Authoritative patch after a successful server write (OH7). */
|
||||
export const applyOpsVotePledgeEstimateWrite = (doc: VotePledgeEstimateWriteResult): void => {
|
||||
const mapped = mapVotePledgeWriteToOps(doc)
|
||||
if (votePledgesCollection.has(mapped.id)) {
|
||||
votePledgesCollection.update(mapped.id, (draft) => {
|
||||
draft.estimatedVotes = mapped.estimatedVotes
|
||||
draft.estimateNote = mapped.estimateNote
|
||||
draft.estimatedAt = mapped.estimatedAt
|
||||
draft.estimatedBy = mapped.estimatedBy
|
||||
draft.updatedAt = mapped.updatedAt
|
||||
})
|
||||
return
|
||||
}
|
||||
votePledgesCollection.insert(mapped)
|
||||
}
|
||||
|
|
|
|||
65
src/components/campaign/opsSync/opsVotePledgeMirror.ts
Normal file
65
src/components/campaign/opsSync/opsVotePledgeMirror.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
'use client'
|
||||
|
||||
import { createCollection, localOnlyCollectionOptions } from '@tanstack/db'
|
||||
|
||||
import {
|
||||
mapVotePledgeWriteToOps,
|
||||
type VotePledgeEstimateWriteResult,
|
||||
} from '@/lib/campaignOps/mapVotePledgeWriteToOps'
|
||||
import type { OpsVotePledge } from '@/lib/campaignOps/opsContract'
|
||||
import type { VoteEstimateScenarioViewModel } from '@/lib/voteEstimate'
|
||||
|
||||
export const votePledgesCollection = createCollection(
|
||||
localOnlyCollectionOptions<OpsVotePledge, number>({
|
||||
id: 'ops-vote-pledges',
|
||||
getKey: (row) => row.id,
|
||||
}),
|
||||
)
|
||||
|
||||
export const readOpsVotePledge = (pledgeId: number): OpsVotePledge | undefined =>
|
||||
votePledgesCollection.get(pledgeId)
|
||||
|
||||
export const subscribeOpsVotePledge = (
|
||||
pledgeId: number,
|
||||
onStoreChange: () => void,
|
||||
): (() => void) => {
|
||||
let previous = votePledgesCollection.get(pledgeId)
|
||||
const subscription = votePledgesCollection.subscribeChanges(() => {
|
||||
const next = votePledgesCollection.get(pledgeId)
|
||||
if (next === previous) return
|
||||
previous = next
|
||||
onStoreChange()
|
||||
})
|
||||
return () => {
|
||||
subscription.unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
/** Optimistic estimate patch while the outbox row is pending. */
|
||||
export const patchOpsVotePledgeEstimateOptimistic = (
|
||||
pledgeId: number,
|
||||
estimatedVotes: VoteEstimateScenarioViewModel,
|
||||
estimateNote: string | null,
|
||||
): void => {
|
||||
if (!votePledgesCollection.has(pledgeId)) return
|
||||
votePledgesCollection.update(pledgeId, (draft) => {
|
||||
draft.estimatedVotes = estimatedVotes
|
||||
draft.estimateNote = estimateNote
|
||||
})
|
||||
}
|
||||
|
||||
/** Authoritative patch after a successful server write (OH7). */
|
||||
export const applyOpsVotePledgeEstimateWrite = (doc: VotePledgeEstimateWriteResult): void => {
|
||||
const mapped = mapVotePledgeWriteToOps(doc)
|
||||
if (votePledgesCollection.has(mapped.id)) {
|
||||
votePledgesCollection.update(mapped.id, (draft) => {
|
||||
draft.estimatedVotes = mapped.estimatedVotes
|
||||
draft.estimateNote = mapped.estimateNote
|
||||
draft.estimatedAt = mapped.estimatedAt
|
||||
draft.estimatedBy = mapped.estimatedBy
|
||||
draft.updatedAt = mapped.updatedAt
|
||||
})
|
||||
return
|
||||
}
|
||||
votePledgesCollection.insert(mapped)
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ import type { OpsEstimateSyncStatus } from '@/components/campaign/opsSync/opsEst
|
|||
import {
|
||||
readOpsVotePledge,
|
||||
subscribeOpsVotePledge,
|
||||
} from '@/components/campaign/opsSync/opsMirrorClient'
|
||||
} from '@/components/campaign/opsSync/opsVotePledgeMirror'
|
||||
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
|
||||
import { VoteEstimateScenarioInputs } from '@/components/campaign/votePledge/VoteEstimateScenarioInputs'
|
||||
import { Badge } from '@/components/ui/Badge'
|
||||
|
|
|
|||
|
|
@ -3,18 +3,20 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
|
|||
|
||||
import {
|
||||
applyOpsMirrorSnapshot,
|
||||
applyOpsVotePledgeEstimateWrite,
|
||||
bootstrapOpsMirror,
|
||||
getOpsMirrorLastSyncedAt,
|
||||
getOpsMirrorPersistenceMode,
|
||||
municipalitiesCollection,
|
||||
OPS_SYNC_PATH,
|
||||
patchOpsVotePledgeEstimateOptimistic,
|
||||
readOpsMirrorSnapshot,
|
||||
resetOpsMirrorClientForTests,
|
||||
syncOpsMirror,
|
||||
votePledgesCollection,
|
||||
} from '@/components/campaign/opsSync/opsMirrorClient'
|
||||
import {
|
||||
applyOpsVotePledgeEstimateWrite,
|
||||
patchOpsVotePledgeEstimateOptimistic,
|
||||
} from '@/components/campaign/opsSync/opsVotePledgeMirror'
|
||||
import { createMemoryOpsMirrorStore } from '@/components/campaign/opsSync/opsMirrorPersistence'
|
||||
import {
|
||||
formatOpsSyncRelative,
|
||||
|
|
|
|||
Loading…
Reference in a new issue