Made it so the code remembers the user's previous answers to current exercises
This commit is contained in:
@@ -7,20 +7,20 @@ import {Fragment, useState} from "react";
|
||||
import LineTo from "react-lineto";
|
||||
import {CommonProps} from ".";
|
||||
|
||||
export default function MatchSentences({id, options, type, prompt, sentences, onNext, onBack}: MatchSentencesExercise & CommonProps) {
|
||||
export default function MatchSentences({id, options, type, prompt, sentences, userSolutions, onNext, onBack}: MatchSentencesExercise & CommonProps) {
|
||||
const [selectedQuestion, setSelectedQuestion] = useState<string>();
|
||||
const [userSolutions, setUserSolutions] = useState<{question: string; option: string}[]>([]);
|
||||
const [answers, setAnswers] = useState<{question: string; option: string}[]>(userSolutions);
|
||||
|
||||
const calculateScore = () => {
|
||||
const total = sentences.length;
|
||||
const correct = userSolutions.filter((x) => sentences.find((y) => y.id === x.question)?.solution === x.option || false).length;
|
||||
const correct = answers.filter((x) => sentences.find((y) => y.id === x.question)?.solution === x.option || false).length;
|
||||
|
||||
return {total, correct};
|
||||
};
|
||||
|
||||
const selectOption = (option: string) => {
|
||||
if (!selectedQuestion) return;
|
||||
setUserSolutions((prev) => [...prev.filter((x) => x.question !== selectedQuestion), {question: selectedQuestion, option}]);
|
||||
setAnswers((prev) => [...prev.filter((x) => x.question !== selectedQuestion), {question: selectedQuestion, option}]);
|
||||
setSelectedQuestion(undefined);
|
||||
};
|
||||
|
||||
@@ -64,9 +64,9 @@ export default function MatchSentences({id, options, type, prompt, sentences, on
|
||||
onClick={() => selectOption(id)}>
|
||||
<div
|
||||
style={
|
||||
userSolutions.find((x) => x.option === id)
|
||||
answers.find((x) => x.option === id)
|
||||
? {
|
||||
border: `2px solid ${getSentenceColor(userSolutions.find((x) => x.option === id)!.question)}`,
|
||||
border: `2px solid ${getSentenceColor(answers.find((x) => x.option === id)!.question)}`,
|
||||
}
|
||||
: {}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ export default function MatchSentences({id, options, type, prompt, sentences, on
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{userSolutions.map((solution, index) => (
|
||||
{answers.map((solution, index) => (
|
||||
<div key={`solution_${index}`} className="absolute">
|
||||
<LineTo
|
||||
className="rounded-full"
|
||||
@@ -93,7 +93,9 @@ export default function MatchSentences({id, options, type, prompt, sentences, on
|
||||
</div>
|
||||
|
||||
<div className="self-end flex flex-col-reverse items-center w-full md:justify-between md:items-start md:flex-row gap-8">
|
||||
<button className={clsx("btn btn-wide gap-4 relative text-white", errorButtonStyle)} onClick={onBack}>
|
||||
<button
|
||||
className={clsx("btn btn-wide gap-4 relative text-white", errorButtonStyle)}
|
||||
onClick={() => onBack({exercise: id, solutions: answers, score: calculateScore(), type})}>
|
||||
<div className="absolute left-4">
|
||||
<Icon path={mdiArrowLeft} color="white" size={1} />
|
||||
</div>
|
||||
@@ -101,7 +103,7 @@ export default function MatchSentences({id, options, type, prompt, sentences, on
|
||||
</button>
|
||||
<button
|
||||
className={clsx("btn btn-wide gap-4 relative text-white", infoButtonStyle)}
|
||||
onClick={() => onNext({exercise: id, solutions: userSolutions, score: calculateScore(), type})}>
|
||||
onClick={() => onNext({exercise: id, solutions: answers, score: calculateScore(), type})}>
|
||||
Next
|
||||
<div className="absolute right-4">
|
||||
<Icon path={mdiArrowRight} color="white" size={1} />
|
||||
|
||||
Reference in New Issue
Block a user