added CRUD Customers

This commit is contained in:
2026-01-20 19:02:49 +01:00
parent 8e8fd70c8c
commit 3a8a6d1576
7 changed files with 1157 additions and 0 deletions

View File

@@ -0,0 +1,490 @@
'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<Adherent[]>([]);
const [search, setSearch] = useState('');
const [loading, setLoading] = useState(true);
const [showForm, setShowForm] = useState(false);
const [editingAdherent, setEditingAdherent] = useState<Adherent | null>(null);
const [viewingAdherent, setViewingAdherent] = useState<Adherent | null>(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 */}
<div className="bg-white rounded-lg shadow-sm p-4 mb-6">
<div className="flex flex-col md:flex-row gap-4 items-center">
{/* Barre de recherche */}
<div className="flex-1 w-full md:w-auto">
<div className="relative">
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
<svg className="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
</div>
<input
type="text"
placeholder="Rechercher un adhérent..."
value={search}
onChange={(e) => 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"
/>
</div>
</div>
{/* Boutons d'action */}
<div className="flex gap-3">
<button
onClick={() => {
setEditingAdherent(null);
setShowForm(true);
}}
className="flex items-center gap-2 px-4 py-2 bg-lgreen text-white rounded-lg hover:bg-dgreen transition-colors"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
</svg>
Nouvel adhérent
</button>
<button className="flex items-center gap-2 px-4 py-2 bg-lblue text-white rounded-lg hover:bg-dblue transition-colors">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
</svg>
Importer
</button>
<button className="flex items-center gap-2 px-4 py-2 bg-lorange text-white rounded-lg hover:bg-dorange transition-colors">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4-4m0 0l-4-4m4 4V4" />
</svg>
Exporter
</button>
</div>
</div>
</div>
{/* Tableau */}
<div className="bg-white rounded-lg shadow-sm overflow-hidden">
{loading ? (
<div className="p-8 text-center text-gray-500">Chargement...</div>
) : adherents.length === 0 ? (
<div className="p-8 text-center text-gray-500">Aucun adhérent trouvé</div>
) : (
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">NOM</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">CONTACT</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ADRESSE</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">PRESCRIPTEUR</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">SITUATION</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ACTIONS</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{adherents.map((adherent) => (
<tr key={adherent.id} className="hover:bg-gray-50">
<td className="px-6 py-4 whitespace-nowrap">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-full bg-lgreen flex items-center justify-center text-white font-semibold">
{getInitials(adherent.nom, adherent.prenom)}
</div>
<div>
<div className="text-sm font-semibold text-gray-900">
{adherent.prenom} {adherent.nom}
</div>
<div className="text-sm text-gray-500">
le {formatDate(adherent.dateNaissance)}
</div>
</div>
</div>
</td>
<td className="px-6 py-4">
<div className="flex items-center gap-2 mb-1">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
</svg>
<span className="text-sm text-gray-900 font-medium">{adherent.telephone}</span>
<span className="text-xs text-gray-500">(Principal)</span>
</div>
{adherent.telephoneSecondaire && (
<div className="flex items-center gap-2 mb-1">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
</svg>
<span className="text-sm text-gray-600">{adherent.telephoneSecondaire}</span>
<span className="text-xs text-gray-500">(Secondaire)</span>
</div>
)}
<div className="flex items-center gap-2 mt-1">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
</svg>
<span className="text-sm text-gray-500">{adherent.email}</span>
</div>
</td>
<td className="px-6 py-4">
<div className="text-sm text-gray-900">{adherent.adresse}</div>
</td>
<td className="px-6 py-4">
<div className="text-sm text-gray-900">
{adherent.prescripteur || '-'}
</div>
</td>
<td className="px-6 py-4">
<div className="text-sm text-gray-900">
{adherent.situation || '-'}
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium">
<div className="flex items-center gap-3">
<button
onClick={() => handleView(adherent.id)}
className="text-lblue hover:text-dblue"
title="Voir"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
</svg>
</button>
<button
onClick={() => handleEdit(adherent)}
className="text-lblue hover:text-dblue"
title="Modifier"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
</button>
<button
onClick={() => handleDelete(adherent.id)}
className="text-red-500 hover:text-red-700"
title="Supprimer"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
{/* Modal formulaire */}
{showForm && (
<AdherentForm
adherent={editingAdherent}
onClose={handleFormClose}
/>
)}
{/* Modal vue détaillée */}
{viewingAdherent && (
<div className="fixed inset-0 bg-black/60 backdrop-blur-sm flex items-center justify-center z-50 p-4 animate-fadeIn">
<div className="bg-white rounded-lg shadow-xl max-w-3xl w-full max-h-[90vh] overflow-hidden flex flex-col animate-slideUp border border-gray-200">
{/* Header */}
<div className="border-b border-gray-200 px-6 py-5 bg-white">
<div className="flex items-center justify-between">
<div className="flex items-center gap-4">
<div className="w-12 h-12 rounded-full bg-gray-100 flex items-center justify-center text-gray-700 font-semibold text-lg border-2 border-gray-200">
{getInitials(viewingAdherent.nom, viewingAdherent.prenom)}
</div>
<div>
<h2 className="text-xl font-semibold text-gray-900">
{viewingAdherent.prenom} {viewingAdherent.nom}
</h2>
<p className="text-sm text-gray-500 mt-0.5">
Informations détaillées de l'adhérent
</p>
</div>
</div>
<button
onClick={() => setViewingAdherent(null)}
className="text-gray-400 hover:text-gray-600 transition-colors p-1.5 hover:bg-gray-100 rounded"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
{/* Contenu scrollable */}
<div className="flex-1 overflow-y-auto px-6 py-6">
{/* Informations principales */}
<div className="mb-8">
<h3 className="text-sm font-semibold text-gray-900 mb-4 uppercase tracking-wide">
Informations principales
</h3>
<div className="space-y-3">
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Date de naissance</span>
</div>
<div className="flex-1 flex items-center gap-2">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
<span className="text-sm text-gray-900 font-medium">{formatDate(viewingAdherent.dateNaissance)}</span>
</div>
</div>
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Téléphone</span>
</div>
<div className="flex-1 flex items-center gap-2">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
</svg>
<a href={`tel:${viewingAdherent.telephone}`} className="text-sm text-gray-900 font-medium hover:text-lblue transition-colors">
{viewingAdherent.telephone}
</a>
</div>
</div>
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Email</span>
</div>
<div className="flex-1 flex items-center gap-2">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
</svg>
<a href={`mailto:${viewingAdherent.email}`} className="text-sm text-gray-900 font-medium hover:text-lblue transition-colors break-all">
{viewingAdherent.email}
</a>
</div>
</div>
<div className="flex items-start py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Adresse</span>
</div>
<div className="flex-1 flex items-start gap-2">
<svg className="h-4 w-4 text-gray-400 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 11a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
<span className="text-sm text-gray-900 font-medium">{viewingAdherent.adresse}</span>
</div>
</div>
</div>
</div>
{/* Informations complémentaires */}
<div>
<h3 className="text-sm font-semibold text-gray-900 mb-4 uppercase tracking-wide">
Informations complémentaires
</h3>
<div className="space-y-3">
{viewingAdherent.situation && (
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Situation</span>
</div>
<div className="flex-1">
<span className="text-sm text-gray-900 font-medium">{viewingAdherent.situation}</span>
</div>
</div>
)}
{viewingAdherent.prescripteur && (
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Prescripteur</span>
</div>
<div className="flex-1">
<span className="text-sm text-gray-900 font-medium">{viewingAdherent.prescripteur}</span>
</div>
</div>
)}
{viewingAdherent.facturation && (
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Facturation</span>
</div>
<div className="flex-1">
<span className="text-sm text-gray-900 font-medium">{viewingAdherent.facturation}</span>
</div>
</div>
)}
{viewingAdherent.telephoneSecondaire && (
<div className="flex items-center py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Téléphone secondaire</span>
</div>
<div className="flex-1 flex items-center gap-2">
<svg className="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
</svg>
<a href={`tel:${viewingAdherent.telephoneSecondaire}`} className="text-sm text-gray-900 font-medium hover:text-lblue transition-colors">
{viewingAdherent.telephoneSecondaire}
</a>
</div>
</div>
)}
{viewingAdherent.commentaire && (
<div className="flex items-start py-3 border-b border-gray-100">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Commentaire</span>
</div>
<div className="flex-1">
<p className="text-sm text-gray-900 font-medium whitespace-pre-wrap">{viewingAdherent.commentaire}</p>
</div>
</div>
)}
{viewingAdherent.instructions && (
<div className="flex items-start py-3">
<div className="w-32 flex-shrink-0">
<span className="text-xs font-medium text-gray-500 uppercase tracking-wide">Instructions</span>
</div>
<div className="flex-1">
<p className="text-sm text-gray-900 font-medium whitespace-pre-wrap">{viewingAdherent.instructions}</p>
</div>
</div>
)}
</div>
</div>
</div>
{/* Footer */}
<div className="border-t border-gray-200 px-6 py-4 bg-gray-50/50">
<div className="flex justify-end gap-3">
<button
onClick={() => setViewingAdherent(null)}
className="px-4 py-2 text-sm font-medium text-gray-700 hover:text-gray-900 transition-colors"
>
Fermer
</button>
<button
onClick={() => {
setViewingAdherent(null);
handleEdit(viewingAdherent);
}}
className="px-4 py-2 bg-lblue text-white text-sm font-medium rounded hover:bg-dblue transition-colors flex items-center gap-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
</svg>
Modifier
</button>
</div>
</div>
</div>
</div>
)}
</>
);
}