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 ; return (

Bienvenue, {voter?.nom}! 👋

Voici votre tableau de bord personnel

{activeVotes.length}
Votes Actifs
Voir →
{futureVotes.length}
À Venir
Voir →
{historyVotes.length}
Votes Terminés
Voir →
{userVotes.length}
Votes Effectués
Voir →
{activeVotes.length > 0 && (

âš¡ Votes Actifs

Votes en cours - Participez maintenant!

{activeVotes.slice(0, 2).map(vote => ( v.election_id === vote.id)?.choix} onVote={(id) => window.location.href = `/vote/${id}`} /> ))}
{activeVotes.length > 2 && ( Voir tous les votes actifs ({activeVotes.length}) )}
)} {futureVotes.length > 0 && (

🔮 Votes à Venir

Élections qui démarreront bientôt

{futureVotes.slice(0, 2).map(vote => ( setSelectedElectionId(id)} /> ))}
{futureVotes.length > 2 && ( Voir tous les votes à venir ({futureVotes.length}) )}
)} {historyVotes.length > 0 && (

📋 Mon Historique

Vos 2 derniers votes

{historyVotes.slice(0, 2).map(vote => ( v.election_id === vote.id)?.choix} showResult={true} context="historique" onShowDetails={(id) => setSelectedElectionId(id)} /> ))}
{historyVotes.length > 2 && ( Voir tout mon historique ({historyVotes.length}) )}
)} {activeVotes.length === 0 && futureVotes.length === 0 && historyVotes.length === 0 && (
📭

Aucun vote disponible

Il n'y a pas encore de votes disponibles.

)} setSelectedElectionId(null)} voter={voter} type="futur" />
); }