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

86.48% Statements 32/37
100% Branches 8/8
75% Functions 12/16
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                                        1x           6x     6x                               1x                                         6x             1x 14x 14x 14x   14x   14x 14x 14x   14x 14x   14x 14x 14x         14x 8x         14x 9x     14x 9x                   14x 14x 14x 11x       14x                                                                                                                                                                                                                                          
import { useEffect, useMemo, useState } from "react";
import ClassCards from "@/app/student-page/components/class-list/ClassCards";
import SearchField from "@/app/student-page/components/class-list/SearchField";
import ButtonNewClass from "@/app/student-page/components/class-list/ButtonNewClass";
import { Alert, AlertTitle, Box, Card, CardActions, CardContent, 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 { FormControl, MenuItem, Select } from "@mui/material";
import { useDispatch, useSelector } from "react-redux";
import { _fetchAllClasses } from "@/store/slices/student/StudentClasses";
import getCurrentSemesterYear from "@/utils/helpers/getCurrentSemesterYear";
import CombinedPagination from "@/utils/components/pagination/CombinedPagination";
import { filterStudentClasses } from "./filterStudentClasses";
 
// The semester/status/search filters must run over the student's FULL class
// list, not just whatever page the server happens to return — so fetch a
// single, generously-sized page up front and do filtering + pagination
// client-side (mirrors the teacher class-list pattern).
const ALL_CLASSES_PAGE_SIZE = 1000;
 
// Placeholder card mirroring ClassCards' banner + title + code + button shape
// (see @/app/student-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={{ minHeight: "80vh" }}>
            <Stack direction={{ xs: "column", sm: "row" }} flexWrap="wrap" gap={2} sx={{ mb: 3 }}>
                <Skeleton variant="rounded" width={200} height={40} animation="wave" />
                <Skeleton variant="rounded" width={180} height={40} animation="wave" />
                <Skeleton variant="rounded" width={160} 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>
    );
}
 
const Main = () => {
    const { classData, hasErrors, isLoading } = useSelector((state) => state.StudentClasses);
    const { studentData } = useSelector((state) => state.StudentData);
    const { currentYear, currentSemester } = getCurrentSemesterYear();
 
    const dispatch = useDispatch();
 
    const [search, setSearch] = useState("");
    const [selectedSemester, setSelectedSemester] = useState("All");
    const [selectedStatus, setSelectedStatus] = useState("Enrolled");
 
    const [isPage, setPage] = useState(0);
    const [rowsPerPage, setRowsPerPage] = useState(10);
 
    const tablePageChange = (_, newPage) => setPage(newPage);
    const pageChange = (_, newPage) => setPage(newPage - 1);
    const tableRowsPageChange = (event) => {
        setRowsPerPage(parseInt(event.target.value, 10));
        setPage(0);
    };
 
    useEffect(() => {
        dispatch(_fetchAllClasses(`?page_num=1&page_size=${ALL_CLASSES_PAGE_SIZE}`));
    }, [dispatch]);
 
    // Reset to the first page whenever a filter changes so the user is never
    // stranded on a now-empty page from a previous, wider result set.
    useEffect(() => {
        setPage(0);
    }, [search, selectedSemester, selectedStatus]);
 
    const filteredList = useMemo(
        () =>
            filterStudentClasses(classData, {
                search,
                selectedSemester,
                selectedStatus,
                studentEmail: studentData?.email,
            }),
        [classData, search, selectedSemester, selectedStatus, studentData?.email]
    );
 
    const totalPages = Math.max(1, Math.ceil(filteredList.length / rowsPerPage));
    const currentPage = Math.min(isPage, totalPages - 1);
    const pagedList = useMemo(
        () => filteredList.slice(currentPage * rowsPerPage, currentPage * rowsPerPage + rowsPerPage),
        [filteredList, currentPage, rowsPerPage]
    );
 
    return (
        <Box>
            <AppBar position="relative" color="primary">
                <Toolbar>
                    <Typography variant="h6">{`${currentYear}-${currentSemester}`}</Typography>
                </Toolbar>
            </AppBar>
            <BreadcrumbsComponent />
            {hasErrors && (
                <Alert sx={{ borderRadius: 0 }} severity="error">
                    <AlertTitle>Error</AlertTitle>
                    {hasErrors}
                </Alert>
            )}
            <Box sx={{ p: { xs: 2, sm: 3 } }}>
                {isLoading ? (
                    <ClassListSkeleton />
                ) : (
                    classData &&
                    (classData.length === 0 ? (
                        <Box
                            sx={{
                                minHeight: "70vh",
                                display: "flex",
                                flexDirection: "column",
                                alignItems: "center",
                                justifyContent: "center",
                                gap: 2,
                            }}
                        >
                            <SchoolIcon sx={{ fontSize: 80, color: "text.disabled" }} />
                            <Typography variant="h6" color="text.secondary">
                                You haven&apos;t joined any classes yet
                            </Typography>
                            <Typography variant="body2" color="text.disabled" textAlign="center">
                                Enroll in a class using a class code from your teacher
                            </Typography>
                            <Box sx={{ width: 220, mt: 1 }}>
                                <ButtonNewClass />
                            </Box>
                        </Box>
                    ) : (
                        <Box
                            sx={{
                                minHeight: "80vh",
                                display: "flex",
                                flexDirection: "column",
                                justifyContent: "space-between",
                            }}
                        >
                            <Stack
                                direction={{ xs: "column", sm: "row" }}
                                alignItems={{ xs: "stretch", sm: "center" }}
                                flexWrap="wrap"
                                gap={2}
                                sx={{ mb: 3 }}
                            >
                                <Box sx={{ minWidth: 200 }}>
                                    <ButtonNewClass />
                                </Box>
                                <FormControl sx={{ minWidth: 180 }}>
                                    <Select
                                        value={selectedSemester}
                                        onChange={(e) => setSelectedSemester(e.target.value)}
                                        size="small"
                                    >
                                        <MenuItem value="All">All Semesters</MenuItem>
                                        <MenuItem value="Fall">Fall</MenuItem>
                                        <MenuItem value="Summer 1">Summer 1</MenuItem>
                                        <MenuItem value="Summer 2">Summer 2</MenuItem>
                                        <MenuItem value="Spring">Spring</MenuItem>
                                    </Select>
                                </FormControl>
                                <FormControl sx={{ minWidth: 160 }}>
                                    <Select
                                        value={selectedStatus}
                                        onChange={(e) => setSelectedStatus(e.target.value)}
                                        size="small"
                                    >
                                        <MenuItem value="All">All Classes</MenuItem>
                                        <MenuItem value="Enrolled">Enrolled</MenuItem>
                                        <MenuItem value="Pending">Pending</MenuItem>
                                        <MenuItem value="Removed">Removed</MenuItem>
                                        <MenuItem value="Rejected">Rejected</MenuItem>
                                        <MenuItem value="Deleted">Deleted</MenuItem>
                                    </Select>
                                </FormControl>
                                <Box sx={{ flex: 1, minWidth: 200, maxWidth: { sm: 320 } }}>
                                    <SearchField search={search} setSearch={setSearch} />
                                </Box>
                            </Stack>
                            <Box sx={{ mt: 4, mb: 4 }}>
                                <ClassCards classList={pagedList} />
                            </Box>
                            <CombinedPagination
                                pagination={{ overAllEntries: filteredList.length }}
                                basicPage={{
                                    isPage: currentPage,
                                    rowsPerPage,
                                    pageChange,
                                }}
                                tablePage={{
                                    isPage: currentPage,
                                    rowsPerPage,
                                    tablePageChange,
                                    tableRowsPageChange,
                                }}
                            />
                        </Box>
                    ))
                )}
            </Box>
        </Box>
    );
};
 
export default Main;