From ca4af46cc787eacfbf618022015da48a0f9b64a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Wed, 27 Sep 2023 16:08:25 +0200 Subject: [PATCH] Avoid race condition when simulator change --- site/source/components/conversation/useNavigateQuestions.ts | 6 ++++++ site/source/hooks/useNextQuestion.tsx | 2 +- site/source/store/selectors/simulationSelectors.ts | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/site/source/components/conversation/useNavigateQuestions.ts b/site/source/components/conversation/useNavigateQuestions.ts index c8b78b37f..9642eaacc 100644 --- a/site/source/components/conversation/useNavigateQuestions.ts +++ b/site/source/components/conversation/useNavigateQuestions.ts @@ -13,6 +13,7 @@ import { import { answeredQuestionsSelector, currentQuestionSelector, + urlSelector, useMissingVariables, } from '@/store/selectors/simulationSelectors' @@ -21,6 +22,7 @@ export function useNavigateQuestions(engines?: Array>) { 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>) { } }, [nextQuestion, currentQuestion]) + useEffect(() => { + dispatch(goToQuestion(nextQuestion)) + }, [url]) + return { currentQuestion: currentQuestion ?? nextQuestion, currentQuestionIsAnswered, diff --git a/site/source/hooks/useNextQuestion.tsx b/site/source/hooks/useNextQuestion.tsx index fe98a1800..163687fa2 100644 --- a/site/source/hooks/useNextQuestion.tsx +++ b/site/source/hooks/useNextQuestion.tsx @@ -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 } diff --git a/site/source/store/selectors/simulationSelectors.ts b/site/source/store/selectors/simulationSelectors.ts index c086dd2af..b57d5b8cc 100644 --- a/site/source/store/selectors/simulationSelectors.ts +++ b/site/source/store/selectors/simulationSelectors.ts @@ -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. *