All files / src/app/student-page/page/assignments/take-assignment/question-list/free-response FreeResponse.jsx

7.69% Statements 1/13
0% Branches 0/5
0% Functions 0/5
8.33% Lines 1/12

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                    2x                                                                                                                          
import React, { useEffect, useState } from "react";
import { Box, Paper, Typography } from "@mui/material";
import { useTheme } from "@mui/material/styles";
import ReadOnlyMathEditor from "@/utils/components/TinyMCETextEditorReadOnly";
import TinyMCEAnswerEditor from "@/utils/components/TinyMCEAnswerEditor";
import { useDispatch, useSelector } from "react-redux";
import { _addMyAnswer } from "@/store/slices/student/StudentQuestionPage";
import { STUDENT_QUESTION_FONT_SIZE, questionContentBg } from "../studentQuestionSurfaces";
import "../StudentQuestion.scss";
 
const FreeResponse = ({ question, readOnly = false }) => {
    const theme = useTheme();
    const dispatch = useDispatch();
    const currentAnswer = useSelector(
        (state) =>
            state.StudentQuestionPage.myAnswers?.data?.find((item) => item.questionId === question._id)?.answer || ""
    );
 
    // The answer is now the TinyMCE editor's HTML (text + optional rendered
    // math-formula spans), not a raw LaTeX string from the old <math-field>.
    const [answerHtml, setAnswerHtml] = useState(currentAnswer);
 
    useEffect(() => {
        setAnswerHtml(currentAnswer);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [question._id]);
 
    useEffect(() => {
        dispatch(
            _addMyAnswer({
                questionId: question._id,
                questionType: "free-response",
                answer: answerHtml,
            })
        );
    }, [dispatch, answerHtml, question._id]);
 
    const questionBg = questionContentBg(theme);
 
    return (
        <Box className="student-free-response">
            <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 sx={{ mt: 2 }}>
                {readOnly ? (
                    <ReadOnlyMathEditor
                        content={answerHtml}
                        backgroundColor={questionBg}
                        fontSize={STUDENT_QUESTION_FONT_SIZE}
                    />
                ) : (
                    <TinyMCEAnswerEditor
                        content={answerHtml}
                        onChange={setAnswerHtml}
                        backgroundColor={questionBg}
                        fontSize={STUDENT_QUESTION_FONT_SIZE}
                    />
                )}
            </Box>
        </Box>
    );
};
 
export default FreeResponse;