All files / src/app/student-page/page/dashboard/components ScoreDistributionCard.jsx

95.12% Statements 39/41
92.85% Branches 26/28
100% Functions 6/6
96.96% Lines 32/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                              1x 65x 52x 39x 26x 13x       1x 51x 51x 51x 51x   51x 17x 17x 17x 17x 17x 17x 15x 14x   2x 2x 2x 2x   17x     17x 17x 17x       51x   51x                                                 65x 65x                                                                                                    
import { useEffect, useState } from "react";
import axios from "axios";
import {
    Box,
    Card,
    CardContent,
    CircularProgress,
    Stack,
    Typography,
    useTheme,
} from "@mui/material";
import BarChartIcon from "@mui/icons-material/BarChart";
import { API_ENDPOINTS } from "@/api/endpoints";
import { normalizeDistribution } from "./normalizeDistribution";
 
const _bucketColor = (theme, label) => {
    if (label === "<60") return theme.palette.error.main;
    if (label === "60-69") return theme.palette.warning.main;
    if (label === "70-79") return theme.palette.info.main;
    if (label === "80-89") return theme.palette.primary.main;
    Eif (label === "90-100") return theme.palette.success.main;
    return theme.palette.grey[500];
};
 
const ScoreDistributionCard = ({ refreshKey }) => {
    const theme = useTheme();
    const [data, setData] = useState({ buckets: [], total_graded: 0, max_count: 0 });
    const [isLoading, setIsLoading] = useState(false);
    const [hasError, setHasError] = useState(false);
 
    useEffect(() => {
        let cancelled = false;
        const fetchDistribution = async () => {
            setIsLoading(true);
            setHasError(false);
            try {
                const response = await axios.get(API_ENDPOINTS.student.dashboard.gradeDistribution);
                if (cancelled) return;
                setData(normalizeDistribution(response?.data));
            } catch (error) {
                Iif (cancelled) return;
                console.error("EI-2966 distribution fetch error:", error?.response?.data || error?.message);
                setHasError(true);
                setData({ buckets: [], total_graded: 0, max_count: 0 });
            } finally {
                if (!cancelled) setIsLoading(false);
            }
        };
        fetchDistribution();
        return () => {
            cancelled = true;
        };
    }, [refreshKey]);
 
    const { buckets, total_graded, max_count } = data;
 
    return (
        <Card id="student-dashboard-distribution-card" sx={{ borderRadius: 2 }}>
            <CardContent>
                <Stack direction="row" alignItems="center" spacing={1} sx={{ mb: 2 }}>
                    <BarChartIcon color="primary" />
                    <Typography variant="h6" sx={{ fontWeight: 600 }}>
                        Score Distribution
                    </Typography>
                </Stack>
 
                {isLoading ? (
                    <Box sx={{ py: 3, textAlign: "center" }}>
                        <CircularProgress size={24} />
                    </Box>
                ) : hasError ? (
                    <Typography id="student-dashboard-distribution-error" variant="body2" color="error">
                        Unable to load distribution. Please try again later.
                    </Typography>
                ) : total_graded === 0 ? (
                    <Typography id="student-dashboard-distribution-empty" variant="body2" color="text.secondary">
                        No graded submissions yet.
                    </Typography>
                ) : (
                    <Stack spacing={1.5} id="student-dashboard-distribution-bars">
                        {buckets.map((b) => {
                            const widthPct = max_count === 0 ? 0 : Math.round((b.count / max_count) * 100);
                            return (
                                <Box key={b.label} data-bucket={b.label}>
                                    <Stack
                                        direction="row"
                                        justifyContent="space-between"
                                        alignItems="center"
                                        sx={{ mb: 0.25 }}
                                    >
                                        <Typography variant="body2" sx={{ fontWeight: 500 }}>
                                            {b.label}
                                        </Typography>
                                        <Typography variant="caption" color="text.secondary">
                                            {b.count} ยท {b.percent}%
                                        </Typography>
                                    </Stack>
                                    <Box
                                        sx={{
                                            position: "relative",
                                            height: 8,
                                            borderRadius: 1,
                                            bgcolor: theme.palette.action.hover,
                                            overflow: "hidden",
                                        }}
                                    >
                                        <Box
                                            sx={{
                                                position: "absolute",
                                                top: 0,
                                                left: 0,
                                                bottom: 0,
                                                width: `${widthPct}%`,
                                                bgcolor: _bucketColor(theme, b.label),
                                                transition: "width 0.4s ease",
                                            }}
                                        />
                                    </Box>
                                </Box>
                            );
                        })}
                        <Typography variant="caption" color="text.secondary" sx={{ mt: 1 }}>
                            {total_graded} graded submission{total_graded === 1 ? "" : "s"}
                        </Typography>
                    </Stack>
                )}
            </CardContent>
        </Card>
    );
};
 
export default ScoreDistributionCard;