Avoid race condition when simulator change

pull/2827/head
Jérémy Rialland 2023-09-27 16:08:25 +02:00 committed by Jérémy Rialland
parent 497a675d06
commit ca4af46cc7
3 changed files with 9 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import {
import {
answeredQuestionsSelector,
currentQuestionSelector,
urlSelector,
useMissingVariables,
} from '@/store/selectors/simulationSelectors'
@ -21,6 +22,7 @@ export function useNavigateQuestions(engines?: Array<Engine<DottedName>>) {
const engine = useEngine()
const nextQuestion = useNextQuestions(engines)[0]
const currentQuestion = useSelector(currentQuestionSelector)
const url = useSelector(urlSelector)
const missingVariables = useMissingVariables({ engines: engines ?? [engine] })
const currentQuestionIsAnswered =
@ -45,6 +47,10 @@ export function useNavigateQuestions(engines?: Array<Engine<DottedName>>) {
}
}, [nextQuestion, currentQuestion])
useEffect(() => {
dispatch(goToQuestion(nextQuestion))
}, [url])
return {
currentQuestion: currentQuestion ?? nextQuestion,
currentQuestionIsAnswered,

View File

@ -83,7 +83,7 @@ export const useNextQuestions = function (
return next.filter(
(question) => engine.getRule(question).rawNode.question !== undefined
)
}, [missingVariables, config, answeredQuestions, engine])
}, [missingVariables, config.questions, answeredQuestions, engine])
return nextQuestions
}

View File

@ -70,6 +70,8 @@ export const answeredQuestionsSelector = (state: RootState) =>
export const shouldFocusFieldSelector = (state: RootState) =>
state.simulation?.shouldFocusField ?? false
export const urlSelector = (state: RootState) => state.simulation?.url
/**
* Merge objectifs missings that depends on the same input field.
*