All files / src/app/student-page/components/class-list ClassCards.jsx

91.42% Statements 32/35
87.87% Branches 29/33
83.33% Functions 10/12
90.62% Lines 29/32

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                                            1x   1x                                                             1x 64x 63x     1x 63x                   1x 64x 64x 64x 64x 64x 64x 64x   64x 64x   64x 2x       2x 1x       64x                 64x                                                                                                                                                                                                                                           1x                                                 1x 19x   19x       19x                 64x              
import { alpha, useTheme } from "@mui/material/styles";
import { Avatar, Box, Button, Card, CardActions, CardContent, Chip, Stack, Typography } from "@mui/material";
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
import PendingIcon from "@mui/icons-material/HourglassEmpty";
import BlockIcon from "@mui/icons-material/Block";
import RejectedIcon from "@mui/icons-material/Cancel";
import SearchOffIcon from "@mui/icons-material/SearchOff";
import DeletedIcon from "@mui/icons-material/DoNotDisturbOn";
import TagIcon from "@mui/icons-material/Tag";
import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded";
import { useNavigate } from "react-router-dom";
import { _fetchSpecificClassByCode } from "@/store/slices/student/StudentSpecificClass";
import { useDispatch, useSelector } from "react-redux";
import { ClassCardBanner } from "@/utils/components";
 
// The banner shows the teacher-uploaded cover photo (entry.class_photo) when
// present. Otherwise it falls back to a flat fill using the district's
// selected theme color (theme "primary", which follows the theme preset
// picker) via the shared ClassCardBanner, whose shape scatter is still
// unique per class code. Mirrors the teacher-side class card (see
// src/app/teacher-page/components/class-list/ClassCards.jsx) — read-only here,
// students can't edit the photo.
const ACCENT_COLOR_KEY = "primary";
 
const STATUS_CONFIG = {
    Enrolled: {
        label: "Enrolled",
        color: "success",
        icon: <CheckCircleIcon sx={{ fontSize: "14px !important" }} />,
        buttonLabel: "View Class",
        disabled: false,
    },
    Pending: {
        label: "Pending",
        color: "warning",
        icon: <PendingIcon sx={{ fontSize: "14px !important" }} />,
        buttonLabel: "Waiting For Approval",
        disabled: true,
    },
    Removed: {
        label: "Removed",
        color: "error",
        icon: <BlockIcon sx={{ fontSize: "14px !important" }} />,
        buttonLabel: "Removed From Class",
        disabled: true,
    },
    Rejected: {
        label: "Rejected",
        color: "error",
        icon: <RejectedIcon sx={{ fontSize: "14px !important" }} />,
        buttonLabel: "Enrollment Rejected",
        disabled: true,
    },
};
 
const getEnrollment = (students, email) => {
    const student = students.find((s) => s.email === email);
    return STATUS_CONFIG[student?.status] ?? null;
};
 
const StatusChip = ({ color, icon, label }) => (
    <Chip
        variant="soft"
        color={color}
        size="small"
        icon={icon}
        label={label}
        sx={{ fontWeight: 600, fontSize: "0.7rem" }}
    />
);
 
const ClassCard = ({ entry, index, studentEmail }) => {
    const theme = useTheme();
    const navigate = useNavigate();
    const dispatch = useDispatch();
    const isDeleted = entry.deleted === true;
    const enrollment = isDeleted ? null : getEnrollment(entry.students, studentEmail);
    const viewDisabled = isDeleted || (enrollment ? enrollment.disabled : true);
    const semLabel = entry.semester.charAt(0).toUpperCase() + entry.semester.slice(1);
 
    const accent = theme.palette[ACCENT_COLOR_KEY];
    const glowShadow = theme.customShadows?.[ACCENT_COLOR_KEY] ?? theme.customShadows?.z16;
 
    const handleView = () => {
        dispatch(_fetchSpecificClassByCode(entry.class_code)).then((response) => {
            // Modified by Allan Ninal — `response.payload` is truthy for a REJECTED
            // action too (it carries the error body), so this navigated into a class
            // page whose fetch had just failed.
            if (!_fetchSpecificClassByCode.fulfilled.match(response)) return;
            navigate(`/student/classes/${entry.class_code}`);
        });
    };
 
    return (
        <Card
            elevation={0}
            sx={{
                borderRadius: "12px",
                overflow: "hidden",
                display: "flex",
                flexDirection: "column",
                height: "100%",
                border: (t) => `1px solid ${alpha(t.palette.grey[500], 0.14)}`,
                transition: theme.transitions.create(["transform", "box-shadow"], { duration: 220 }),
                "&:hover": { transform: "translateY(-4px)", boxShadow: glowShadow },
            }}
        >
            {entry.class_photo ? (
                <Box sx={{ position: "relative", height: 120 }}>
                    <Box
                        component="img"
                        src={entry.class_photo}
                        alt={`${entry.title} cover`}
                        sx={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }}
                    />
                    <Chip
                        label={semLabel}
                        size="small"
                        sx={{
                            position: "absolute",
                            top: 12,
                            right: 12,
                            bgcolor: (t) => alpha(t.palette.background.paper, 0.92),
                            color: "text.primary",
                            fontWeight: 700,
                            fontSize: "0.64rem",
                            height: 22,
                            backdropFilter: "blur(4px)",
                        }}
                    />
                </Box>
            ) : (
                <ClassCardBanner
                    seed={entry.class_code || entry.title || ""}
                    accentMain={accent.main}
                    label={semLabel}
                    ariaLabel={`${entry.title} cover`}
                />
            )}
 
            <CardContent sx={{ flex: 1, pt: 2, pb: 1 }}>
                <Typography
                    variant="subtitle1"
                    fontWeight={700}
                    noWrap
                    title={entry.title}
                    sx={{ fontFamily: theme.typography.fontSecondaryFamily, mb: 0.25 }}
                >
                    {entry.title}
                    <Typography component="span" variant="inherit" color="text.secondary" fontWeight={400}>
                        {" "}
                        (Section {entry.section})
                    </Typography>
                </Typography>
 
                <Stack direction="row" alignItems="center" spacing={0.4} sx={{ mb: 1.5, color: "text.secondary" }}>
                    <TagIcon sx={{ fontSize: 13 }} />
                    <Typography variant="caption" sx={{ fontFamily: "monospace" }}>
                        {entry.class_code}
                    </Typography>
                </Stack>
 
                <Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 1.5 }}>
                    {isDeleted ? (
                        <StatusChip
                            color="error"
                            icon={<DeletedIcon sx={{ fontSize: "14px !important" }} />}
                            label="Unavailable"
                        />
                    ) : (
                        enrollment && (
                            <StatusChip color={enrollment.color} icon={enrollment.icon} label={enrollment.label} />
                        )
                    )}
                </Stack>
 
                <Stack direction="row" spacing={1} alignItems="center">
                    <Avatar sx={{ width: 28, height: 28, bgcolor: accent.main, fontSize: "0.75rem" }}>
                        {entry.teacher.first_name[0]}
                        {entry.teacher.last_name[0]}
                    </Avatar>
                    <Box>
                        <Typography variant="body2" fontWeight={500}>
                            {`${entry.teacher.first_name} ${entry.teacher.last_name}`}
                        </Typography>
                        <Typography variant="caption" color="text.secondary">
                            Instructor
                        </Typography>
                    </Box>
                </Stack>
            </CardContent>
 
            <CardActions sx={{ px: 2.5, pb: 2.5, pt: 0 }}>
                <Button
                    id={`student-classes-card-button-view-${index}`}
                    variant="contained"
                    disableElevation
                    size="small"
                    fullWidth
                    disabled={viewDisabled}
                    endIcon={!viewDisabled && <ArrowForwardRoundedIcon sx={{ fontSize: 16 }} />}
                    onClick={handleView}
                    sx={{
                        textTransform: "none",
                        fontWeight: 700,
                        borderRadius: "8px",
                        ...(!viewDisabled && {
                            bgcolor: accent.main,
                            color: accent.contrastText,
                            "&:hover": { bgcolor: accent.dark },
                        }),
                    }}
                >
                    {isDeleted ? "No Longer Available" : (enrollment?.buttonLabel ?? "View Class")}
                </Button>
            </CardActions>
        </Card>
    );
};
 
const EmptyState = () => (
    <Box sx={{ py: 8, display: "flex", flexDirection: "column", alignItems: "center", gap: 1.5 }}>
        <Box
            sx={{
                width: 72,
                height: 72,
                borderRadius: "50%",
                backgroundColor: "action.hover",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                mb: 1,
            }}
        >
            <SearchOffIcon sx={{ fontSize: 36, color: "text.disabled" }} />
        </Box>
        <Typography variant="h6" fontWeight={600} color="text.secondary">
            No classes found
        </Typography>
        <Typography variant="body2" color="text.disabled" textAlign="center">
            Try adjusting your search, semester, or status filter
        </Typography>
    </Box>
);
 
const ClassCards = ({ classList }) => {
    const { studentData } = useSelector((state) => state.StudentData);
 
    Iif (!classList || classList.length === 0) {
        return <EmptyState />;
    }
 
    return (
        <Box
            sx={{
                display: "grid",
                gridTemplateColumns: "repeat(auto-fill, minmax(268px, 1fr))",
                gap: { xs: 1.5, sm: 2, md: 2.5 },
            }}
        >
            {classList.map((entry, index) => (
                <ClassCard key={entry.class_code} entry={entry} index={index} studentEmail={studentData.email} />
            ))}
        </Box>
    );
};
 
export default ClassCards;