'use client'; export const dynamic = 'force-dynamic'; import React, { useState, useEffect, useRef } from 'react'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Button } from 'primereact/button'; import { InputText } from 'primereact/inputtext'; import { Card } from 'primereact/card'; import { Toast } from 'primereact/toast'; import { Toolbar } from 'primereact/toolbar'; import { Tag } from 'primereact/tag'; import { Badge } from 'primereact/badge'; import { Chip } from 'primereact/chip'; import { ConfirmDialog, confirmDialog } from 'primereact/confirmdialog'; import notificationService, { Notification } from '../../../services/notificationService'; import { formatDistanceToNow } from 'date-fns'; import { fr } from 'date-fns/locale'; const NotificationsPage = () => { const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const [globalFilter, setGlobalFilter] = useState(''); const [selectedNotifications, setSelectedNotifications] = useState([]); const toast = useRef(null); const dt = useRef>(null); useEffect(() => { loadNotifications(); }, []); const loadNotifications = async () => { try { setLoading(true); const data = await notificationService.getNotifications(); setNotifications(data); } catch (error) { console.error('Erreur lors du chargement des notifications:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de charger les notifications', 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 markAllAsRead = async () => { try { await notificationService.markAllAsRead(); setNotifications(notifications.map(n => ({ ...n, lu: true }))); toast.current?.show({ severity: 'success', summary: 'Succès', detail: 'Toutes les notifications ont été marquées comme lues', life: 3000 }); } catch (error) { console.error('Erreur:', error); } }; const deleteNotification = async (notification: Notification) => { confirmDialog({ message: 'Êtes-vous sûr de vouloir supprimer cette notification ?', header: 'Confirmation', icon: 'pi pi-exclamation-triangle', acceptLabel: 'Oui', rejectLabel: 'Non', accept: async () => { try { await notificationService.deleteNotification(notification.id); setNotifications(notifications.filter(n => n.id !== notification.id)); toast.current?.show({ severity: 'success', summary: 'Succès', detail: 'Notification supprimée', life: 3000 }); } catch (error) { console.error('Erreur:', error); toast.current?.show({ severity: 'error', summary: 'Erreur', detail: 'Impossible de supprimer la notification', life: 3000 }); } } }); }; 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 statusBodyTemplate = (rowData: Notification) => { return rowData.lu ? ( ) : ( ); }; const dateBodyTemplate = (rowData: Notification) => { return formatDistanceToNow(new Date(rowData.date), { addSuffix: true, locale: fr }); }; const actionBodyTemplate = (rowData: Notification) => { return (
{!rowData.lu && (
); }; const leftToolbarTemplate = () => { const unreadCount = notifications.filter(n => !n.lu).length; return (
Notifications
{unreadCount > 0 && ( )}
); }; const rightToolbarTemplate = () => { return (
); }; const header = (
Toutes les notifications
setGlobalFilter(e.currentTarget.value)} />
); return (
setSelectedNotifications(e.value)} selectionMode="checkbox" dataKey="id" paginator rows={10} rowsPerPageOptions={[5, 10, 25, 50]} className="datatable-responsive" paginatorTemplate="FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink CurrentPageReport RowsPerPageDropdown" currentPageReportTemplate="Affichage de {first} à {last} sur {totalRecords} notifications" globalFilter={globalFilter} emptyMessage="Aucune notification trouvée." header={header} responsiveLayout="scroll" loading={loading} rowClassName={(data) => !data.lu ? 'bg-blue-50' : ''} >
); }; export default NotificationsPage;