All files / src/app/teacher-page/page/class-details ClassDetails.jsx

89.74% Statements 35/39
66.66% Branches 8/12
75% Functions 12/16
89.18% Lines 33/37

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                                            1x 62x               1x 62x                         1x 32x 32x 32x   32x 32x   32x 32x 32x   32x 3x                         32x 8x 8x 8x 6x 6x     2x 2x 2x 2x                 32x   31x   31x                                                                                                                                 43x   43x                           31x                                                                                                         10x                                                       31x                                                  
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import { useSnackbar } from "notistack";
import { alpha, useTheme } from "@mui/material/styles";
import { Box, Button, Chip, IconButton, Paper, Stack, Tooltip, Typography } from "@mui/material";
import CameraAltIcon from "@mui/icons-material/CameraAlt";
import ImageOutlinedIcon from "@mui/icons-material/ImageOutlined";
import EditIcon from "@mui/icons-material/Edit";
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import TagIcon from "@mui/icons-material/Tag";
import ArticleIcon from "@mui/icons-material/Article";
import CalendarMonthIcon from "@mui/icons-material/CalendarMonth";
import ScheduleIcon from "@mui/icons-material/Schedule";
import { formatDate } from "@/utils/helpers/utcToLocalTime";
import YesNoOption from "@/utils/components/dialogs/YesNoOption";
import { _fetchAllClasses } from "@/store/slices/teacher/TeacherClasses";
import { _deleteClass, setSpecificClassData } from "@/store/slices/teacher/TeacherSpecificClass";
import { messageFromRejection } from "@/utils/helpers/rejectionMessage";
import EditClass from "@/app/teacher-page/components/class-details/EditClass";
import ClassPhotoDialog from "@/app/teacher-page/components/class-list/ClassPhotoDialog";
 
const SectionCard = ({ title, children }) => (
    <Box sx={{ p: 2.5, borderRadius: 2, bgcolor: "background.paper", border: "1px solid", borderColor: "divider" }}>
        <Typography variant="subtitle2" fontWeight={600} sx={{ mb: 2 }}>
            {title}
        </Typography>
        {children}
    </Box>
);
 
const InfoRow = ({ icon, label, value }) => (
    <Stack direction="row" spacing={2} alignItems="flex-start">
        {icon}
        <Box sx={{ minWidth: 0 }}>
            <Typography variant="caption" color="text.secondary">
                {label}
            </Typography>
            <Typography variant="body2" fontWeight={500} sx={{ wordBreak: "break-word" }}>
                {value}
            </Typography>
        </Box>
    </Stack>
);
 
const ClassDetails = () => {
    const theme = useTheme();
    const { specificClassData } = useSelector((state) => state.TeacherSpecificClass);
    const { enqueueSnackbar } = useSnackbar();
 
    const dispatch = useDispatch();
    const navigate = useNavigate();
 
    const [openDeleteDialog, setOpenDeleteDialog] = useState(false);
    const [openUpdateDialog, setOpenUpdateDialog] = useState(false);
    const [openPhotoDialog, setOpenPhotoDialog] = useState(false);
 
    const handleCloseDeleteDialog = () => {
        setOpenDeleteDialog(false);
    };
 
    /**
     * Modified by Allan Ninal — this reported success when the delete had failed.
     *
     * With rejectWithValue a rejected action's `payload` IS the error body, so the
     * old `if (response.payload)` guard was TRUE on failure: the teacher was told
     * "Class successfully deleted", sent back to the class list, and left looking at
     * the class they had just been told was gone. Deleting a class is destructive
     * and irreversible from here, which makes a false success the worst answer —
     * the teacher stops looking.
     */
    const handleDeleteClass = () => {
        setOpenDeleteDialog(false);
        dispatch(_deleteClass(specificClassData._id)).then((response) => {
            if (!_deleteClass.fulfilled.match(response)) {
                enqueueSnackbar(messageFromRejection(response), { variant: "error" });
                return;
            }
 
            handleCloseDeleteDialog();
            navigate("/teacher/classes");
            dispatch(_fetchAllClasses());
            enqueueSnackbar("Class successfully deleted", {
                variant: "success",
            });
        });
    };
 
    // Modified by Allan Ninal — without this the page threw "Cannot read properties
    // of null (reading 'semester')" on every render before the class arrived. The
    // student twin has had this guard all along.
    if (!specificClassData) return null;
 
    const semesterLabel = specificClassData.semester.charAt(0).toUpperCase() + specificClassData.semester.slice(1);
 
    return (
        <>
            <EditClass showDialog={openUpdateDialog} setDialog={setOpenUpdateDialog} classDetails={specificClassData} />
            <ClassPhotoDialog
                open={openPhotoDialog}
                onClose={() => setOpenPhotoDialog(false)}
                classUUID={specificClassData._id}
                currentPhotoUrl={specificClassData.class_photo}
                onPhotoUpdated={(newPhotoUrl) =>
                    dispatch(setSpecificClassData({ ...specificClassData, class_photo: newPhotoUrl }))
                }
            />
            <YesNoOption
                title="Confirm Delete"
                message={`Do you want to delete this class ${specificClassData.title} (${specificClassData.section})?`}
                showDialog={openDeleteDialog}
                clickNo={handleCloseDeleteDialog}
                clickYes={handleDeleteClass}
            />
 
            <Stack spacing={2.5}>
                {/* Cover photo, title, and primary actions */}
                <Paper
                    elevation={0}
                    sx={{ borderRadius: 2, border: "1px solid", borderColor: "divider", overflow: "hidden" }}
                >
                    <Box
                        sx={{
                            position: "relative",
                            "&:hover .teacher-class-details-photo-edit-btn": { opacity: 1 },
                        }}
                    >
                        {specificClassData.class_photo ? (
                            <Box
                                component="img"
                                src={specificClassData.class_photo}
                                alt="Class cover"
                                sx={{ width: "100%", height: 200, objectFit: "cover", display: "block" }}
                            />
                        ) : (
                            <Box
                                sx={{
                                    width: "100%",
                                    height: 200,
                                    bgcolor: "action.hover",
                                    display: "flex",
                                    alignItems: "center",
                                    justifyContent: "center",
                                }}
                            >
                                <ImageOutlinedIcon sx={{ fontSize: 48, color: "action.disabled" }} />
                            </Box>
                        )}
 
                        <Tooltip title={specificClassData.class_photo ? "Change cover photo" : "Add cover photo"} arrow>
                            <IconButton
                                id="teacher-class-btn-change-photo"
                                className="teacher-class-details-photo-edit-btn"
                                onClick={() => setOpenPhotoDialog(true)}
                                sx={{
                                    position: "absolute",
                                    top: 12,
                                    left: 12,
                                    opacity: 0,
                                    transition: theme.transitions.create("opacity", { duration: 150 }),
                                    bgcolor: (t) => alpha(t.palette.common.black, 0.45),
                                    color: "#fff",
                                    "&:hover": { bgcolor: (t) => alpha(t.palette.common.black, 0.65) },
                                }}
                            >
                                <CameraAltIcon fontSize="small" />
                            </IconButton>
                        </Tooltip>
 
                        <Chip
                            label={semesterLabel}
                            size="small"
                            sx={{
                                position: "absolute",
                                top: 12,
                                right: 12,
                                bgcolor: (t) => alpha(t.palette.background.paper, 0.92),
                                color: "text.primary",
                                fontWeight: 700,
                                backdropFilter: "blur(4px)",
                            }}
                        />
                    </Box>
 
                    <Box sx={{ p: 2.5 }}>
                        <Stack
                            direction={{ xs: "column", sm: "row" }}
                            justifyContent="space-between"
                            alignItems={{ xs: "flex-start", sm: "center" }}
                            spacing={1.5}
                        >
                            <Box sx={{ minWidth: 0 }}>
                                <Typography variant="h6" fontWeight={700} sx={{ wordBreak: "break-word" }}>
                                    {specificClassData.title}{" "}
                                    <Typography
                                        component="span"
                                        variant="inherit"
                                        color="text.secondary"
                                        fontWeight={400}
                                    >
                                        ({specificClassData.section})
                                    </Typography>
                                </Typography>
                                <Stack
                                    direction="row"
                                    alignItems="center"
                                    spacing={0.5}
                                    sx={{ mt: 0.5, color: "text.secondary" }}
                                >
                                    <TagIcon sx={{ fontSize: 14 }} />
                                    <Typography variant="body2" sx={{ fontFamily: "monospace" }}>
                                        {specificClassData.class_code}
                                    </Typography>
                                </Stack>
                            </Box>
 
                            <Stack direction="row" spacing={1} flexShrink={0}>
                                <Button
                                    startIcon={<EditIcon />}
                                    variant="outlined"
                                    color="secondary"
                                    onClick={() => setOpenUpdateDialog(true)}
                                >
                                    Edit Class
                                </Button>
                                <Button
                                    startIcon={<DeleteOutlineIcon />}
                                    variant="outlined"
                                    color="error"
                                    onClick={() => setOpenDeleteDialog(true)}
                                >
                                    Delete Class
                                </Button>
                            </Stack>
                        </Stack>
                    </Box>
                </Paper>
 
                <SectionCard title="Class Information">
                    <Stack spacing={2}>
                        <InfoRow
                            icon={<ArticleIcon color="primary" />}
                            label="Description"
                            value={specificClassData.description || "(No description available)"}
                        />
                        <InfoRow
                            icon={<CalendarMonthIcon color="primary" />}
                            label="Date Created"
                            value={formatDate(specificClassData.created_at)}
                        />
                    </Stack>
                </SectionCard>
 
                <SectionCard title="Schedule">
                    {specificClassData.schedules.length > 0 ? (
                        <Stack spacing={1.5}>
                            {specificClassData.schedules.map((item, index) => (
                                <Stack key={index} direction="row" spacing={2} alignItems="center">
                                    <ScheduleIcon color="primary" />
                                    <Box>
                                        <Typography variant="body2" fontWeight={500}>
                                            {item.day}
                                        </Typography>
                                        <Typography variant="caption" color="text.secondary">
                                            {item.time_start} - {item.time_end}
                                        </Typography>
                                    </Box>
                                </Stack>
                            ))}
                        </Stack>
                    ) : (
                        <Typography variant="body2" color="text.disabled">
                            No class schedules
                        </Typography>
                    )}
                </SectionCard>
            </Stack>
        </>
    );
};
 
export default ClassDetails;