/* eslint-disable @next/next/no-img-element */ import { MultipleChoiceExercise, MultipleChoiceQuestion } from "@/interfaces/exam"; import useExamStore from "@/stores/examStore"; import clsx from "clsx"; import { useEffect, useState } from "react"; import reactStringReplace from "react-string-replace"; import { CommonProps } from "."; import Button from "../Low/Button"; function Question({ id, variant, prompt, options, userSolution, onSelectOption, }: MultipleChoiceQuestion & { userSolution: string | undefined; onSelectOption?: (option: string) => void; showSolution?: boolean, }) { const renderPrompt = (prompt: string) => { return reactStringReplace(prompt, /(.*?<\/u>)/g, (match) => { const word = match.replaceAll("", "").replaceAll("", ""); return word.length > 0 ? {word} : null; }); }; return (
{isNaN(Number(id)) ? ( {renderPrompt(prompt).filter((x) => x?.toString() !== "")} ) : ( <> {id} - {renderPrompt(prompt).filter((x) => x?.toString() !== "")} )}
{variant === "image" && options.map((option) => (
(onSelectOption ? onSelectOption(option.id.toString()) : null)} className={clsx( "flex flex-col items-center border border-mti-gray-platinum p-4 px-8 rounded-xl gap-4 cursor-pointer bg-white relative", userSolution === option.id.toString() && "border-mti-purple-light", )}> {option.id.toString()} {`Option
))} {variant === "text" && options.map((option) => (
(onSelectOption ? onSelectOption(option.id.toString()) : null)} className={clsx( "flex border p-4 rounded-xl gap-2 cursor-pointer bg-white text-base", userSolution === option.id.toString() && "border-mti-purple-light", )}> {option.id.toString()}. {option.text}
))}
); } export default function MultipleChoice({ id, prompt, type, questions, userSolutions, onNext, onBack }: MultipleChoiceExercise & CommonProps) { const [answers, setAnswers] = useState<{ question: string; option: string }[]>(userSolutions); //const { shuffleMaps } = useExamStore((state) => state); const { questionIndex, setQuestionIndex } = useExamStore((state) => state); const { userSolutions: storeUserSolutions, setUserSolutions } = useExamStore((state) => state); const hasExamEnded = useExamStore((state) => state.hasExamEnded); 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 }, [hasExamEnded]); const onSelectOption = (option: string) => { const question = questions[questionIndex]; setAnswers((prev) => [...prev.filter((x) => x.question !== question.id), { option, question: question.id }]); }; const calculateScore = () => { const total = questions.length; const correct = answers.filter((x) => { const matchingQuestion = questions.find((y) => { return y.id.toString() === x.question.toString(); }); let isSolutionCorrect; //if (shuffleMaps.length == 0) { isSolutionCorrect = matchingQuestion?.solution === x.option; //} else { // const shuffleMap = shuffleMaps.find((map) => map.id == x.question) // isSolutionCorrect = shuffleMap?.map[x.option] == matchingQuestion?.solution; //} return isSolutionCorrect || false; }).length; const missing = total - correct; return { total, correct, missing }; }; /*const getShuffles = () => { let shuffle = {}; if (shuffleMaps.length !== 0) { shuffle = { shuffleMaps: shuffleMaps.filter((map) => answers.some(answer => answer.question === map.id) ) } } return shuffle; }*/ const next = () => { if (questionIndex === questions.length - 1) { onNext({ exercise: id, solutions: answers, score: calculateScore(), type, });//...getShuffles() }); } else { setQuestionIndex(questionIndex + 1); } scrollToTop(); }; const back = () => { if (questionIndex === 0) { onBack({ exercise: id, solutions: answers, score: calculateScore(), type, });// ...getShuffles() }); } else { setQuestionIndex(questionIndex - 1); } scrollToTop(); }; return ( <>
{/*{"Select the appropriate option."}*/} {questionIndex < questions.length && ( questions[questionIndex].id === x.question)?.option} onSelectOption={onSelectOption} /> )}
); }