import React from "react"; import { QuestionTypes } from "./MkdJsonQuiz"; const MkdJsonQuizOption = ({ option, className, currentQuestion, updateQuestions, }) => { const singleChoiceUpdate = (id) => { const tempCurrentQuestion = { ...currentQuestion }; // Avoid deep cloning if possible // Check if the current answer has the same ID as the one being toggled if (tempCurrentQuestion?.answer?.id === id) { tempCurrentQuestion["answer"] = null; // Clear the answer updateQuestions(tempCurrentQuestion); return; // Exit the function to avoid further processing } // Find the index of the selected option based on the provided ID const optionIndex = tempCurrentQuestion?.options?.findIndex( (option) => option?.id === id ); if (optionIndex !== -1) { tempCurrentQuestion["answer"] = tempCurrentQuestion?.options[optionIndex]; updateQuestions(tempCurrentQuestion); } }; const multipleChoiceUpdate = (id) => { // console.log("id", id); const tempCurrentQuestion = { ...currentQuestion }; // Avoid deep cloning if possible // Check if the current answer has the same ID as the one being toggled if (tempCurrentQuestion?.answers && tempCurrentQuestion?.answers?.length) { const answerIndex = tempCurrentQuestion?.answers?.findIndex( (answer) => answer?.id === id ); if (answerIndex !== -1) { tempCurrentQuestion.answers.splice(answerIndex, 1); } else { // Find the index of the selected option based on the provided ID const optionIndex = tempCurrentQuestion?.options?.findIndex( (option) => option?.id === id ); if (optionIndex !== -1) { tempCurrentQuestion?.answers?.push( tempCurrentQuestion?.options[optionIndex] ); } } } else { tempCurrentQuestion["answers"] = []; // Find the index of the selected option based on the provided ID const optionIndex = tempCurrentQuestion?.options?.findIndex( (option) => option?.id === id ); if (optionIndex !== -1) { tempCurrentQuestion?.answers?.push( tempCurrentQuestion?.options[optionIndex] ); } } updateQuestions(tempCurrentQuestion); }; const isSelected = (id) => { if (currentQuestion["answers"] && currentQuestion["answers"]?.length) { return !!currentQuestion["answers"]?.find((answer) => answer?.id === id); } else { return false; } }; const toggleSelection = (id, type) => { switch (type) { case QuestionTypes.single_choice: singleChoiceUpdate(id); break; case QuestionTypes.multiple_choice: multipleChoiceUpdate(id); break; } }; return ( <> {currentQuestion && currentQuestion?.type === QuestionTypes.single_choice ? (