From 3c7783c510ef0b348d8c56e0a49e6b477d8ad6f8 Mon Sep 17 00:00:00 2001 From: Kasper Baun Date: Tue, 27 Feb 2024 13:00:00 +0100 Subject: [PATCH] fix: corrected a hook that caused re-renders on each mouseclick --- .vscode/settings.json | 2 ++ packages/core/src/components/QuickForm.tsx | 3 +- .../components/error-message/ErrorMessage.tsx | 10 +++++-- .../slide-renderer/SlideRenderer.tsx | 29 ++----------------- .../core/src/hooks/useHandleEnterKeypress.ts | 2 +- packages/core/src/hooks/useHandleScroll.ts | 11 ++++--- packages/core/src/hooks/useKeyPressHandler.ts | 6 ++-- packages/core/src/model/InputType.ts | 7 +++-- packages/core/src/model/QuestionModel.ts | 2 +- .../defaults/DefaultInputTypeResolver.ts | 1 + .../defaults/DefaultQuestionTransformer.ts | 2 +- packages/core/tsconfig.json | 2 +- 12 files changed, 31 insertions(+), 46 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/packages/core/src/components/QuickForm.tsx b/packages/core/src/components/QuickForm.tsx index bb5f44d..195805f 100644 --- a/packages/core/src/components/QuickForm.tsx +++ b/packages/core/src/components/QuickForm.tsx @@ -1,7 +1,7 @@ import "../style/QuickForm.css"; import React from "react"; import { useQuickForm } from "../state/QuickFormContext"; -import { Ending, ErrorMessage, Submit } from "./index"; +import { Ending, Submit } from "./index"; import { Intro } from "./intro/Intro"; import { SlideRenderer } from "./slide-renderer/SlideRenderer"; @@ -21,7 +21,6 @@ export const QuickForm: React.FC = () => { return (
- {state.errorMsg && state.errorMsg !== "" && }
); diff --git a/packages/core/src/components/error-message/ErrorMessage.tsx b/packages/core/src/components/error-message/ErrorMessage.tsx index 9fa5d38..d769a5e 100644 --- a/packages/core/src/components/error-message/ErrorMessage.tsx +++ b/packages/core/src/components/error-message/ErrorMessage.tsx @@ -9,9 +9,15 @@ type ErrorProps = { export const ErrorMessage: React.FC = ({ message }: ErrorProps) => { const [isVisible, setIsVisible] = useState(false); - const { dispatch } = useQuickForm(); + const { dispatch, state } = useQuickForm(); - useDelayedClickListener(() => dispatch({ type: "SET_ERROR_MSG", msg: "" })); + const resetErrorMessage = () => { + if (state.errorMsg !== "") { + dispatch({ type: "SET_ERROR_MSG", msg: "" }) + } + } + + useDelayedClickListener(resetErrorMessage); useEffect(() => { if (message) { diff --git a/packages/core/src/components/slide-renderer/SlideRenderer.tsx b/packages/core/src/components/slide-renderer/SlideRenderer.tsx index 4a2d9c6..d9213d6 100644 --- a/packages/core/src/components/slide-renderer/SlideRenderer.tsx +++ b/packages/core/src/components/slide-renderer/SlideRenderer.tsx @@ -6,39 +6,16 @@ import { Slide } from '../slide/Slide'; export const SlideRenderer: React.FC = () => { - const { state, goToNextSlide, answerQuestion } = useQuickForm(); + const { state, answerQuestion } = useQuickForm(); const currentSlide = state.slides[state.currIdx]; - // const [allQuestionsAnswered, setAllQuestionsAnswered] = useState(false); - - // useEffect(() => { - // // This effect runs whenever currentSlide changes, to reset our check. - // setAllQuestionsAnswered(currentSlide.isAnswered); - // }, [currentSlide]); - - // useEffect(() => { - // // This effect attempts to navigate to the next slide if all questions are answered. - // if (allQuestionsAnswered) { - // goToNextSlide(); - // } - // }, [allQuestionsAnswered, goToNextSlide]); + /* If all questions are answered, goToNextSlide() is called by the answerQuestion function (dispatch) */ const handleGoToNextSlideClicked = () => { for (var q of currentSlide.questions) { if (typeof q.output !== "undefined" && q.output !== "") { answerQuestion(q.logicalName!, q.output); } } - - // // Check outside the loop, after state has a chance to update - // const allAnswered = !currentSlide.questions.some(q => q.answered === false); - // setAllQuestionsAnswered(allAnswered); - - // if (!allAnswered) { - // console.log("Not all question have been answered on slide: ", currentSlide); - // } else { - // console.log("All questions answered for slide: ", currentSlide); - // // We no longer directly call goToNextSlide here - // } }; /* Listens to enter key pressed */ @@ -53,7 +30,7 @@ export const SlideRenderer: React.FC = () => { >