Skip to content

Commit c6409c1

Browse files
authored
feat: add cells data on leaf state update events (#254)
1 parent 3bc9bea commit c6409c1

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/components/orchestrator/Orchestrator.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,24 @@ export function Orchestrator(props: OrchestratorProps) {
232232
initialInterrogation?.stateData?.date,
233233
)
234234

235+
const getCurrentArticulationState = () => {
236+
// @ts-expect-error source has articulation
237+
return getArticulationState(source, getData(false))
238+
}
239+
235240
const { interrogation, updateInterrogation } = useInterrogation(
236241
initialInterrogation,
237242
{
238243
getArticulationState: () => {
239244
if (!hasArticulation) {
240245
return { items: [] }
241246
}
242-
// @ts-expect-error source has articulation
243-
return getArticulationState(source, getData(false))
247+
return getCurrentArticulationState()
244248
},
245249
getMultimode: getMultimode,
246250
},
247251
)
248-
useEvents(interrogation)
252+
useEvents(interrogation, getCurrentArticulationState, pageTag, getChangedData)
249253

250254
/** For validating the questionnaire, it tries to put a `VALIDATED` stateData with the `endPage` */
251255
const validateQuestionnaire = async () => {

src/components/orchestrator/hooks/useEvents.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
import type { getArticulationState } from '@inseefr/lunatic'
2+
13
import { usePostEvents } from '@/api/10-events'
24
import {
35
LeafStatesUpdatedEventType,
46
MultimodeMovedEventType,
57
QuestionnaireEventType,
68
} from '@/models/api'
79
import type { Interrogation } from '@/models/interrogation'
10+
import type { LunaticPageTag } from '@/models/lunaticType.ts'
811
import { hasMultimode } from '@/utils/env.ts'
912

1013
import { useCallbackOnValueChange } from './useCallbackOnValueChange.ts'
1114

1215
/**
13-
* Trigger events when specific operation happens during interrogation
16+
* Trigger events when specific operations happens during interrogation
1417
*/
15-
export function useEvents(interrogation: Interrogation) {
18+
export function useEvents(
19+
interrogation: Interrogation,
20+
getLeafStatesData: () => ReturnType<typeof getArticulationState>,
21+
pageTag: LunaticPageTag,
22+
getChangedData: (reset: boolean) => Record<string, unknown>,
23+
) {
1624
// Disable events api if multimode is not enabled
1725
if (!hasMultimode()) {
1826
return
@@ -49,18 +57,48 @@ export function useEvents(interrogation: Interrogation) {
4957
if (!leafStates || !interrogation.id) {
5058
return
5159
}
60+
61+
// Inject cell data inside the leafStates
62+
const data = getLeafStatesData()
63+
const leafStatesWithCells = leafStates.map((leafState, index) => {
64+
return {
65+
...leafState,
66+
cells: data.items[index].cells,
67+
}
68+
})
69+
5270
mutateAsync({
5371
data: {
5472
type: LeafStatesUpdatedEventType.LEAF_STATES_UPDATED,
5573
payload: {
5674
interrogationId: interrogation.id,
57-
leafStates,
75+
leafStates: leafStatesWithCells,
5876
},
5977
},
6078
})
6179
},
6280
)
6381

82+
// Send an event when data change between pages
83+
useCallbackOnValueChange(pageTag, () => {
84+
// No data where changed between pages
85+
const changedData = getChangedData(true) as {
86+
COLLECTED: Record<string, unknown>
87+
}
88+
if (Object.keys(changedData.COLLECTED).length === 0 || !interrogation.id) {
89+
return
90+
}
91+
92+
mutateAsync({
93+
data: {
94+
type: `QUESTIONNAIRE_UPDATED` as QuestionnaireEventType,
95+
payload: {
96+
interrogationId: interrogation.id,
97+
},
98+
},
99+
})
100+
})
101+
64102
// Send an event when the interrogation state changes
65103
useCallbackOnValueChange(interrogation.stateData?.state, (state) => {
66104
if (!state || !interrogation.id) {

src/models/api/questionnaireEventType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export const QuestionnaireEventType = {
1414
QUESTIONNAIRE_INIT: 'QUESTIONNAIRE_INIT',
1515
QUESTIONNAIRE_VALIDATED: 'QUESTIONNAIRE_VALIDATED',
1616
QUESTIONNAIRE_COMPLETED: 'QUESTIONNAIRE_COMPLETED',
17+
QUESTIONNAIRE_UPDATED: 'QUESTIONNAIRE_UPDATED',
1718
} as const

0 commit comments

Comments
 (0)