'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; import { useParams } from 'next/navigation'; import { Campaign, Participant, ParticipantWithVoteStatus } from '@/types'; import { campaignService, participantService, voteService } from '@/lib/services'; import AddParticipantModal from '@/components/AddParticipantModal'; import EditParticipantModal from '@/components/EditParticipantModal'; import DeleteParticipantModal from '@/components/DeleteParticipantModal'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { Input } from '@/components/ui/input'; import Navigation from '@/components/Navigation'; import AuthGuard from '@/components/AuthGuard'; import { Users, User, Calendar, Mail, Vote, Copy, Check } from 'lucide-react'; export const dynamic = 'force-dynamic'; function CampaignParticipantsPageContent() { const params = useParams(); const campaignId = params.id as string; const [campaign, setCampaign] = useState(null); const [participants, setParticipants] = useState([]); const [loading, setLoading] = useState(true); const [showAddModal, setShowAddModal] = useState(false); const [showEditModal, setShowEditModal] = useState(false); const [showDeleteModal, setShowDeleteModal] = useState(false); const [selectedParticipant, setSelectedParticipant] = useState(null); const [copiedParticipantId, setCopiedParticipantId] = useState(null); useEffect(() => { loadData(); }, [campaignId]); const loadData = async () => { try { setLoading(true); const [campaigns, participantsWithVoteStatus] = await Promise.all([ campaignService.getAll(), voteService.getParticipantVoteStatus(campaignId) ]); const campaignData = campaigns.find(c => c.id === campaignId); setCampaign(campaignData || null); setParticipants(participantsWithVoteStatus); } catch (error) { console.error('Erreur lors du chargement des données:', error); } finally { setLoading(false); } }; const handleParticipantAdded = () => { setShowAddModal(false); loadData(); }; const handleParticipantEdited = () => { setShowEditModal(false); loadData(); }; const handleParticipantDeleted = () => { setShowDeleteModal(false); loadData(); }; const getInitials = (firstName: string, lastName: string) => { return `${firstName.charAt(0)}${lastName.charAt(0)}`.toUpperCase(); }; const copyVoteLink = (participantId: string) => { const voteUrl = `${window.location.origin}/campaigns/${campaignId}/vote/${participantId}`; navigator.clipboard.writeText(voteUrl); setCopiedParticipantId(participantId); setTimeout(() => setCopiedParticipantId(null), 2000); }; if (loading) { return (

Chargement des participants...

); } if (!campaign) { return (

Campagne introuvable

La campagne que vous recherchez n'existe pas ou a été supprimée.

); } const votedCount = participants.filter(p => p.has_voted).length; const totalBudget = participants.reduce((sum, p) => sum + (p.total_voted_amount || 0), 0); return (
{/* Header */}

Participants

{campaign.title}

{/* Stats Overview */}

Total Participants

{participants.length}

Ont voté

{votedCount}

Taux de participation

{participants.length > 0 ? Math.round((votedCount / participants.length) * 100) : 0}%

📊

Budget total voté

{totalBudget}€

💰
{/* Participants List */} {participants.length === 0 ? (

Aucun participant

Aucun participant n'a encore été ajouté à cette campagne.

) : (
{participants.map((participant) => (
{participant.first_name} {participant.last_name} {participant.has_voted ? 'A voté' : 'N\'a pas voté'}
{participant.email}
{getInitials(participant.first_name, participant.last_name)}
{participant.email}
{new Date(participant.created_at).toLocaleDateString('fr-FR')}
{participant.has_voted && participant.total_voted_amount !== undefined && (
{participant.total_voted_amount}€ votés
)}
{/* Vote Link for voting campaigns */} {campaign.status === 'voting' && (

Lien de vote personnel

)}
))}
)} {/* Modals */} setShowAddModal(false)} onSuccess={handleParticipantAdded} campaignId={campaignId} /> {selectedParticipant && ( setShowEditModal(false)} onSuccess={handleParticipantEdited} participant={selectedParticipant} /> )} {selectedParticipant && ( setShowDeleteModal(false)} onSuccess={handleParticipantDeleted} participant={selectedParticipant} /> )}
); } export default function CampaignParticipantsPage() { return ( ); }