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

100% Statements 20/20
94.11% Branches 16/17
100% Functions 12/12
100% Lines 19/19

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                                                                            12x     12x                               2x                                       12x               18x 18x 18x   18x 18x         18x 17x     18x                     18x 13x     18x   17x                                                                                                                                     1x                                                                     12x 12x                                    
import { useEffect, useMemo, useState } from "react";
import {
    Alert,
    AlertTitle,
    Box,
    Card,
    CardActions,
    CardContent,
    Chip,
    FormControl,
    MenuItem,
    Select,
    Skeleton,
    Stack,
    Typography,
} from "@mui/material";
import AppBar from "@mui/material/AppBar";
import Toolbar from "@mui/material/Toolbar";
import SchoolIcon from "@mui/icons-material/SchoolOutlined";
import BreadcrumbsComponent from "@/utils/components/breadcrumbs/dynamic-breadcrumbs/Breadcrumbs";
import { useDispatch, useSelector } from "react-redux";
import { _fetchAllClasses } from "@/store/slices/teacher/TeacherClasses";
import getCurrentSemesterYear from "@/utils/helpers/getCurrentSemesterYear";
// EI-1187 — PR #119 (feat/responsive-design) moved this page from page/ to
// components/ but dropped the semester-header wiring PR #101 originally
// added; this import re-instates it so the AppBar header reflects the
// user's semester filter instead of always showing the current real-world
// semester.
import { classesHeaderLabel } from "@/app/teacher-page/page/class-list/classesHeaderLabel";
import { filterClassesBySearch } from "@/app/teacher-page/components/class-list/filterClassesBySearch";
import SearchField from "@/utils/components/SearchField";
import ClassCards from "@/app/teacher-page/components/class-list/ClassCards";
import ButtonNewClass from "@/app/teacher-page/components/class-list/NewClass";
 
// Placeholder card mirroring ClassCards' banner + title + code + button shape
// (see @/app/teacher-page/components/class-list/ClassCards.jsx) so the
// skeleton doesn't jump/reflow once the real cards render.
function ClassCardSkeleton() {
    return (
        <Card
            elevation={0}
            sx={{ borderRadius: "12px", overflow: "hidden", border: (t) => `1px solid ${t.palette.divider}` }}
        >
            <Skeleton variant="rectangular" height={120} animation="wave" />
            <CardContent sx={{ pt: 2, pb: 1 }}>
                <Skeleton variant="text" width="70%" height={28} animation="wave" />
                <Skeleton variant="text" width="45%" height={20} animation="wave" sx={{ mb: 1.5 }} />
                <Skeleton variant="rounded" width={92} height={22} animation="wave" />
            </CardContent>
            <CardActions sx={{ px: 2.5, pb: 2.5, pt: 0 }}>
                <Skeleton variant="rounded" width="100%" height={32} animation="wave" />
            </CardActions>
        </Card>
    );
}
 
function ClassListSkeleton() {
    return (
        <Box sx={{ display: "flex", flexDirection: "column", gap: 3 }}>
            <Stack direction={{ xs: "column", sm: "row" }} flexWrap="wrap" gap={1.5} sx={{ mb: 0 }}>
                <Skeleton variant="rounded" width={200} height={40} animation="wave" />
                <Skeleton variant="rounded" width={140} height={40} animation="wave" />
                <Skeleton
                    variant="rounded"
                    animation="wave"
                    sx={{ flex: 1, minWidth: 200, maxWidth: 320 }}
                    height={40}
                />
            </Stack>
            <Box
                sx={{
                    display: "grid",
                    gridTemplateColumns: "repeat(auto-fill, minmax(268px, 1fr))",
                    gap: { xs: 1.5, sm: 2, md: 2.5 },
                }}
            >
                {Array.from({ length: 6 }).map((_, i) => (
                    <ClassCardSkeleton key={i} />
                ))}
            </Box>
        </Box>
    );
}
 
export default function ClassList() {
    const { classData, hasErrors, isLoading } = useSelector((state) => state.TeacherClasses);
    const { currentYear, currentSemester } = getCurrentSemesterYear();
    const dispatch = useDispatch();
 
    const [search, setSearch] = useState("");
    const [selectedSemester, setSelectedSemester] = useState("All");
 
    // The visible classes use the unit-tested `filterClassesBySearch` helper
    // (semester + free-text search); the chip count and the rendered cards
    // both derive from this one filtered list.
    const filtered = useMemo(
        () => (classData ? filterClassesBySearch(classData, search, selectedSemester) : []),
        [classData, search, selectedSemester]
    );
    const filteredCount = filtered.length;
 
    // Modified by Allan Ninal — pagination removed. The backend caps a teacher at ten
    // classes (see NewClass.jsx, which disables Create at that cap), and the page size
    // was ten, so a second page could never exist: the control, its page-size selector
    // and the page state behind them were all unreachable in practice.
    //
    // That also retires a real bug rather than fixing it. Filtering used to leave the
    // teacher on the LAST page of the new results instead of the first, because the
    // page was only clamped into range. With one page there is no page to be wrong.
 
    useEffect(() => {
        dispatch(_fetchAllClasses(""));
    }, [dispatch]);
 
    return (
        <Box
            sx={{ minHeight: "100vh", bgcolor: (t) => (t.palette.mode === "dark" ? "background.default" : "#f8f9fc") }}
        >
            <AppBar position="relative" color="primary" elevation={0}>
                <Toolbar>
                    <Typography variant="h6" fontWeight={700} id="teacher-classes-semester-and-year">
                        {classesHeaderLabel({ currentYear, currentSemester, selectedSemester })}
                    </Typography>
                </Toolbar>
            </AppBar>
 
            <BreadcrumbsComponent />
 
            {hasErrors && !isLoading && (
                <Alert severity="error" sx={{ borderRadius: 0 }}>
                    <AlertTitle>Error</AlertTitle>
                    {hasErrors}
                </Alert>
            )}
 
            <Box sx={{ p: { xs: 2, sm: 3, md: 4 } }}>
                {isLoading ? (
                    <ClassListSkeleton />
                ) : classData && classData.length === 0 ? (
                    <Box
                        sx={{
                            minHeight: "65vh",
                            display: "flex",
                            flexDirection: "column",
                            alignItems: "center",
                            justifyContent: "center",
                            gap: 2,
                        }}
                    >
                        <SchoolIcon sx={{ fontSize: 80, color: "text.disabled" }} />
                        <Typography variant="h6" fontWeight={700} color="text.secondary">
                            No classes yet
                        </Typography>
                        <Typography variant="body2" color="text.disabled" textAlign="center">
                            Create your first class to get started
                        </Typography>
                        <Box sx={{ width: 220, mt: 1 }}>
                            <ButtonNewClass />
                        </Box>
                    </Box>
                ) : (
                    classData && (
                        <Box sx={{ display: "flex", flexDirection: "column", gap: 3 }}>
                            <Stack
                                className="teacher-class-list-header"
                                direction={{ xs: "column", sm: "row" }}
                                alignItems={{ xs: "stretch", sm: "center" }}
                                justifyContent="space-between"
                                gap={1.5}
                            >
                                <Stack
                                    direction={{ xs: "column", sm: "row" }}
                                    alignItems={{ xs: "stretch", sm: "center" }}
                                    gap={1.5}
                                    sx={{ flex: 1 }}
                                >
                                    <Box sx={{ minWidth: { xs: "100%", sm: 200 } }}>
                                        <ButtonNewClass />
                                    </Box>
                                    <FormControl>
                                        <Select
                                            id="teacher-classes-page-select-semester"
                                            value={selectedSemester}
                                            onChange={(e) => setSelectedSemester(e.target.value)}
                                            displayEmpty
                                            size="small"
                                        >
                                            <MenuItem id="teacher-classes-page-select-item-all" value="All">
                                                All Semesters
                                            </MenuItem>
                                            <MenuItem id="teacher-classes-page-select-item-fall" value="Fall">
                                                Fall
                                            </MenuItem>
                                            <MenuItem id="teacher-classes-page-select-item-summer1" value="Summer 1">
                                                Summer 1
                                            </MenuItem>
                                            <MenuItem id="teacher-classes-page-select-item-summer2" value="Summer 2">
                                                Summer 2
                                            </MenuItem>
                                            <MenuItem id="teacher-classes-page-select-item-spring" value="Spring">
                                                Spring
                                            </MenuItem>
                                        </Select>
                                    </FormControl>
                                    <Box sx={{ flex: 1, minWidth: 200, maxWidth: { sm: 320 } }}>
                                        <SearchField
                                            search={search}
                                            setSearch={setSearch}
                                            placeHolder="Search classes by name, section, or code…"
                                        />
                                    </Box>
                                </Stack>
 
                                <Chip
                                    label={`${filteredCount} ${filteredCount === 1 ? "class" : "classes"}`}
                                    sx={{
                                        fontWeight: 700,
                                        color: "primary.main",
                                        bgcolor: (t) => `${t.palette.primary.main}12`,
                                        border: (t) => `1px solid ${t.palette.primary.main}30`,
                                        alignSelf: { xs: "flex-start", sm: "center" },
                                    }}
                                />
                            </Stack>
 
                            <Box sx={{ mt: 4, mb: 4 }}>
                                <ClassCards selectedSemester={selectedSemester} classList={filtered} search={search} />
                            </Box>
 
                            <Box sx={{ height: 24 }} />
                        </Box>
                    )
                )}
            </Box>
        </Box>
    );
}