All files / src/app/student-page/page/assignments/take-assignment/question-list/checkbox CheckboxOption.jsx

11.53% Statements 3/26
0% Branches 0/4
10% Functions 1/10
9.09% Lines 2/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                    52x   2x                                                                                                                                          
import { Box, Paper, Typography } from "@mui/material";
import { useTheme } from "@mui/material/styles";
import React, { useEffect, useState } from "react";
import ReadOnlyMathEditor from "@/utils/components/TinyMCETextEditorReadOnly";
import { useDispatch, useSelector } from "react-redux";
import { _addMyAnswer } from "@/store/slices/student/StudentQuestionPage";
import StudentChoiceRow from "../StudentChoiceRow";
import { STUDENT_QUESTION_FONT_SIZE, questionContentBg } from "../studentQuestionSurfaces";
import "../StudentQuestion.scss";
 
const letters = Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i));
 
const CheckboxOption = ({ question }) => {
    const theme = useTheme();
    const dispatch = useDispatch();
 
    const { myAnswers } = useSelector((state) => state.StudentQuestionPage);
    const currentAnswer = myAnswers?.data?.find((item) => item.questionId === question._id)?.answer;
 
    const [selectedChoices, setSelectedChoices] = useState([]);
 
    useEffect(() => {
        if (Array.isArray(currentAnswer)) {
            setSelectedChoices(currentAnswer);
        } else {
            setSelectedChoices([]);
        }
    }, [myAnswers]);
 
    const handleCheckboxChange = (choiceValue) => {
        setSelectedChoices((prevSelected) => {
            let updatedChoices;
 
            if (prevSelected.includes(choiceValue)) {
                updatedChoices = prevSelected.filter((val) => val !== choiceValue);
            } else {
                updatedChoices = [...prevSelected, choiceValue];
            }
 
            dispatch(
                _addMyAnswer({
                    questionId: question._id,
                    questionType: "checkbox",
                    answer: updatedChoices,
                })
            );
 
            return updatedChoices;
        });
    };
 
    const questionBg = questionContentBg(theme);
 
    return (
        <Box>
            <Paper variant="outlined" className="student-question-content" sx={{ bgcolor: questionBg }}>
                <Typography variant="h6">
                    <ReadOnlyMathEditor
                        content={question?.question}
                        backgroundColor={questionBg}
                        fontSize={STUDENT_QUESTION_FONT_SIZE}
                    />
                </Typography>
            </Paper>
 
            <Box className="student-choices-list">
                {question.choices.map((choice, index) => (
                    <StudentChoiceRow
                        key={index}
                        letter={letters[index]}
                        text={choice.text}
                        isSelected={selectedChoices.includes(choice.text)}
                        onClick={() => handleCheckboxChange(choice.text)}
                    />
                ))}
            </Box>
        </Box>
    );
};
 
export default CheckboxOption;