All files / src/app/teacher-page/page/assignments ViewAssignmentPage.jsx

100% Statements 31/31
91.66% Branches 22/24
100% Functions 10/10
100% Lines 26/26

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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318                                                              1x 16x                                                           1x 16x               1x 11x 11x 11x 11x   11x 11x 11x         11x   11x           11x 11x 8x 8x       11x 3x         1x             8x 11x 12x   11x                                                                                                                                                                                                                                                                                                                                                                                   1x                              
import React, { useEffect } from "react";
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import QuestionsList from "@/app/teacher-page/components/assignments/QuestionsList";
import AssignmentDetails from "@/app/teacher-page/components/assignments/AssignmentDetails";
import AssignmentSettings from "@/app/teacher-page/components/assignments/AssignmentSettings";
import { Chip, Divider, Card, CardContent, Grid, Stack } from "@mui/material";
import BasicBreadcrumbs from "@/utils/components/breadcrumbs/basic-breadcrumbs/BasicBreadcrumbs";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { _fetchSpecificClassByCode } from "@/store/slices/teacher/TeacherSpecificClass";
import { _teacherFetchSpecificAssignment } from "@/store/slices/teacher/TeacherSpecificAssignment";
import SimpleLoading from "@/utils/components/SimpleLoading";
import AssignmentViewAsStudent from "@/app/teacher-page/page/assignments/new-assignment/student-preview/StudentPreviewPage.jsx";
import { utcToLocalTime } from "@/utils/helpers/utcToLocalTime";
import { getAssignmentStatus } from "@/utils/helpers/assignmentAvailability";
import AssignmentIcon from "@mui/icons-material/Assignment";
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
import TuneIcon from "@mui/icons-material/Tune";
import BoltIcon from "@mui/icons-material/Bolt";
import QuizIcon from "@mui/icons-material/Quiz";
import EventAvailableIcon from "@mui/icons-material/EventAvailable";
import RepeatIcon from "@mui/icons-material/Repeat";
import GradeIcon from "@mui/icons-material/Grade";
import SoftChip from "@/utils/components/feature-access/SoftChip";
 
/** One "at a glance" figure in the hero card — mirrors the student
    view-assignment page's StatTile so both pages read the same way. */
const StatTile = ({ icon, label, value, testId }) => (
    <Stack direction="row" spacing={1.25} alignItems="center" sx={{ minWidth: 160 }}>
        <Box
            sx={{
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                width: 40,
                height: 40,
                borderRadius: "50%",
                bgcolor: "primary.lighter",
                color: "primary.main",
                flexShrink: 0,
            }}
        >
            {icon}
        </Box>
        <Box>
            <Typography variant="caption" color="text.secondary" sx={{ display: "block", lineHeight: 1.2 }}>
                {label}
            </Typography>
            <Typography variant="subtitle1" fontWeight={700} data-testid={testId} sx={{ lineHeight: 1.3 }}>
                {value}
            </Typography>
        </Box>
    </Stack>
);
 
/** Icon + title header used by every card below — Details, Questions, Actions,
    and Settings are visually identical units so the eye doesn't have to learn
    four different layouts for four related sections. */
const SectionHeader = ({ icon, title }) => (
    <Box sx={{ display: "flex", alignItems: "center", mb: 2 }}>
        {icon}
        <Typography variant="h6" fontWeight={600}>
            {title}
        </Typography>
    </Box>
);
 
const ViewAssignment = () => {
    const { specificClassData } = useSelector((state) => state.TeacherSpecificClass);
    const { assignmentData, isLoading } = useSelector((state) => state.TeacherSpecificAssignment);
    const teacherCreateAssignment = useSelector((state) => state.TeacherCreateAssignment);
    const { classCode, assignmentTitle } = useParams();
 
    const dispatch = useDispatch();
    const location = useLocation();
    const navigate = useNavigate();
 
    // Guard: location.state may be null on a direct deep-link or browser refresh —
    // dispatching the fetch with `undefined` used to throw straight out of the
    // effect (location.state.assignmentUUID on a null state) and blank the page.
    const assignmentUUID = location.state?.assignmentUUID;
 
    const breadcrumb = [
        { name: "Classes", isLink: true, path: "/teacher/classes" },
        { name: specificClassData?.title, isLink: true, path: `/teacher/classes/${classCode}` },
        { name: assignmentTitle, isLink: false },
    ];
 
    useEffect(() => {
        if (!assignmentUUID) return;
        dispatch(_fetchSpecificClassByCode(classCode));
        dispatch(_teacherFetchSpecificAssignment(assignmentUUID));
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [assignmentUUID, classCode]);
 
    if (!assignmentUUID) {
        return (
            <Box sx={{ p: 4, textAlign: "center" }}>
                <Typography variant="h6" color="text.secondary">
                    Please open this assignment from your class page.
                </Typography>
                <Button sx={{ mt: 2 }} variant="outlined" onClick={() => navigate(`/teacher/classes/${classCode}`)}>
                    Go to Class
                </Button>
            </Box>
        );
    }
 
    const details = assignmentData?.details;
    const status = details ? getAssignmentStatus(details.date_open, details.date_close) : null;
    const totalPoints = assignmentData?.questions?.reduce((sum, q) => sum + (Number(q.points) || 0), 0) || 0;
 
    return (
        <React.Fragment>
            <SimpleLoading showLoading={isLoading} />
            <Box>
                {teacherCreateAssignment?.assignmentData?.viewAsStudent ? (
                    <AssignmentViewAsStudent />
                ) : (
                    !isLoading && (
                        <React.Fragment>
                            <AppBar color="primary" position="relative" elevation={0}>
                                <Toolbar>
                                    <AssignmentIcon sx={{ mr: 2 }} />
                                    <Typography variant="h6" fontWeight={600}>
                                        View Class Assignment
                                    </Typography>
                                </Toolbar>
                            </AppBar>
                            <BasicBreadcrumbs links={breadcrumb} />
 
                            {details ? (
                                <Box sx={{ maxWidth: 1100, mx: "auto", p: { xs: 2, md: 4 }, mb: 8 }}>
                                    {/* Hero: title, status, and the handful of facts a teacher scans for
                                        first — one place to look, instead of the title in one card and
                                        the schedule facts only discoverable further down the page. */}
                                    <Card elevation={3} sx={{ mb: 3 }}>
                                        <CardContent sx={{ p: { xs: 3, md: 4 } }}>
                                            <Stack spacing={3}>
                                                <Box
                                                    sx={{
                                                        display: "flex",
                                                        justifyContent: "space-between",
                                                        alignItems: "flex-start",
                                                        flexWrap: "wrap",
                                                        gap: 2,
                                                    }}
                                                >
                                                    <Box sx={{ flex: 1, minWidth: 250 }}>
                                                        <Stack
                                                            direction="row"
                                                            spacing={1.5}
                                                            alignItems="center"
                                                            flexWrap="wrap"
                                                            useFlexGap
                                                            sx={{ mb: 1 }}
                                                        >
                                                            <Typography variant="h4" fontWeight={700}>
                                                                {details.title}
                                                            </Typography>
                                                            {status && (
                                                                <Chip
                                                                    label={status.label}
                                                                    color={status.color}
                                                                    sx={{ fontWeight: 600 }}
                                                                />
                                                            )}
                                                            <SoftChip label={details.type} color="accent" size="sm" />
                                                        </Stack>
                                                        <Typography variant="body1" color="text.secondary">
                                                            {details.description || "No description provided"}
                                                        </Typography>
                                                    </Box>
                                                </Box>
 
                                                <Divider />
 
                                                {/* At a glance: the facts a teacher actually needs before
                                                    diving into the full Details card below. */}
                                                <Stack direction="row" spacing={3} flexWrap="wrap" useFlexGap>
                                                    <StatTile
                                                        icon={<QuizIcon />}
                                                        label="Questions"
                                                        value={assignmentData.questions?.length || 0}
                                                        testId="assignment-total-questions"
                                                    />
                                                    <StatTile
                                                        icon={<GradeIcon />}
                                                        label="Total Points"
                                                        value={totalPoints}
                                                    />
                                                    <StatTile
                                                        icon={<RepeatIcon />}
                                                        label="Attempts Allowed"
                                                        value={details.settings?.allowed_attempts || "Unlimited"}
                                                    />
                                                    <StatTile
                                                        icon={<EventAvailableIcon />}
                                                        label="Closes"
                                                        value={utcToLocalTime(details.date_close)}
                                                    />
                                                </Stack>
                                            </Stack>
                                        </CardContent>
                                    </Card>
 
                                    {/* Details, Actions, and Settings stacked as a left-hand rail;
                                        Questions on the right — same three cards as before, just aligned
                                        to each other in one column instead of spread across a row. */}
                                    <Grid container spacing={3}>
                                        <Grid size={{ xs: 12, md: 4 }}>
                                            <Stack spacing={3} sx={{ position: "sticky", top: 10 }}>
                                                <Card elevation={2}>
                                                    <CardContent>
                                                        <SectionHeader
                                                            icon={
                                                                <InfoOutlinedIcon
                                                                    sx={{
                                                                        fontSize: 30,
                                                                        color: "primary.main",
                                                                        mr: 1.5,
                                                                    }}
                                                                />
                                                            }
                                                            title="Details"
                                                        />
                                                        <Divider sx={{ mb: 1 }} />
                                                        <AssignmentDetails
                                                            assignmentData={assignmentData}
                                                            section="schedule"
                                                        />
                                                    </CardContent>
                                                </Card>
 
                                                <Card elevation={2}>
                                                    <CardContent>
                                                        <SectionHeader
                                                            icon={
                                                                <BoltIcon
                                                                    sx={{
                                                                        fontSize: 30,
                                                                        color: "primary.main",
                                                                        mr: 1.5,
                                                                    }}
                                                                />
                                                            }
                                                            title="Actions"
                                                        />
                                                        <Divider sx={{ mb: 2 }} />
                                                        <AssignmentSettings />
                                                    </CardContent>
                                                </Card>
 
                                                <Card elevation={2}>
                                                    <CardContent>
                                                        <SectionHeader
                                                            icon={
                                                                <TuneIcon
                                                                    sx={{
                                                                        fontSize: 30,
                                                                        color: "primary.main",
                                                                        mr: 1.5,
                                                                    }}
                                                                />
                                                            }
                                                            title="Settings"
                                                        />
                                                        <Divider sx={{ mb: 1 }} />
                                                        <AssignmentDetails
                                                            assignmentData={assignmentData}
                                                            section="settings"
                                                        />
                                                    </CardContent>
                                                </Card>
                                            </Stack>
                                        </Grid>
 
                                        <Grid size={{ xs: 12, md: 8 }}>
                                            <SectionHeader
                                                icon={
                                                    <QuizIcon sx={{ fontSize: 30, color: "primary.main", mr: 1.5 }} />
                                                }
                                                title="Questions"
                                            />
                                            <Divider sx={{ mb: 2 }} />
                                            <QuestionsList assignmentData={assignmentData} showHeader={false} />
                                        </Grid>
                                    </Grid>
                                </Box>
                            ) : (
                                <Box sx={{ maxWidth: 600, mx: "auto", mt: 6, textAlign: "center" }}>
                                    <AssignmentIcon sx={{ fontSize: 48, color: "text.disabled", mb: 1 }} />
                                    <Typography variant="h6" color="text.secondary">
                                        This assignment could not be found.
                                    </Typography>
                                    <Button
                                        sx={{ mt: 2 }}
                                        variant="outlined"
                                        onClick={() => navigate(`/teacher/classes/${classCode}`)}
                                    >
                                        Go to Class
                                    </Button>
                                </Box>
                            )}
                        </React.Fragment>
                    )
                )}
            </Box>
        </React.Fragment>
    );
};
 
export default ViewAssignment;