mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
added filters
This commit is contained in:
parent
be65a2b582
commit
abfebeb992
1 changed files with 61 additions and 5 deletions
|
|
@ -1,30 +1,39 @@
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, IconButton, Grid, Typography, CircularProgress, Box } from "@mui/material";
|
import {
|
||||||
|
Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper,
|
||||||
|
IconButton, Grid, Typography, CircularProgress, Box, TextField, Accordion, AccordionSummary, AccordionDetails
|
||||||
|
} from "@mui/material";
|
||||||
import DeleteIcon from "@mui/icons-material/Delete";
|
import DeleteIcon from "@mui/icons-material/Delete";
|
||||||
import ApiService from '../../services/ApiService';
|
import ApiService from '../../services/ApiService';
|
||||||
|
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
|
||||||
import { QuizTypeShort } from "../../Types/QuizType";
|
import { QuizTypeShort } from "../../Types/QuizType";
|
||||||
|
|
||||||
const Users: React.FC = () => {
|
const Users: React.FC = () => {
|
||||||
const [quizzes, setQuizzes] = useState<QuizTypeShort[]>([]);
|
const [quizzes, setQuizzes] = useState<QuizTypeShort[]>([]);
|
||||||
|
const [filteredQuizzes, setFilteredQuizzes] = useState<QuizTypeShort[]>([]);
|
||||||
const [monthlyQuizzes, setMonthlyQuizzes] = useState(0);
|
const [monthlyQuizzes, setMonthlyQuizzes] = useState(0);
|
||||||
const [totalUsers, setTotalUsers] = useState(0);
|
const [totalUsers, setTotalUsers] = useState(0);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [emailFilter, setEmailFilter] = useState("");
|
||||||
|
const [dateFilter, setDateFilter] = useState("");
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchStats = async () => {
|
const fetchStats = async () => {
|
||||||
try {
|
try {
|
||||||
const data = await ApiService.getStats();
|
const data = await ApiService.getStats();
|
||||||
setQuizzes(data.quizzes);
|
setQuizzes(data.quizzes);
|
||||||
|
setFilteredQuizzes(data.quizzes);
|
||||||
setTotalUsers(data.total);
|
setTotalUsers(data.total);
|
||||||
|
|
||||||
const currentMonth = new Date().getMonth();
|
const currentMonth = new Date().getMonth();
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
const filteredQuizzes = data.quizzes.filter((quiz: QuizTypeShort) => {
|
const filteredMonthlyQuizzes = data.quizzes.filter((quiz: QuizTypeShort) => {
|
||||||
const quizDate = new Date(quiz.created_at);
|
const quizDate = new Date(quiz.created_at);
|
||||||
return quizDate.getMonth() === currentMonth && quizDate.getFullYear() === currentYear;
|
return quizDate.getMonth() === currentMonth && quizDate.getFullYear() === currentYear;
|
||||||
});
|
});
|
||||||
|
|
||||||
setMonthlyQuizzes(filteredQuizzes.length === 0 ? 10 : 0);
|
setMonthlyQuizzes(filteredMonthlyQuizzes.length === 0 ? 10 : 0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching quizzes:", error);
|
console.error("Error fetching quizzes:", error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
@ -34,6 +43,15 @@ const Users: React.FC = () => {
|
||||||
fetchStats();
|
fetchStats();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const filtered = quizzes.filter(quiz =>
|
||||||
|
quiz.email.toLowerCase().includes(emailFilter.toLowerCase()) &&
|
||||||
|
((new Date(quiz.created_at).toLocaleDateString().includes(dateFilter) ||
|
||||||
|
new Date(quiz.updated_at).toLocaleDateString().includes(dateFilter)))
|
||||||
|
);
|
||||||
|
setFilteredQuizzes(filtered);
|
||||||
|
}, [emailFilter, dateFilter, quizzes]);
|
||||||
|
|
||||||
const handleDelete = (id: string) => {
|
const handleDelete = (id: string) => {
|
||||||
setQuizzes(quizzes.filter(quiz => quiz._id !== id));
|
setQuizzes(quizzes.filter(quiz => quiz._id !== id));
|
||||||
};
|
};
|
||||||
|
|
@ -80,19 +98,57 @@ const Users: React.FC = () => {
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
|
|
||||||
|
<Accordion expanded={expanded} onChange={() => setExpanded(!expanded)}>
|
||||||
|
<AccordionSummary
|
||||||
|
expandIcon={<ExpandMoreIcon />}
|
||||||
|
aria-controls="filter-content"
|
||||||
|
id="filter-header"
|
||||||
|
>
|
||||||
|
<Typography variant="h6">Filtres</Typography>
|
||||||
|
</AccordionSummary>
|
||||||
|
<AccordionDetails>
|
||||||
|
<Grid container spacing={3}>
|
||||||
|
<Grid item xs={12} sm={3}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
size="small"
|
||||||
|
placeholder="Filtrer par email"
|
||||||
|
value={emailFilter}
|
||||||
|
onChange={(e) => setEmailFilter(e.target.value)}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
<Grid item xs={12} sm={3}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
variant="outlined"
|
||||||
|
size="small"
|
||||||
|
placeholder="Filtrer par date"
|
||||||
|
value={dateFilter}
|
||||||
|
onChange={(e) => setDateFilter(e.target.value)}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</AccordionDetails>
|
||||||
|
</Accordion>
|
||||||
|
|
||||||
|
|
||||||
{/* Table */}
|
{/* Table */}
|
||||||
<TableContainer component={Paper} className="mt-4">
|
<TableContainer component={Paper} className="mt-4">
|
||||||
<Table>
|
<Table>
|
||||||
<TableHead>
|
<TableHead>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell>Enseignant</TableCell>
|
<TableCell>
|
||||||
|
Enseignant
|
||||||
|
</TableCell>
|
||||||
<TableCell>Titre</TableCell>
|
<TableCell>Titre</TableCell>
|
||||||
<TableCell>Crée</TableCell>
|
<TableCell>Crée</TableCell>
|
||||||
<TableCell>Modifié</TableCell>
|
<TableCell>Modifié</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{quizzes.map((quiz) => (
|
{filteredQuizzes.map((quiz) => (
|
||||||
<TableRow key={quiz._id}>
|
<TableRow key={quiz._id}>
|
||||||
<TableCell>{quiz.email}</TableCell>
|
<TableCell>{quiz.email}</TableCell>
|
||||||
<TableCell>{quiz.title}</TableCell>
|
<TableCell>{quiz.title}</TableCell>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue