diff --git a/site/source/components/conversation/RuleInput.tsx b/site/source/components/conversation/RuleInput.tsx index 58ba301e7..aa028efa5 100644 --- a/site/source/components/conversation/RuleInput.tsx +++ b/site/source/components/conversation/RuleInput.tsx @@ -151,7 +151,7 @@ export default function RuleInput({ <> commonProps.onChange({ batchUpdate: c })} + onChange={(c) => commonProps.onChange(c)} value={value as Evaluation} /> diff --git a/site/source/domaine/updateSituation.ts b/site/source/domaine/updateSituation.ts index 068b58d3e..4495a35eb 100644 --- a/site/source/domaine/updateSituation.ts +++ b/site/source/domaine/updateSituation.ts @@ -1,3 +1,6 @@ +import { pipe } from 'effect' +import { isNumber, isString } from 'effect/Predicate' +import * as R from 'effect/Record' import { DottedName } from 'modele-social' import { PublicodesExpression } from 'publicodes' @@ -19,8 +22,17 @@ export function updateSituation( const objectifsExclusifs = config['objectifs exclusifs'] ?? [] + const nouvellesValeurs = + isString(value) || isNumber(value) + ? { [dottedName]: value } + : pipe( + value, + R.mapKeys((suffixe) => `${dottedName} . ${suffixe}`), + R.map((valeur) => (isString(valeur) ? `'${valeur}'` : valeur)) // 😭 + ) + if (!objectifsExclusifs.includes(dottedName)) { - return { ...currentSituation, [dottedName]: value } + return { ...currentSituation, ...nouvellesValeurs } } const objectifsToReset = objectifsExclusifs.filter( diff --git a/site/source/store/middlewares/prendLaProchaineQuestion.middleware.ts b/site/source/store/middlewares/prendLaProchaineQuestion.middleware.ts index d31ee38ec..5e6457c36 100644 --- a/site/source/store/middlewares/prendLaProchaineQuestion.middleware.ts +++ b/site/source/store/middlewares/prendLaProchaineQuestion.middleware.ts @@ -73,9 +73,9 @@ export const prendLaProchaineQuestionMiddleware = if (!lastSituationsAvecContextes || situationAChangé) { lastSituationsAvecContextes = situationsAvecContextes - engines.forEach((engine, index) => + engines.forEach((engine, index) => { engine.setSituation(situationsAvecContextes[index]) - ) + }) lastSimulation = simulation lastConfig = config diff --git a/site/source/store/reducers/simulation.reducer.spec.ts b/site/source/store/reducers/simulation.reducer.spec.ts index 02e5e8258..7cae3d445 100644 --- a/site/source/store/reducers/simulation.reducer.spec.ts +++ b/site/source/store/reducers/simulation.reducer.spec.ts @@ -174,6 +174,48 @@ describe('simulationReducer', () => { answeredQuestions: ['a', 'b', 'c'], }) }) + it('enregistre la réponse dans la situation', () => { + const state = { + answeredQuestions: ['a', 'b'], + config: { + 'objectifs exclusifs': [], + }, + } + + expect( + simulationReducer( + state as unknown as Simulation, + enregistreLaRéponse('c' as DottedName, 42) + ) + ).toMatchObject({ + situation: { + c: 42, + }, + }) + }) + it('enregistre correctement les réponses multiples', () => { + const state = { + answeredQuestions: ['a', 'b'], + config: { + 'objectifs exclusifs': [], + }, + } + + expect( + simulationReducer( + state as unknown as Simulation, + enregistreLaRéponse('c' as DottedName, { + sub1: 42, + ['sub2 . subC']: 'hello', + }) + ) + ).toMatchObject({ + situation: { + 'c . sub1': 42, + 'c . sub2 . subC': `'hello'`, + }, + }) + }) }) describe('DELETE_FROM_SITUATION', () => { it('supprime la question des questions répondues si réponse effacée', () => {