import React, { useEffect, useState } from 'react'; import { fetchNotificaciones, fetchAllNotifications, marcarNotificacionComoVista } from '../api/notificaciones'; export default function Notificaciones() { const [notificaciones, setNotificaciones] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [page, setPage] = useState(1); const pageSize = 15; const [count, setCount] = useState(0); const [filtroVisto, setFiltroVisto] = useState('todos'); const fetchData = async () => { setLoading(true); setError(null); try { let data; if (filtroVisto === 'todos') { data = await fetchAllNotifications({ page, pageSize }); } else { const params = { page, pageSize }; if (filtroVisto === 'visto') params.visto = true; else if (filtroVisto === 'novisto') params.visto = false; data = await fetchNotificaciones(params); } setNotificaciones(data.results); setCount(data.count); } catch (e) { setError('Error al cargar notificaciones'); } finally { setLoading(false); } }; useEffect(() => { fetchData(); // eslint-disable-next-line }, [page, filtroVisto]); const handleActualizarTodas = async () => { setLoading(true); try { await Promise.all( notificaciones.filter(n => !n.visto).map(n => marcarNotificacionComoVista(n.id)) ); fetchData(); } catch (e) { setError('Error al actualizar notificaciones'); } finally { setLoading(false); } }; const handleFiltroChange = (e) => { setFiltroVisto(e.target.value); setPage(1); }; return (

Notificaciones

{loading ? (
Cargando...
) : error ? (
{error}
) : (
{Array.from({ length: pageSize }).map((_, idx) => { const n = notificaciones[idx]; if (n) { return ( ); } else { return ( ); } })}
ID Tipo Mensaje Fecha Visto
{n.id} {n.tipo?.descripcion || n.tipo?.tipo} {n.mensaje} {new Date(n.fecha_envio || n.created_at).toLocaleString()} {n.visto ? ( ) : ( No )}
- - - - -
)} {/* Paginación */}
Página {page} de {Math.ceil(count / pageSize) || 1}
15 por página
); }