From 6f5645d15823219a0ee2c2b0e1754ce00492e47b Mon Sep 17 00:00:00 2001 From: Dimas Atmodjo Date: Mon, 16 Dec 2024 00:38:37 +0700 Subject: [PATCH] fixing matching pairs send data --- .../user/exercise/hooks/useExercises.jsx | 3 +-- .../components/MatchingPairsQuestion.jsx | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/roles/user/exercise/hooks/useExercises.jsx b/src/roles/user/exercise/hooks/useExercises.jsx index b71686d..b07c37a 100644 --- a/src/roles/user/exercise/hooks/useExercises.jsx +++ b/src/roles/user/exercise/hooks/useExercises.jsx @@ -54,7 +54,6 @@ export const useExercises = (topic, level) => { ANSWER_STUDENT: answer }; setAnswers(newAnswers); - // localStorage.setItem(levelId, JSON.stringify(newAnswers)); localStorage.setItem(learningId, JSON.stringify(newAnswers)); }; @@ -98,7 +97,7 @@ export const useExercises = (topic, level) => { } catch (error) { console.error(error); }finally{ - // navigate(`/learning/module/${section}/${topic}/${level}/result`); + navigate(`/learning/module/${section}/${topic}/${level}/result`); } }; diff --git a/src/roles/user/exercise/views/components/MatchingPairsQuestion.jsx b/src/roles/user/exercise/views/components/MatchingPairsQuestion.jsx index b006fef..9c7184a 100644 --- a/src/roles/user/exercise/views/components/MatchingPairsQuestion.jsx +++ b/src/roles/user/exercise/views/components/MatchingPairsQuestion.jsx @@ -22,6 +22,17 @@ function stringToArray(str) { return str.split(', '); } +function extractAnswer(input) { + const lines = input.split('|'); + + const descriptions = lines.map(line => { + const parts = line.split('>'); + return parts[1]?.trim(); + }); + + return descriptions; +} + const MatchingPairsQuestion = ({ question, onAnswer, studentAnswer, index }) => { const savedAnswer = studentAnswer !== null ? studentAnswer.ANSWER_STUDENT : null; const [pairs, setPairs] = useState([]); @@ -54,7 +65,12 @@ const MatchingPairsQuestion = ({ question, onAnswer, studentAnswer, index }) => })); if (savedAnswer !== null) { - const arrSavedAnswer = stringToArray(savedAnswer); + //if use "," as separator + // const arrSavedAnswer = stringToArray(savedAnswer); + + //if use ">" as separator + const arrSavedAnswer = extractAnswer(savedAnswer); + const updatedPairs = initialPairs.map((pair, index) => ({ ...pair, right: arrSavedAnswer[index], @@ -105,13 +121,14 @@ const MatchingPairsQuestion = ({ question, onAnswer, studentAnswer, index }) => setIsComplete(true); const rightAnswers = newPairs.map((pair) => pair.right); + // onAnswer(index, arrayToString(rightAnswers), question.ID_ADMIN_EXERCISE); const stringPairs = newPairs .map(pair => `${pair.left}>${pair.right}`) .join("| "); - // onAnswer(index, arrayToString(rightAnswers), question.ID_ADMIN_EXERCISE); onAnswer(index, stringPairs, question.ID_ADMIN_EXERCISE); + // console.log(stringPairs); } };