All files / src/app/teacher-page/page/assignments/new-assignment/student-preview/layout-2 Paginated.jsx

0% Statements 0/36
0% Branches 0/39
0% Functions 0/11
0% Lines 0/34

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState } from "react";
import Typography from "@mui/material/Typography";
import Pagination from "@mui/material/Pagination";
import { Box, Button, Chip, IconButton, Paper } from "@mui/material";
import { useDispatch, useSelector } from "react-redux";
import VolumeUpIcon from "@mui/icons-material/VolumeUp";
import ReadAloud from "@/utils/components/ReadAloud";
import FlagIcon from "@mui/icons-material/Flag";
import FlagOutlinedIcon from "@mui/icons-material/FlagOutlined";
 
// actions
import { _updateQuestionFlag } from "@/store/slices/student/StudentQuestionPage";
import MultipleChoiceOption from "@/app/student-page/page/assignments/take-assignment/question-list/multiple-choice/MultipleChoiceOption";
import CheckboxOption from "@/app/student-page/page/assignments/take-assignment/question-list/checkbox/CheckboxOption";
import FreeResponse from "@/app/student-page/page/assignments/take-assignment/question-list/free-response/FreeResponse";
import GraphOption from "@/app/student-page/page/assignments/take-assignment/question-list/graph/GraphOption";
import DropDownMenu from "@/app/student-page/page/assignments/take-assignment/question-list/drop-down-menu/DropDownMenu";
import DragAndDrop from "@/app/student-page/page/assignments/take-assignment/question-list/drag-and-drop/DragAndDrop";
import EmbeddedMultipleChoice from "@/app/student-page/page/assignments/take-assignment/question-list/embedded-multiple-choice/EmbeddedMultipleChoice";
import SingleStimulus from "@/app/student-page/page/assignments/take-assignment/question-list/single-stimulus/SingleStimulus";
import MultiPartQuestion from "@/app/student-page/page/assignments/take-assignment/question-list/multi-part-question/MultiPartQuestion";
import GridQuestion from "@/app/student-page/page/assignments/take-assignment/question-list/grid-question/GridQuestion";
import InteractiveDots from "@/app/student-page/page/assignments/take-assignment/question-list/interactive-dots/InteractiveDots";
import { isInteractiveDotsQuestion } from "@/utils/components/question-bank/question-display/interactive-dots/interactiveDotsUtils";
 
export default function PaginationControlled({ page, setPage }) {
    const dispatch = useDispatch();
 
    const { myAnswers } = useSelector((state) => state.StudentQuestionPage);
    const { assignmentData } = useSelector((state) => state.TeacherCreateAssignment);
 
    const { speak, cancelSpeech, speaking } = ReadAloud();
 
    const handleChange = (event, value) => {
        setPage(value);
    };
 
    const questionIndex = page - 1;
 
    const handleSpeakClick = (text) => {
        speak(text);
    };
 
    const handleQuestionFlag = (questionIndex) => {
        dispatch(_updateQuestionFlag(questionIndex));
    };
 
    const displayQuestionType = (question) => {
        // key per question — this preview pages questions through a single reused
        // slot, so each type needs a per-question key to avoid carrying the
        // previous question's local answer state. (EI-2906 cross-question bleed)
        if (isInteractiveDotsQuestion(question)) {
            return <InteractiveDots key={question._id} question={question} />;
        }
        switch (question?.questionType?.toLowerCase()) {
            case "multiple-choice":
                return <MultipleChoiceOption key={question._id} question={question} />;
            case "checkbox":
                return <CheckboxOption key={question._id} question={question} />;
            case "free-response":
                return <FreeResponse key={question._id} question={question} />;
            case "graph":
                return <GraphOption key={question._id} question={question} />;
            case "drop-down-menu":
                return <DropDownMenu key={question._id} question={question} />;
            case "drag-and-drop":
                return <DragAndDrop key={question._id} question={question} />;
            case "embedded-multiple-choice":
                return <EmbeddedMultipleChoice key={question._id} question={question} />;
            case "single-stimulus":
                return <SingleStimulus key={question._id} question={question} />;
            case "multi-part-question":
                return <MultiPartQuestion key={question._id} question={question} />;
            case "grid-question":
                return <GridQuestion key={question._id} question={question} />;
            case "graph-multiple-select":
            case "graph-multiple-choice":
                return <InteractiveDots key={question._id} question={question} />;
            default:
                break;
        }
    };
 
    const isFlagged = myAnswers?.data?.[questionIndex]?.isFlagged;
    const selectedQuestions = assignmentData?.step2?.selectedQuestions ?? [];
 
    return (
        <Box
            sx={{
                minHeight: "70vh",
                display: "flex",
                flexDirection: "column",
                gap: 3,
                p: 3,
            }}
        >
            <Paper
                elevation={0}
                sx={{
                    borderRadius: 3,
                    border: (t) => `1px solid ${t.palette.divider}`,
                    overflow: "hidden",
                    flex: 1,
                }}
            >
                {/* Question Header */}
                <Box
                    sx={{
                        px: 3,
                        py: 1.5,
                        display: "flex",
                        alignItems: "center",
                        justifyContent: "space-between",
                        bgcolor: "background.default",
                        borderBottom: (t) => `1px solid ${t.palette.divider}`,
                    }}
                >
                    <Box sx={{ display: "flex", alignItems: "center", gap: 2 }}>
                        <Typography>
                            {selectedQuestions[questionIndex]?.points}{" "}
                            {selectedQuestions[questionIndex]?.points > 1 ? "Points" : "Point"}
                        </Typography>
                        <Chip
                            label={selectedQuestions[questionIndex]?.questionType}
                            size="small"
                            variant="outlined"
                            color="primary"
                            sx={{ fontSize: "0.75rem", color: "primary.main" }}
                        />
                    </Box>
                    <Typography>
                        Question {page} of {selectedQuestions.length}
                    </Typography>
                    <Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
                        <IconButton
                            size="small"
                            color={speaking ? "primary" : "default"}
                            onClick={() => handleSpeakClick(selectedQuestions[questionIndex]?.question ?? "")}
                        >
                            <VolumeUpIcon />
                        </IconButton>
                        <Button
                            variant={isFlagged ? "contained" : "outlined"}
                            size="small"
                            color={isFlagged ? "warning" : "inherit"}
                            startIcon={isFlagged ? <FlagIcon /> : <FlagOutlinedIcon />}
                            onClick={() => handleQuestionFlag(questionIndex)}
                            sx={{
                                borderRadius: 2,
                                textTransform: "none",
                                fontWeight: 500,
                                borderColor: isFlagged ? "warning.main" : "#c0c8d4",
                                color: isFlagged ? "white" : "text.secondary",
                                "&:hover": {
                                    borderColor: isFlagged ? "warning.dark" : "#a0a8b4",
                                    bgcolor: isFlagged ? "warning.dark" : "#f5f5f5",
                                },
                            }}
                        >
                            {isFlagged ? "Flagged" : "Flag"}
                        </Button>
                    </Box>
                </Box>
 
                {/* Question Content */}
                <Box
                    sx={{
                        p: 3,
                        minHeight: 300,
                    }}
                >
                    {displayQuestionType(selectedQuestions[questionIndex])}
                </Box>
            </Paper>
 
            {/* Pagination */}
            <Box
                sx={{
                    display: "flex",
                    justifyContent: "center",
                    py: 2,
                }}
            >
                <Pagination
                    count={selectedQuestions.length}
                    page={page}
                    onChange={handleChange}
                    variant="outlined"
                    shape="rounded"
                    color="primary"
                    size="large"
                    showFirstButton
                    showLastButton
                    sx={{
                        "& .MuiPaginationItem-root": {
                            borderRadius: 2,
                            fontWeight: 500,
                            "&.Mui-selected": {
                                fontWeight: 600,
                            },
                        },
                    }}
                />
            </Box>
        </Box>
    );
}