'use client'; import { useState, useEffect } from 'react'; import AdherentForm from './AdherentForm'; interface Adherent { id: string; nom: string; prenom: string; dateNaissance: string; adresse: string; email: string; telephone: string; situation?: string | null; prescripteur?: string | null; facturation?: string | null; commentaire?: string | null; telephoneSecondaire?: string | null; instructions?: string | null; } export default function AdherentsTable() { const [adherents, setAdherents] = useState([]); const [search, setSearch] = useState(''); const [loading, setLoading] = useState(true); const [showForm, setShowForm] = useState(false); const [editingAdherent, setEditingAdherent] = useState(null); const [viewingAdherent, setViewingAdherent] = useState(null); const fetchAdherents = async (searchTerm: string = '') => { setLoading(true); try { const url = searchTerm ? `/api/adherents?search=${encodeURIComponent(searchTerm)}` : '/api/adherents'; const response = await fetch(url); if (response.ok) { const data = await response.json(); setAdherents(data); } } catch (error) { console.error('Erreur lors du chargement des adhérents:', error); } finally { setLoading(false); } }; useEffect(() => { const timeoutId = setTimeout(() => { fetchAdherents(search); }, 300); return () => clearTimeout(timeoutId); }, [search]); useEffect(() => { fetchAdherents(); }, []); const handleDelete = async (id: string) => { if (!confirm('Êtes-vous sûr de vouloir supprimer cet adhérent ?')) { return; } try { const response = await fetch(`/api/adherents/${id}`, { method: 'DELETE', }); if (response.ok) { fetchAdherents(search); } else { alert('Erreur lors de la suppression'); } } catch (error) { console.error('Erreur lors de la suppression:', error); alert('Erreur lors de la suppression'); } }; const handleEdit = (adherent: Adherent) => { setEditingAdherent(adherent); setShowForm(true); }; const handleView = async (id: string) => { try { const response = await fetch(`/api/adherents/${id}`); if (response.ok) { const data = await response.json(); setViewingAdherent(data); } } catch (error) { console.error('Erreur lors de la récupération:', error); } }; const handleFormClose = () => { setShowForm(false); setEditingAdherent(null); fetchAdherents(search); }; const getInitials = (nom: string, prenom: string) => { return `${prenom.charAt(0)}${nom.charAt(0)}`.toUpperCase(); }; const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' }); }; return ( <> {/* Barre de recherche et actions */}
{/* Barre de recherche */}
setSearch(e.target.value)} className="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-lblue focus:border-transparent" />
{/* Boutons d'action */}
{/* Tableau */}
{loading ? (
Chargement...
) : adherents.length === 0 ? (
Aucun adhérent trouvé
) : (
{adherents.map((adherent) => ( ))}
NOM CONTACT ADRESSE PRESCRIPTEUR SITUATION ACTIONS
{getInitials(adherent.nom, adherent.prenom)}
{adherent.prenom} {adherent.nom}
Né le {formatDate(adherent.dateNaissance)}
{adherent.telephone} (Principal)
{adherent.telephoneSecondaire && (
{adherent.telephoneSecondaire} (Secondaire)
)}
{adherent.email}
{adherent.adresse}
{adherent.prescripteur || '-'}
{adherent.situation || '-'}
)}
{/* Modal formulaire */} {showForm && ( )} {/* Modal vue détaillée */} {viewingAdherent && (
{/* Header */}
{getInitials(viewingAdherent.nom, viewingAdherent.prenom)}

{viewingAdherent.prenom} {viewingAdherent.nom}

Informations détaillées de l'adhérent

{/* Contenu scrollable */}
{/* Informations principales */}

Informations principales

Date de naissance
{formatDate(viewingAdherent.dateNaissance)}
Adresse
{viewingAdherent.adresse}
{/* Informations complémentaires */}

Informations complémentaires

{viewingAdherent.situation && (
Situation
{viewingAdherent.situation}
)} {viewingAdherent.prescripteur && (
Prescripteur
{viewingAdherent.prescripteur}
)} {viewingAdherent.facturation && (
Facturation
{viewingAdherent.facturation}
)} {viewingAdherent.telephoneSecondaire && ( )} {viewingAdherent.commentaire && (
Commentaire

{viewingAdherent.commentaire}

)} {viewingAdherent.instructions && (
Instructions

{viewingAdherent.instructions}

)}
{/* Footer */}
)} ); }