EvalueTonSavoir/client/src/pages/Admin/Stats.tsx

116 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-03-16 00:52:22 -04:00
import React, { useState, useEffect } from "react";
2025-03-16 20:26:55 -04:00
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, IconButton, Grid, Typography, CircularProgress, Box } from "@mui/material";
2025-03-16 00:52:22 -04:00
import DeleteIcon from "@mui/icons-material/Delete";
import ApiService from '../../services/ApiService';
import { QuizTypeShort } from "../../Types/QuizType";
const Users: React.FC = () => {
const [quizzes, setQuizzes] = useState<QuizTypeShort[]>([]);
2025-03-16 20:26:55 -04:00
const [monthlyQuizzes, setMonthlyQuizzes] = useState(0);
const [totalUsers, setTotalUsers] = useState(0);
const [loading, setLoading] = useState(true);
2025-03-16 00:52:22 -04:00
useEffect(() => {
2025-03-16 20:26:55 -04:00
const fetchStats = async () => {
2025-03-16 00:52:22 -04:00
try {
2025-03-16 20:26:55 -04:00
const data = await ApiService.getStats();
setQuizzes(data.quizzes);
setTotalUsers(data.total);
const currentMonth = new Date().getMonth();
const currentYear = new Date().getFullYear();
const filteredQuizzes = data.quizzes.filter((quiz: QuizTypeShort) => {
const quizDate = new Date(quiz.created_at);
return quizDate.getMonth() === currentMonth && quizDate.getFullYear() === currentYear;
});
setMonthlyQuizzes(filteredQuizzes.length === 0 ? 10 : 0);
2025-03-16 00:52:22 -04:00
} catch (error) {
console.error("Error fetching quizzes:", error);
2025-03-16 20:26:55 -04:00
} finally {
setLoading(false);
2025-03-16 00:52:22 -04:00
}
};
2025-03-16 20:26:55 -04:00
fetchStats();
2025-03-16 00:52:22 -04:00
}, []);
const handleDelete = (id: string) => {
setQuizzes(quizzes.filter(quiz => quiz._id !== id));
};
2025-03-16 20:26:55 -04:00
const totalQuizzes = quizzes.length;
if (loading) {
return (
<Box display="flex" justifyContent="center" alignItems="center" height="100vh">
<CircularProgress size={80} thickness={5} />
</Box>
);
}
2025-03-16 00:52:22 -04:00
return (
2025-03-16 20:26:55 -04:00
<Paper className="p-4" sx={{ boxShadow: 'none' }}>
<Grid container spacing={8} justifyContent="center">
<Grid item>
<Typography variant="h6" align="center">Quiz du Mois</Typography>
<Box position="relative" display="inline-flex">
<CircularProgress variant="determinate" value={monthlyQuizzes === 0 ? 0 : monthlyQuizzes} size={80} thickness={5} />
<Box position="absolute" top={0} left={0} bottom={0} right={0} display="flex" alignItems="center" justifyContent="center">
<Typography variant="h6">{monthlyQuizzes}</Typography>
</Box>
</Box>
</Grid>
<Grid item>
<Typography variant="h6" align="center">Quiz total</Typography>
<Box position="relative" display="inline-flex">
<CircularProgress variant="determinate" value={totalQuizzes} size={80} thickness={5} />
<Box position="absolute" top={0} left={0} bottom={0} right={0} display="flex" alignItems="center" justifyContent="center">
<Typography variant="h6">{totalQuizzes}</Typography>
</Box>
</Box>
</Grid>
<Grid item>
<Typography variant="h6" align="center">Enseignants</Typography>
<Box position="relative" display="inline-flex">
<CircularProgress variant="determinate" value={totalUsers} size={80} thickness={5} />
<Box position="absolute" top={0} left={0} bottom={0} right={0} display="flex" alignItems="center" justifyContent="center">
<Typography variant="h6">{totalUsers}</Typography>
</Box>
</Box>
</Grid>
</Grid>
{/* Table */}
<TableContainer component={Paper} className="mt-4">
<Table>
<TableHead>
<TableRow>
<TableCell>Enseignant</TableCell>
<TableCell>Titre</TableCell>
<TableCell>Crée</TableCell>
<TableCell>Modifié</TableCell>
2025-03-16 00:52:22 -04:00
</TableRow>
2025-03-16 20:26:55 -04:00
</TableHead>
<TableBody>
{quizzes.map((quiz) => (
<TableRow key={quiz._id}>
2025-03-17 19:06:25 -04:00
<TableCell>{quiz.email}</TableCell>
2025-03-16 20:26:55 -04:00
<TableCell>{quiz.title}</TableCell>
<TableCell>{new Date(quiz.created_at).toLocaleDateString()}</TableCell>
<TableCell>{new Date(quiz.updated_at).toLocaleDateString()}</TableCell>
<TableCell>
<IconButton onClick={() => handleDelete(quiz._id)} color="error">
<DeleteIcon />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Paper>
2025-03-16 00:52:22 -04:00
);
};
export default Users;