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

86.48% Statements 32/37
63.63% Branches 14/22
76.47% Functions 13/17
87.87% Lines 29/33

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                                                                            1x 1x     6x 6x               6x                                                           6x 6x 11x 11x 6x   6x 6x 6x   6x                 6x                                                                                                                     12x   12x                                                                                                                                                         2x                                   6x 6x         6x 6x       6x 2x     2x 1x       6x   6x                 6x          
import { useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { alpha, useTheme } from "@mui/material/styles";
import {
    Box,
    Button,
    Card,
    CardActions,
    CardContent,
    Chip,
    IconButton,
    Stack,
    Tooltip,
    Typography,
} from "@mui/material";
import PeopleIcon from "@mui/icons-material/People";
import HourglassEmptyIcon from "@mui/icons-material/HourglassEmpty";
import TagIcon from "@mui/icons-material/Tag";
import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded";
import CameraAltIcon from "@mui/icons-material/CameraAlt";
import SearchOffIcon from "@mui/icons-material/SearchOff";
import { ClassCardBanner } from "@/utils/components";
import { seededRandom } from "@/utils/components/class-card-banner/generateClassCardShapes";
 
import { _fetchSpecificClassByCode } from "@/store/slices/teacher/TeacherSpecificClass";
import { filterClassesBySearch } from "@/app/teacher-page/components/class-list/filterClassesBySearch";
import ClassPhotoDialog from "./ClassPhotoDialog";
 
// The banner falls back to a flat fill via the shared ClassCardBanner, whose
// shape scatter is already unique per class code — used only when the class
// has no uploaded cover photo (entry.class_photo). Every card used to fill
// with the exact same theme.palette.primary.main, which made the class grid
// read as identical tiles. The fill now rotates through the theme's own blue
// shades (primary + secondary, light/main/dark), seeded the same way as the
// shapes so a given class always gets the same shade across reloads — still
// on-brand, just not a single flat repeat. The "View Class" button keeps the
// theme's actual primary color — that's a CTA, not decoration.
const ACCENT_COLOR_KEY = "primary";
const BANNER_HEIGHT = 120;
 
function bannerAccentFor(seed, theme) {
    const rand = seededRandom(`${seed || "class"}-accent`);
    const shades = [
        theme.palette.primary.light,
        theme.palette.primary.main,
        theme.palette.primary.dark,
        theme.palette.secondary.light,
        theme.palette.secondary.main,
        theme.palette.secondary.dark,
    ];
    return shades[Math.floor(rand() * shades.length)];
}
 
function EmptyState() {
    return (
        <Box sx={{ py: 10, display: "flex", flexDirection: "column", alignItems: "center", gap: 1.5 }}>
            <Box
                sx={{
                    width: 72,
                    height: 72,
                    borderRadius: "50%",
                    bgcolor: "action.hover",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                }}
            >
                <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 semester filter
            </Typography>
        </Box>
    );
}
 
function ClassCard({ entry, index, onView }) {
    const theme = useTheme();
    const [photoDialogOpen, setPhotoDialogOpen] = useState(false);
    const enrolled = entry.students?.filter((s) => s.status === "Enrolled").length ?? 0;
    const pending = entry.students?.filter((s) => s.status === "Pending").length ?? 0;
    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 bannerAccent = bannerAccentFor(entry.class_code || entry.title || "", theme);
 
    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 },
            }}
        >
            <Box
                sx={{
                    position: "relative",
                    "&:hover .teacher-class-card-photo-edit-btn": { opacity: 1 },
                }}
            >
                {entry.class_photo ? (
                    <Box sx={{ position: "relative", height: BANNER_HEIGHT }}>
                        <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"
                            id={`teacher-classes-card-semester-${index}`}
                            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={bannerAccent}
                        label={semLabel}
                        labelId={`teacher-classes-card-semester-${index}`}
                        ariaLabel={`${entry.title} cover`}
                        height={BANNER_HEIGHT}
                    />
                )}
 
                <Tooltip title={entry.class_photo ? "Change cover photo" : "Add cover photo"} arrow>
                    <IconButton
                        id={`teacher-classes-card-photo-edit-${index}`}
                        className="teacher-class-card-photo-edit-btn"
                        size="small"
                        onClick={() => setPhotoDialogOpen(true)}
                        sx={{
                            position: "absolute",
                            top: 8,
                            left: 8,
                            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>
            </Box>
 
            <ClassPhotoDialog
                open={photoDialogOpen}
                onClose={() => setPhotoDialogOpen(false)}
                classUUID={entry._id}
                currentPhotoUrl={entry.class_photo}
            />
 
            <CardContent sx={{ flex: 1, pt: 2, pb: 1 }}>
                <Typography
                    id={`teacher-classes-card-title-${index}`}
                    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}>
                        {" "}
                        ({entry.section})
                    </Typography>
                </Typography>
 
                <Stack
                    id={`teacher-classes-card-code-${index}`}
                    direction="row"
                    alignItems="center"
                    spacing={0.4}
                    sx={{ mb: 1.5, color: "text.secondary" }}
                >
                    <TagIcon sx={{ fontSize: 13 }} />
                    <Typography variant="subtitle1" sx={{ fontFamily: "monospace" }}>
                        {entry.class_code}
                    </Typography>
                </Stack>
 
                <Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
                    <Tooltip title="Enrolled students" arrow>
                        <Chip
                            variant="soft"
                            color="success"
                            size="small"
                            icon={<PeopleIcon sx={{ fontSize: "14px !important" }} />}
                            label={<span id="teacher-class-card-accepted-students">{enrolled} Enrolled</span>}
                            sx={{ fontWeight: 600, fontSize: "0.7rem" }}
                        />
                    </Tooltip>
 
                    <Tooltip title="Pending students" arrow>
                        <Chip
                            variant="soft"
                            color="warning"
                            size="small"
                            icon={<HourglassEmptyIcon sx={{ fontSize: "14px !important" }} />}
                            label={<span id="teacher-class-card-pending-students">{pending} Pending</span>}
                            sx={{ fontWeight: 600, fontSize: "0.7rem" }}
                        />
                    </Tooltip>
                </Stack>
            </CardContent>
 
            <CardActions sx={{ px: 2.5, pb: 2.5, pt: 0 }}>
                <Button
                    id={`teacher-classes-card-button-view-${index}`}
                    variant="contained"
                    disableElevation
                    size="small"
                    fullWidth
                    endIcon={<ArrowForwardRoundedIcon sx={{ fontSize: 16 }} />}
                    onClick={() => onView(entry.class_code)}
                    sx={{
                        textTransform: "none",
                        fontWeight: 700,
                        borderRadius: "8px",
                        bgcolor: accent.main,
                        color: accent.contrastText,
                        "&:hover": { bgcolor: accent.dark },
                    }}
                >
                    View Class
                </Button>
            </CardActions>
        </Card>
    );
}
 
export default function ClassCards({ selectedSemester, search, classList }) {
    const navigate = useNavigate();
    const dispatch = useDispatch();
 
    // Single source of truth: the same unit-tested helper the chip count
    // uses (semester + free-text search, incl. the combined "title (section)"
    // card-title display) so the rendered cards and the count never diverge.
    const filtered = useMemo(
        () => filterClassesBySearch(classList, search, selectedSemester),
        [classList, selectedSemester, search]
    );
 
    const handleView = (classCode) => {
        dispatch(_fetchSpecificClassByCode(classCode)).then((res) => {
            // Modified by Allan Ninal — see the student card: a rejected action's
            // payload is the error body, so this opened a class that failed to load.
            if (!_fetchSpecificClassByCode.fulfilled.match(res)) return;
            navigate(`/teacher/classes/${classCode}`);
        });
    };
 
    Iif (filtered.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 },
            }}
        >
            {filtered.map((entry, idx) => (
                <ClassCard key={entry.class_code} entry={entry} index={idx} onView={handleView} />
            ))}
        </Box>
    );
}