(typeof exam.parts[0].intro === "string" && !showSolutions);
@@ -120,37 +119,6 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
}, [partIndex, exerciseIndex, questionIndex]);
- useEffect(() => {
- const regex = /.*?['"](.*?)['"] in line (\d+)\?$/;
- if (
- exerciseIndex !== -1 && currentExercise &&
- currentExercise.type === "multipleChoice" &&
- currentExercise.questions[questionIndex] &&
- currentExercise.questions[questionIndex].prompt &&
- exam.parts[partIndex].context
- ) {
- const match = currentExercise.questions[questionIndex].prompt.match(regex);
- if (match) {
- const word = match[1];
- const originalLineNumber = match[2];
-
- if (word !== contextWord) {
- setContextWord(word);
- }
-
- const updatedPrompt = currentExercise.questions[questionIndex].prompt.replace(
- `in line ${originalLineNumber}`,
- `in line ${contextWordLine || originalLineNumber}`
- );
-
- currentExercise.questions[questionIndex].prompt = updatedPrompt;
- } else {
- setContextWord(undefined);
- }
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [currentExercise, questionIndex]);
-
const nextExercise = (solution?: UserSolution) => {
scrollToTop();
@@ -341,6 +309,38 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
});
}
+ useEffect(() => {
+ const regex = /.*?['"](.*?)['"] in line (\d+)\?$/;
+ if (
+ exerciseIndex !== -1 && currentExercise &&
+ currentExercise.type === "multipleChoice" &&
+ currentExercise.questions[questionIndex] &&
+ currentExercise.questions[questionIndex].prompt &&
+ exam.parts[partIndex].context
+ ) {
+ const match = currentExercise.questions[questionIndex].prompt.match(regex);
+ if (match) {
+ const word = match[1];
+ const originalLineNumber = match[2];
+
+ if (word !== contextWord) {
+ setContextWord(word);
+ }
+
+ const updatedPrompt = currentExercise.questions[questionIndex].prompt.replace(
+ `in line ${originalLineNumber}`,
+ `in line ${contextWordLine || originalLineNumber}`
+ );
+
+ currentExercise.questions[questionIndex].prompt = updatedPrompt;
+ setChangedPrompt(true);
+ } else {
+ setContextWord(undefined);
+ }
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [currentExercise, questionIndex, contextWordLine]);
+
useEffect(() => {
if (continueAnyways) {
setContinueAnyways(false);
@@ -375,6 +375,24 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
}
+ const memoizedRender = useMemo(() => {
+ setChangedPrompt(false);
+ return (
+ <>
+ {textRender ?
+ renderText() :
+ <>
+ {exam.parts[partIndex].context && renderText()}
+ {(showSolutions || editing) ?
+ renderSolution(currentExercise, nextExercise, previousExercise)
+ :
+ renderExercise(currentExercise, exam.id, nextExercise, previousExercise)
+ }
+ >
+ }
+ >)
+ }, [textRender, currentExercise, changedPrompt]);
+
return (
<>
@@ -429,18 +447,7 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
"mb-20 w-full",
!!exam.parts[partIndex].context && !textRender && "grid grid-cols-2 gap-4",
)}>
-
- {textRender ?
- renderText() :
- <>
- {exam.parts[partIndex].context && renderText()}
- {(showSolutions || editing) ?
- renderSolution(currentExercise, nextExercise, previousExercise)
- :
- renderExercise(currentExercise, exam.id, nextExercise, previousExercise)
- }
- >
- }
+ {memoizedRender}
{/*exerciseIndex === -1 && partIndex > 0 && (
From c9174f37ef1a555587b49625bc3ca70e997e950c Mon Sep 17 00:00:00 2001
From: Carlos Mesquita
Date: Tue, 27 Aug 2024 17:13:19 +0100
Subject: [PATCH 2/3] Forgot to toggle strict mode again
---
next.config.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/next.config.js b/next.config.js
index 2203b000..cdba31d8 100644
--- a/next.config.js
+++ b/next.config.js
@@ -1,7 +1,7 @@
/** @type {import('next').NextConfig} */
const websiteUrl = process.env.NODE_ENV === 'production' ? "https://encoach.com" : "http://localhost:3000";
const nextConfig = {
- reactStrictMode: false,
+ reactStrictMode: true,
output: "standalone",
async headers() {
return [
From ef32226c6ccf94638a90f5fe04c8c94c04ea9306 Mon Sep 17 00:00:00 2001
From: Carlos Mesquita
Date: Tue, 27 Aug 2024 22:10:31 +0100
Subject: [PATCH 3/3] Previous commit solved completion but messed up question
modal, patched that and added the condition to show the submission modal when
all questions are awnswered
---
src/components/Exercises/FillBlanks/index.tsx | 1 +
src/components/Exercises/MultipleChoice.tsx | 12 +------
src/exams/Level/index.tsx | 31 +++++++++++++++----
3 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/src/components/Exercises/FillBlanks/index.tsx b/src/components/Exercises/FillBlanks/index.tsx
index 96b3ae7e..063ef0b1 100644
--- a/src/components/Exercises/FillBlanks/index.tsx
+++ b/src/components/Exercises/FillBlanks/index.tsx
@@ -131,6 +131,7 @@ const FillBlanks: React.FC = ({
));
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, [text, variant, renderLines, currentMCSelection]);
diff --git a/src/components/Exercises/MultipleChoice.tsx b/src/components/Exercises/MultipleChoice.tsx
index e03487ac..8cdc4f72 100644
--- a/src/components/Exercises/MultipleChoice.tsx
+++ b/src/components/Exercises/MultipleChoice.tsx
@@ -81,9 +81,7 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti
shuffles,
hasExamEnded,
partIndex,
- userSolutions: storeUserSolutions,
setQuestionIndex,
- setUserSolutions,
setCurrentSolution
} = useExamStore((state) => state);
@@ -91,14 +89,6 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti
const scrollToTop = () => Array.from(document.getElementsByTagName("body")).forEach((body) => body.scrollTo(0, 0));
- useEffect(() => {
- setUserSolutions(
- [...storeUserSolutions.filter((x) => x.exercise !== id), {
- exercise: id, solutions: answers, score: calculateScore(), type
- }]);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [answers]);
-
useEffect(() => {
if (hasExamEnded) onNext({ exercise: id, solutions: answers, score: calculateScore(), type });
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -112,7 +102,7 @@ export default function MultipleChoice({ id, prompt, type, questions, userSoluti
useEffect(() => {
setCurrentSolution({ exercise: id, solutions: answers, score: calculateScore(), type, shuffleMaps: shuffleMaps });
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [answers])
+ }, [answers, setAnswers])
const getShuffledSolution = (originalSolution: string, questionShuffleMap: ShuffleMap) => {
for (const [newPosition, originalPosition] of Object.entries(questionShuffleMap.map)) {
diff --git a/src/exams/Level/index.tsx b/src/exams/Level/index.tsx
index 587c07ff..60e6bf65 100644
--- a/src/exams/Level/index.tsx
+++ b/src/exams/Level/index.tsx
@@ -51,12 +51,13 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
setCurrentSolution
} = useExamStore((state) => state);
-
const [multipleChoicesDone, setMultipleChoicesDone] = useState<{ id: string; amount: number }[]>([]);
const [showQuestionsModal, setShowQuestionsModal] = useState(false);
const [continueAnyways, setContinueAnyways] = useState(false);
const [textRender, setTextRender] = useState(false);
const [changedPrompt, setChangedPrompt] = useState(false);
+ const [nextExerciseCalled, setNextExerciseCalled] = useState(false);
+ const [currentSolutionSet, setCurrentSolutionSet] = useState(false);
const [seenParts, setSeenParts] = useState(showSolutions ? exam.parts.map((_, index) => index) : [0]);
@@ -80,6 +81,7 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
useEffect(() => {
if (typeof currentSolution !== "undefined") {
setUserSolutions([...userSolutions.filter((x) => x.exercise !== currentSolution.exercise), { ...currentSolution, module: "level" as Module, exam: exam.id, shuffleMaps: exam.shuffle ? [...shuffles.find((x) => x.exerciseID == currentExercise?.id)?.shuffles!] : [] }]);
+ setCurrentSolutionSet(true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentSolution, exam.id, exam.shuffle, shuffles, currentExercise])
@@ -118,8 +120,11 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [partIndex, exerciseIndex, questionIndex]);
+ const next = () => {
+ setNextExerciseCalled(true);
+ }
- const nextExercise = (solution?: UserSolution) => {
+ const nextExercise = () => {
scrollToTop();
if (exerciseIndex + 1 < exam.parts[partIndex].exercises.length && !hasExamEnded) {
@@ -155,13 +160,27 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
return;
}
+ if(partIndex + 1 === exam.parts.length && exerciseIndex === exam.parts[partIndex].exercises.length - 1 && !continueAnyways) {
+ modalKwargs();
+ setShowQuestionsModal(true);
+ }
+
setHasExamEnded(false);
if (typeof showSolutionsSave !== "undefined") {
onFinish(showSolutionsSave);
} else {
onFinish(userSolutions);
}
- };
+ }
+
+ useEffect(() => {
+ if (nextExerciseCalled && currentSolutionSet) {
+ nextExercise();
+ setNextExerciseCalled(false);
+ setCurrentSolutionSet(false);
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [nextExerciseCalled, currentSolutionSet])
const previousExercise = (solution?: UserSolution) => {
scrollToTop();
@@ -384,13 +403,13 @@ export default function Level({ exam, showSolutions = false, onFinish, editing =
<>
{exam.parts[partIndex].context && renderText()}
{(showSolutions || editing) ?
- renderSolution(currentExercise, nextExercise, previousExercise)
- :
- renderExercise(currentExercise, exam.id, nextExercise, previousExercise)
+ renderSolution(currentExercise, nextExercise, previousExercise) :
+ renderExercise(currentExercise, exam.id, next, previousExercise)
}
>
}
>)
+ // eslint-disable-next-line react-hooks/exhaustive-deps
}, [textRender, currentExercise, changedPrompt]);
return (