'use client'; import React, { useState, useEffect, useRef } from 'react'; import { Card } from 'primereact/card'; import { Chart } from 'primereact/chart'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Button } from 'primereact/button'; import { Toast } from 'primereact/toast'; import { Tag } from 'primereact/tag'; import { Badge } from 'primereact/badge'; import { ProgressBar } from 'primereact/progressbar'; import notificationService, { Notification, NotificationStats } from '../../../../services/notificationService'; import { formatDistanceToNow } from 'date-fns'; import { fr } from 'date-fns/locale'; const NotificationsTableauBordPage = () => { const [notifications, setNotifications] = useState([]); const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const toast = useRef(null); useEffect(() => { loadData(); }, []); const loadData = async () => { try { setLoading(true); const [notifData, statsData] = await Promise.all([ notificationService.getNotifications(), notificationService.getNotificationStats() ]); // Prendre les 5 dernières notifications const recentNotifications = notifData .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()) .slice(0, 5); setNotifications(recentNotifications); setStats(statsData); } catch (error) { console.error('Erreur lors du chargement des données:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de charger le tableau de bord', life: 3000 }); } finally { setLoading(false); } }; const markAsRead = async (notification: Notification) => { try { await notificationService.markAsRead(notification.id); setNotifications(notifications.map(n => n.id === notification.id ? { ...n, lu: true } : n )); loadData(); // Recharger pour mettre à jour les stats } catch (error) { console.error('Erreur:', error); } }; const chartData = stats ? { labels: ['Info', 'Avertissement', 'Succès', 'Erreur'], datasets: [ { data: [ stats.parType.info || 0, stats.parType.warning || 0, stats.parType.success || 0, stats.parType.error || 0 ], backgroundColor: [ '#3B82F6', '#F59E0B', '#10B981', '#EF4444' ] } ] } : null; const typeBodyTemplate = (rowData: Notification) => { const getSeverity = (type: string) => { switch (type) { case 'info': return 'info'; case 'warning': return 'warning'; case 'success': return 'success'; case 'error': return 'danger'; default: return 'info'; } }; const getIcon = (type: string) => { switch (type) { case 'info': return 'pi-info-circle'; case 'warning': return 'pi-exclamation-triangle'; case 'success': return 'pi-check-circle'; case 'error': return 'pi-times-circle'; default: return 'pi-info-circle'; } }; return ( ); }; const dateBodyTemplate = (rowData: Notification) => { return formatDistanceToNow(new Date(rowData.date), { addSuffix: true, locale: fr }); }; const actionBodyTemplate = (rowData: Notification) => { return !rowData.lu ? (