'use client'; import { useState, useEffect } from 'react'; import UniversProForm from './UniversProForm'; interface UniversPro { id: string; nom: string; prenom: string; telephone: string; email: string; adresse: string; nomEntreprise: string; } export default function UniversProTable() { const [contacts, setContacts] = useState([]); const [search, setSearch] = useState(''); const [loading, setLoading] = useState(true); const [showForm, setShowForm] = useState(false); const [editingContact, setEditingContact] = useState(null); const [viewingContact, setViewingContact] = useState(null); const fetchContacts = async (searchTerm: string = '') => { setLoading(true); try { const url = searchTerm ? `/api/univers-pro?search=${encodeURIComponent(searchTerm)}` : '/api/univers-pro'; const response = await fetch(url); if (response.ok) { const data = await response.json(); setContacts(data); } } catch (error) { console.error('Erreur lors du chargement des contacts:', error); } finally { setLoading(false); } }; useEffect(() => { const timeoutId = setTimeout(() => { fetchContacts(search); }, 300); return () => clearTimeout(timeoutId); }, [search]); useEffect(() => { fetchContacts(); }, []); const handleDelete = async (id: string) => { if (!confirm('Êtes-vous sûr de vouloir supprimer ce contact ?')) { return; } try { const response = await fetch(`/api/univers-pro/${id}`, { method: 'DELETE', }); if (response.ok) { fetchContacts(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 = (contact: UniversPro) => { setEditingContact(contact); setShowForm(true); }; const handleView = async (id: string) => { try { const response = await fetch(`/api/univers-pro/${id}`); if (response.ok) { const data = await response.json(); setViewingContact(data); } } catch (error) { console.error('Erreur lors de la récupération:', error); } }; const handleFormClose = () => { setShowForm(false); setEditingContact(null); fetchContacts(search); }; const getInitials = (nom: string, prenom: string) => { return `${prenom.charAt(0)}${nom.charAt(0)}`.toUpperCase(); }; 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...
) : contacts.length === 0 ? (
Aucun contact trouvé
) : (
{contacts.map((contact) => ( ))}
NOM CONTACT ADRESSE ENTREPRISE ACTIONS
{getInitials(contact.nom, contact.prenom)}
{contact.prenom} {contact.nom}
{contact.telephone}
{contact.email}
{contact.adresse}
{contact.nomEntreprise}
)}
{/* Modal formulaire */} {showForm && ( )} {/* Modal vue détaillée */} {viewingContact && (
{/* Header */}
{getInitials(viewingContact.nom, viewingContact.prenom)}

{viewingContact.prenom} {viewingContact.nom}

Informations détaillées du contact

{/* Contenu scrollable */}
Adresse
{viewingContact.adresse}
Entreprise
{viewingContact.nomEntreprise}
{/* Footer */}
)} ); }