import React, { useEffect, useState } from 'react'; import './SavedNotifications.css'; function SavedNotifications({ onClose, onNotificationChange }) { const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [items, setItems] = useState([]); const [busyId, setBusyId] = useState(null); useEffect(() => { let cancelled = false; (async () => { try { const res = await fetch('/api/notifications/mine', { credentials: 'include' }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); if (cancelled) return; const merged = [ ...(data.pharmacy || []), ...(data.global || []), ].sort((a, b) => (b.created_at || '').localeCompare(a.created_at || '')); setItems(merged); } catch (err) { if (!cancelled) setError(err.message || 'No se pudieron cargar las notificaciones guardadas'); } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, []); async function handleDelete(item) { const key = `${item.scope}:${item.id}`; setBusyId(key); try { const res = await fetch('/api/notifications/mine', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ scope: item.scope, id: item.id }), }); if (!res.ok && res.status !== 204) throw new Error(`HTTP ${res.status}`); setItems(prev => prev.filter(i => !(i.scope === item.scope && i.id === item.id))); onNotificationChange?.(); } catch (err) { setError(err.message || 'No se pudo eliminar la notificación'); } finally { setBusyId(null); } } return (
e.stopPropagation()}>

🔔 Notificaciones Guardadas

{loading &&

Cargando…

} {!loading && error &&

{error}

} {!loading && !error && items.length === 0 && (

Aún no hay notificaciones guardadas. Toca la campana 🔕 en una farmacia sin stock para recibir notificaciones cuando se reponga.

)} {!loading && !error && items.length > 0 && (
    {items.map(item => { const key = `${item.scope}:${item.id}`; return (
  • {item.medicine_name || item.medicine_nregistro}
    {item.scope === 'pharmacy' ? ( <> 🏥 {item.pharmacy_name || `Farmacia #${item.pharmacy_id}`} {item.pharmacy_address && ( {item.pharmacy_address} )} ) : ( Cualquier farmacia )}
  • ); })}
)}
); } export default SavedNotifications;