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

90.47% Statements 57/63
81.25% Branches 39/48
100% Functions 17/17
90.56% Lines 48/53

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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440                                                                        1x 28x 28x 28x   28x 28x 28x   28x   28x 19x     28x 78x 36x 33x 18x   45x     28x 31x 18x 16x 6x 10x 5x 5x 4x     16x     28x 3x 3x                   3x   2x 2x 2x           28x 10x 12x 10x 4x           28x             1x     3x                                                                                                                                                                                                                                         52x                                                                                                                                                                                                                                                                                                       5x     5x 3x 2x 2x                               5x                                                                        
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { _searchClassByCode, _enrollClassByClassCode } from "@/store/slices/student/StudentSearchClass";
import { useNavigate, useParams } from "react-router-dom";
import {
    Alert,
    AppBar,
    Avatar,
    Box,
    Button,
    Card,
    CardContent,
    Chip,
    Container,
    Divider,
    Grid,
    Paper,
    Stack,
    Toolbar,
    Typography,
} from "@mui/material";
import ScheduleIcon from "@mui/icons-material/Schedule";
import PersonIcon from "@mui/icons-material/Person";
import ClassIcon from "@mui/icons-material/Class";
import CodeIcon from "@mui/icons-material/Code";
import DescriptionIcon from "@mui/icons-material/Description";
import CalendarIcon from "@mui/icons-material/CalendarMonth";
import EmailIcon from "@mui/icons-material/Email";
import TimeIcon from "@mui/icons-material/AccessTime";
import BreadcrumbsComponent from "@/utils/components/breadcrumbs/dynamic-breadcrumbs/Breadcrumbs";
import YesNoOption from "@/utils/components/dialogs/YesNoOption.jsx";
import SimpleLoading from "@/utils/components/SimpleLoading.jsx";
import { _fetchAllClasses } from "@/store/slices/student/StudentClasses.js";
import { useSnackbar } from "notistack";
 
const SearchClass = () => {
    const { studentData } = useSelector((state) => state.StudentData);
    const { classSearchData, isLoading, hasErrors } = useSelector((state) => state.StudentSearchClass);
    const { enqueueSnackbar } = useSnackbar();
 
    const dispatch = useDispatch();
    const navigate = useNavigate();
    const { classCode } = useParams();
 
    const [showYesNoOption, setShowYesNoOption] = useState(false);
 
    useEffect(() => {
        dispatch(_searchClassByCode(classCode));
    }, []);
 
    const isPendingOrEnrolled = (students) => {
        if (students.length !== 0) {
            const index = students.findIndex((student) => student.email === studentData.email);
            if (students[index]?.status === "Pending" || students[index]?.status === "Enrolled") return true;
            return false;
        }
        return false;
    };
 
    const getStudentStatusValue = (students) => {
        if (students.length !== 0) {
            const index = students.findIndex((student) => student.email == studentData.email);
            if (students[index]?.status === "Pending") {
                return "Waiting for Approval";
            } else if (students[index]?.status === "Removed") {
                return "Re-enroll";
            } else if (students[index]?.status === "Enrolled") {
                return "Already Enrolled";
            }
        }
        return "Enroll Class";
    };
 
    const handleYesOption = (classCode) => {
        setShowYesNoOption(false);
        dispatch(_enrollClassByClassCode(classCode)).then((response) => {
            // Modified by Allan Ninal — this told a student they had asked to join a
            // class when the request had failed.
            //
            // With rejectWithValue a rejected action's `payload` IS the error body, so
            // `if (response.payload)` was true on failure: the student saw "Successfully
            // waiting for approval", was taken away to their class list, and then waited
            // for an approval of a request that did not exist. The store already holds a
            // readable reason (#267) and this page renders it, so failing here simply
            // means not claiming success and not navigating away from the message.
            if (!_enrollClassByClassCode.fulfilled.match(response)) return;
 
            dispatch(_searchClassByCode(classCode));
            navigate("/student/classes");
            enqueueSnackbar("Successfully waiting for approval", {
                variant: "success",
            });
        });
    };
 
    const getStatusChipColor = (students) => {
        Eif (students.length !== 0) {
            const index = students.findIndex((student) => student.email === studentData.email);
            if (students[index]?.status === "Pending") return "warning";
            Eif (students[index]?.status === "Enrolled") return "success";
            if (students[index]?.status === "Removed") return "error";
        }
        return "default";
    };
 
    return (
        <>
            <YesNoOption
                title="Confirm Enrollment"
                message={classSearchData && `Do you want to enroll in ${classSearchData.title}?`}
                showDialog={showYesNoOption}
                clickNo={() => {
                    setShowYesNoOption(false);
                }}
                clickYes={() => {
                    handleYesOption(classSearchData.class_code);
                }}
            />
            <SimpleLoading showLoading={isLoading} />
 
            <Box>
                <AppBar position="relative" color="primary" elevation={0}>
                    <Toolbar>
                        <Typography variant="h6">Enroll in Class</Typography>
                    </Toolbar>
                </AppBar>
                <BreadcrumbsComponent />
 
                <Container maxWidth="lg" sx={{ py: 4 }}>
                    {hasErrors && (
                        <Alert severity="error" sx={{ mb: 3 }}>
                            {hasErrors}
                        </Alert>
                    )}
 
                    {classSearchData && (
                        <Grid container spacing={3}>
                            {/* Main Class Information Card */}
                            <Grid size={{ xs: 12, md: 8 }}>
                                <Card elevation={2}>
                                    <CardContent sx={{ p: 3 }}>
                                        <Stack spacing={3}>
                                            {/* Header */}
                                            <Box>
                                                <Typography variant="h4" gutterBottom fontWeight={600}>
                                                    {classSearchData.title}
                                                </Typography>
                                                <Stack
                                                    direction="row"
                                                    spacing={1}
                                                    sx={{ mt: 1 }}
                                                    flexWrap="wrap"
                                                    useFlexGap
                                                >
                                                    <Chip
                                                        icon={<CodeIcon />}
                                                        label={classSearchData.class_code}
                                                        color="primary"
                                                        variant="outlined"
                                                        sx={{
                                                            fontWeight: 500,
                                                            "& .MuiChip-label": {
                                                                color: "primary.main",
                                                            },
                                                            "& .MuiChip-icon": {
                                                                color: "primary.main",
                                                            },
                                                        }}
                                                    />
                                                    <Chip
                                                        label={`Section ${classSearchData.section}`}
                                                        color="secondary"
                                                        variant="outlined"
                                                        sx={{
                                                            fontWeight: 500,
                                                            "& .MuiChip-label": {
                                                                color: "secondary.main",
                                                            },
                                                        }}
                                                    />
                                                    <Chip
                                                        icon={<CalendarIcon />}
                                                        label={classSearchData.semester}
                                                        color="info"
                                                        variant="outlined"
                                                        sx={{
                                                            fontWeight: 500,
                                                            "& .MuiChip-label": {
                                                                color: "info.main",
                                                            },
                                                            "& .MuiChip-icon": {
                                                                color: "info.main",
                                                            },
                                                        }}
                                                    />
                                                </Stack>
                                            </Box>
 
                                            <Divider />
 
                                            {/* Description */}
                                            <Box>
                                                <Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 1 }}>
                                                    <DescriptionIcon color="action" />
                                                    <Typography variant="h6" fontWeight={500}>
                                                        Description
                                                    </Typography>
                                                </Stack>
                                                <Typography variant="body1" color="text.secondary" sx={{ pl: 4 }}>
                                                    {classSearchData.description || "No description provided"}
                                                </Typography>
                                            </Box>
 
                                            <Divider />
 
                                            {/* Class Schedules */}
                                            <Box>
                                                <Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 2 }}>
                                                    <ScheduleIcon color="action" />
                                                    <Typography variant="h6" fontWeight={500}>
                                                        Class Schedule
                                                    </Typography>
                                                </Stack>
 
                                                {!classSearchData.schedules ||
                                                classSearchData.schedules.length === 0 ? (
                                                    <Typography variant="body2" color="text.secondary" sx={{ pl: 4 }}>
                                                        No class schedules available
                                                    </Typography>
                                                ) : (
                                                    <Stack spacing={2} sx={{ pl: 4 }}>
                                                        {classSearchData.schedules.map((sched, index) => (
                                                            <Paper
                                                                key={index}
                                                                variant="outlined"
                                                                sx={{
                                                                    p: 2,
                                                                    borderLeft: 4,
                                                                    borderLeftColor: "primary.main",
                                                                }}
                                                            >
                                                                <Grid container spacing={2} alignItems="center">
                                                                    <Grid size={{ xs: 12, sm: 4 }}>
                                                                        <Typography
                                                                            variant="subtitle2"
                                                                            color="text.secondary"
                                                                        >
                                                                            Day
                                                                        </Typography>
                                                                        <Typography variant="body1" fontWeight={500}>
                                                                            {sched.day}
                                                                        </Typography>
                                                                    </Grid>
                                                                    <Grid size={{ xs: 6, sm: 4 }}>
                                                                        <Typography
                                                                            variant="subtitle2"
                                                                            color="text.secondary"
                                                                        >
                                                                            Start Time
                                                                        </Typography>
                                                                        <Stack
                                                                            direction="row"
                                                                            spacing={0.5}
                                                                            alignItems="center"
                                                                        >
                                                                            <TimeIcon fontSize="small" color="action" />
                                                                            <Typography variant="body1">
                                                                                {sched.time_start}
                                                                            </Typography>
                                                                        </Stack>
                                                                    </Grid>
                                                                    <Grid size={{ xs: 6, sm: 4 }}>
                                                                        <Typography
                                                                            variant="subtitle2"
                                                                            color="text.secondary"
                                                                        >
                                                                            End Time
                                                                        </Typography>
                                                                        <Stack
                                                                            direction="row"
                                                                            spacing={0.5}
                                                                            alignItems="center"
                                                                        >
                                                                            <TimeIcon fontSize="small" color="action" />
                                                                            <Typography variant="body1">
                                                                                {sched.time_end}
                                                                            </Typography>
                                                                        </Stack>
                                                                    </Grid>
                                                                </Grid>
                                                            </Paper>
                                                        ))}
                                                    </Stack>
                                                )}
                                            </Box>
                                        </Stack>
                                    </CardContent>
                                </Card>
                            </Grid>
 
                            {/* Sidebar - Teacher and Enrollment */}
                            <Grid size={{ xs: 12, md: 4 }}>
                                <Stack spacing={3}>
                                    {/* Teacher Information Card */}
                                    <Card elevation={2}>
                                        <CardContent sx={{ p: 3 }}>
                                            <Stack direction="row" spacing={1} alignItems="center" sx={{ mb: 2 }}>
                                                <PersonIcon color="action" />
                                                <Typography variant="h6" fontWeight={500}>
                                                    Instructor
                                                </Typography>
                                            </Stack>
 
                                            <Stack spacing={2} alignItems="center" sx={{ textAlign: "center" }}>
                                                <Avatar
                                                    sx={{
                                                        width: 80,
                                                        height: 80,
                                                        bgcolor: "primary.main",
                                                        color: "primary.contrastText",
                                                        fontSize: "2rem",
                                                    }}
                                                >
                                                    {classSearchData.teacher.first_name[0]}
                                                    {classSearchData.teacher.last_name[0]}
                                                </Avatar>
 
                                                <Box>
                                                    <Typography variant="h6" fontWeight={500}>
                                                        {`${classSearchData.teacher.first_name} ${
                                                            classSearchData.teacher.middle_name
                                                                ? classSearchData.teacher.middle_name + " "
                                                                : ""
                                                        }${classSearchData.teacher.last_name}`}
                                                    </Typography>
 
                                                    <Stack
                                                        direction="row"
                                                        spacing={0.5}
                                                        alignItems="center"
                                                        justifyContent="center"
                                                        sx={{ mt: 1 }}
                                                    >
                                                        <EmailIcon fontSize="small" color="action" />
                                                        <Typography variant="body2" color="text.secondary">
                                                            {classSearchData.teacher.email}
                                                        </Typography>
                                                    </Stack>
                                                </Box>
                                            </Stack>
                                        </CardContent>
                                    </Card>
 
                                    {/* Enrollment Action Card */}
                                    <Card
                                        elevation={2}
                                        // sx={{
                                        //     bgcolor: (theme) =>
                                        //         theme.palette.mode === "light"
                                        //             ? "grey.50"
                                        //             : "grey.900",
                                        // }}
                                    >
                                        <CardContent sx={{ p: 3 }}>
                                            <Typography variant="h6" fontWeight={500} gutterBottom>
                                                Enrollment Status
                                            </Typography>
 
                                            {isPendingOrEnrolled(classSearchData.students) && (
                                                <Chip
                                                    label={getStudentStatusValue(classSearchData.students)}
                                                    color={getStatusChipColor(classSearchData.students)}
                                                    variant="outlined"
                                                    sx={{
                                                        mb: 2,
                                                        width: "100%",
                                                        fontWeight: 600,
                                                        borderWidth: 2,
                                                        "& .MuiChip-label": {
                                                            color: (theme) => {
                                                                const chipColor = getStatusChipColor(
                                                                    classSearchData.students
                                                                );
                                                                if (chipColor === "warning")
                                                                    return theme.palette.warning.main;
                                                                Eif (chipColor === "success")
                                                                    return theme.palette.success.main;
                                                                if (chipColor === "error")
                                                                    return theme.palette.error.main;
                                                                return theme.palette.text.primary;
                                                            },
                                                        },
                                                    }}
                                                />
                                            )}
 
                                            <Button
                                                variant="contained"
                                                color="primary"
                                                fullWidth
                                                size="large"
                                                onClick={() => {
                                                    setShowYesNoOption(true);
                                                }}
                                                disabled={isPendingOrEnrolled(classSearchData.students)}
                                                sx={{
                                                    py: 1.5,
                                                    textTransform: "none",
                                                    fontSize: "1rem",
                                                    fontWeight: 600,
                                                }}
                                            >
                                                {getStudentStatusValue(classSearchData.students)}
                                            </Button>
 
                                            {!isPendingOrEnrolled(classSearchData.students) && (
                                                <Typography
                                                    variant="caption"
                                                    color="text.secondary"
                                                    sx={{ display: "block", mt: 1, textAlign: "center" }}
                                                >
                                                    Your enrollment is pending and subject to approval from the
                                                    instructor.
                                                </Typography>
                                            )}
                                        </CardContent>
                                    </Card>
                                </Stack>
                            </Grid>
                        </Grid>
                    )}
                </Container>
            </Box>
        </>
    );
};
 
export default SearchClass;