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 209 210 211 212 | 25x 48x 25x 25x 1x 25x 25x 2x 25x 2x 25x 25x 2x 23x 17x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 2x 2x | import { useState } from "react";
import Typography from "@mui/material/Typography";
import Pagination from "@mui/material/Pagination";
import { Box, Button, Chip, Divider, 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 "../question-list/multiple-choice/MultipleChoiceOption";
import CheckboxOption from "../question-list/checkbox/CheckboxOption";
import FreeResponse from "../question-list/free-response/FreeResponse";
import GraphOption from "../question-list/graph/GraphOption";
import DropDownMenu from "../question-list/drop-down-menu/DropDownMenu";
import DragAndDrop from "../question-list/drag-and-drop/DragAndDrop";
import EmbeddedMultipleChoice from "../question-list/embedded-multiple-choice/EmbeddedMultipleChoice";
import SingleStimulus from "../question-list/single-stimulus/SingleStimulus";
import MultiPartQuestion from "../question-list/multi-part-question/MultiPartQuestion";
import GridQuestion from "../question-list/grid-question/GridQuestion";
import InteractiveDots from "../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 { assignmentQuestions, myAnswers, settings } = useSelector((state) => state.StudentQuestionPage);
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={question._id} on EVERY type: the paginated layout renders one
// question into a single reused slot, so without a per-question key the
// component instance carries the previous question's local answer state
// (the EI-2906 cross-question bleed — not just drag-and-drop:
// multiple-choice / drop-down / graph init their state once on mount).
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;
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>
{assignmentQuestions[questionIndex]?.points}{" "}
{assignmentQuestions[questionIndex]?.points > 1 ? "Points" : "Point"}
</Typography>
<Chip
label={assignmentQuestions[questionIndex]?.questionType}
size="small"
variant="outlined"
color="primary"
sx={{ fontSize: "0.75rem", color: "primary.main" }}
/>
</Box>
<Typography>
Question {page} of {assignmentQuestions.length}
</Typography>
<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
<IconButton
size="small"
color={speaking ? "primary" : "default"}
// Modified by Allan Ninal — optional-chained to match every other
// read of this element in the file. It was the one unguarded
// access, so on an empty question list the read-aloud button was
// the single click that took the page down.
onClick={() => handleSpeakClick(assignmentQuestions[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(assignmentQuestions[questionIndex])}
</Box>
</Paper>
{/* Pagination */}
<Box
sx={{
display: "flex",
justifyContent: "center",
py: 2,
}}
>
<Pagination
count={assignmentQuestions.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>
);
}
|