fixing matching pairs send data

This commit is contained in:
Dimas Atmodjo 2024-12-16 00:38:37 +07:00
parent 3b447efeae
commit 6f5645d158
2 changed files with 20 additions and 4 deletions

View File

@ -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`);
}
};

View File

@ -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);
}
};