import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { BarChart3, Clock, CheckCircle, AlertCircle } from 'lucide-react';
import VoteCard from '../components/VoteCard';
import ElectionDetailsModal from '../components/ElectionDetailsModal';
import LoadingSpinner from '../components/LoadingSpinner';
import './DashboardPage.css';
export default function DashboardPage({ voter }) {
const [votes, setVotes] = useState([]);
const [userVotes, setUserVotes] = useState([]);
const [loading, setLoading] = useState(true);
const [selectedElectionId, setSelectedElectionId] = useState(null);
useEffect(() => {
fetchVotes();
}, []);
const fetchVotes = async () => {
try {
const token = localStorage.getItem('token');
const [activeRes, upcomingRes, completedRes] = await Promise.all([
fetch('http://localhost:8000/api/elections/active', { headers: { 'Authorization': `Bearer ${token}` } }),
fetch('http://localhost:8000/api/elections/upcoming', { headers: { 'Authorization': `Bearer ${token}` } }),
fetch('http://localhost:8000/api/elections/completed', { headers: { 'Authorization': `Bearer ${token}` } })
]);
const active = activeRes.ok ? await activeRes.json() : [];
const upcoming = upcomingRes.ok ? await upcomingRes.json() : [];
const completed = completedRes.ok ? await completedRes.json() : [];
const allVotes = [
...((Array.isArray(active) ? active : [active]).map(v => ({ ...v, status: 'actif' }))),
...upcoming.map(v => ({ ...v, status: 'futur' })),
...completed.map(v => ({ ...v, status: 'ferme' }))
];
setVotes(allVotes);
const votesResponse = await fetch('http://localhost:8000/api/votes/history', {
headers: { 'Authorization': `Bearer ${token}` },
});
if (votesResponse.ok) {
const votesData = await votesResponse.json();
setUserVotes(votesData);
}
} catch (err) {
console.error('Erreur:', err);
} finally {
setLoading(false);
}
};
const activeVotes = votes.filter(v => v.status === 'actif');
const futureVotes = votes.filter(v => v.status === 'futur');
const historyVotes = votes.filter(v => v.status === 'ferme');
if (loading) return
Voici votre tableau de bord personnel
Votes en cours - Participez maintenant!
Élections qui démarreront bientôt
Vos 2 derniers votes
Il n'y a pas encore de votes disponibles.