@@ -39,6 +39,7 @@ import type { Variables } from '../interfaces/variables.interface';
3939import { processCalculatedExpressions } from '../utils/calculatedExpression' ;
4040import { applyComputedUpdates } from '../utils/computedUpdates' ;
4141import {
42+ assignPopulatedAnswersToEnableWhen ,
4243 mutateRepeatEnableWhenItemInstances ,
4344 updateEnableWhenItemAnswer
4445} from '../utils/enableWhen' ;
@@ -441,6 +442,7 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
441442
442443 4. Re-run calculatedExpressions as a final step to ensure all calculations are up-to-date, incorporating any toggled options or dynamic value set changes.
443444 5. Update store state based on the expressions that changed
445+ 6. (If initial update) Initialise enableWhen items
444446 */
445447
446448 const {
@@ -450,6 +452,7 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
450452 variables,
451453 targetConstraints,
452454 answerOptionsToggleExpressions,
455+ enableWhenItems,
453456 enableWhenExpressions,
454457 processedValueSets,
455458 calculatedExpressions,
@@ -471,6 +474,8 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
471474 enableWhenExpressions ?: EnableWhenExpressions ;
472475 answerOptionsToggleExpressions ?: Record < string , AnswerOptionsToggleExpression [ ] > ;
473476 processedValueSets ?: Record < string , ProcessedValueSet > ;
477+ enableWhenItems ?: EnableWhenItems ;
478+ enableWhenLinkedQuestions ?: Record < string , string [ ] > ;
474479 }
475480 ) {
476481 // Update QuestionnaireStore state
@@ -489,6 +494,12 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
489494 } ) ,
490495 ...( otherExpressions ?. processedValueSets && {
491496 processedValueSets : otherExpressions . processedValueSets
497+ } ) ,
498+ ...( otherExpressions ?. enableWhenItems && {
499+ enableWhenItems : otherExpressions . enableWhenItems
500+ } ) ,
501+ ...( otherExpressions ?. enableWhenLinkedQuestions && {
502+ enableWhenLinkedQuestions : otherExpressions . enableWhenLinkedQuestions
492503 } )
493504 } ) ) ;
494505
@@ -573,6 +584,8 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
573584 enableWhenExpressions : EnableWhenExpressions ;
574585 answerOptionsToggleExpressions : Record < string , AnswerOptionsToggleExpression [ ] > ;
575586 processedValueSets : Record < string , ProcessedValueSet > ;
587+ enableWhenItems : EnableWhenItems ;
588+ enableWhenLinkedQuestions : Record < string , string [ ] > ;
576589 } > = { } ;
577590 if ( targetConstraintsUpdate . isUpdated ) {
578591 otherExpressionsToUpdate . targetConstraints = targetConstraintsUpdate . value ;
@@ -588,6 +601,16 @@ export const questionnaireStore = createStore<QuestionnaireStoreType>()((set, ge
588601 otherExpressionsToUpdate . processedValueSets = processedValueSetsUpdate . value ;
589602 }
590603
604+ // Step 6: Evaluate enableWhen on the first update
605+ if ( isInitialUpdate ) {
606+ const {
607+ initialisedItems : initialisedEnableWhenItems ,
608+ linkedQuestions : initialisedEnableWhenLinkedQuestions
609+ } = assignPopulatedAnswersToEnableWhen ( enableWhenItems , lastUpdatedResponse ) ;
610+ otherExpressionsToUpdate . enableWhenItems = initialisedEnableWhenItems ;
611+ otherExpressionsToUpdate . enableWhenLinkedQuestions = initialisedEnableWhenLinkedQuestions ;
612+ }
613+
591614 updateStoreState (
592615 lastUpdatedResponse ,
593616 updatedCalculatedExpressions ,
0 commit comments