'use client'; import React, { useState, useEffect, useRef } from 'react'; import { Card } from 'primereact/card'; import { Toast } from 'primereact/toast'; import { Button } from 'primereact/button'; import { Timeline } from 'primereact/timeline'; import { Tag } from 'primereact/tag'; import { Badge } from 'primereact/badge'; import notificationService, { Notification } from '../../../../services/notificationService'; import { formatDistanceToNow, format } from 'date-fns'; import { fr } from 'date-fns/locale'; const NotificationsRecentesPage = () => { const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const toast = useRef(null); useEffect(() => { loadNotifications(); }, []); const loadNotifications = async () => { try { setLoading(true); const data = await notificationService.getNotifications(); // Filtrer les notifications des 7 derniers jours const sevenDaysAgo = new Date(); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); const recentNotifications = data.filter(n => new Date(n.date) >= sevenDaysAgo); // Trier par date décroissante recentNotifications.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); setNotifications(recentNotifications); } catch (error) { console.error('Erreur lors du chargement des notifications:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de charger les notifications récentes', 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 )); toast.current?.show({ severity: 'success', summary: 'Succès', detail: 'Notification marquée comme lue', life: 2000 }); } catch (error) { console.error('Erreur:', error); } }; const getTypeIcon = (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'; } }; const getTypeColor = (type: string) => { switch (type) { case 'info': return '#3B82F6'; case 'warning': return '#F59E0B'; case 'success': return '#10B981'; case 'error': return '#EF4444'; default: return '#3B82F6'; } }; const customizedMarker = (item: Notification) => { return ( ); }; const customizedContent = (item: Notification) => { return (

{item.titre}

{!item.lu && }

{item.message}

{!item.lu && (
{formatDistanceToNow(new Date(item.date), { addSuffix: true, locale: fr })} {format(new Date(item.date), 'dd/MM/yyyy HH:mm', { locale: fr })}
{item.metadata && (
{item.metadata.chantierNom && (
)} {item.metadata.clientNom && (
)}
)}
); }; const unreadCount = notifications.filter(n => !n.lu).length; return (

Notifications Récentes

{unreadCount > 0 && ( )}
{loading ? (

Chargement des notifications...

) : notifications.length === 0 ? (

Aucune notification récente

Aucune notification au cours des 7 derniers jours

) : (
Affichage des notifications des 7 derniers jours ({notifications.length} notification{notifications.length > 1 ? 's' : ''})
)}
); }; export default NotificationsRecentesPage;