// web/js/components/NotificationDropdown.js const NotificationDropdown = () => { const [notifications, setNotifications] = React.useState([]); const [unreadCount, setUnreadCount] = React.useState(0); const [isOpen, setIsOpen] = React.useState(false); const [loading, setLoading] = React.useState(false); // Load notifications periodically React.useEffect(() => { loadNotifications(); const interval = setInterval(loadNotifications, 30000); // Refresh tiap 30 detik return () => clearInterval(interval); }, []); const loadNotifications = async () => { try { const response = await fetch( `${CONFIG.API_URL}${CONFIG.API_ENDPOINTS.NOTIFICATIONS}?limit=5`, { headers: { Authorization: `Bearer ${AuthUtils.getToken()}` }, } ); if (response.ok) { const result = await response.json(); setNotifications(result.data.notifications || []); setUnreadCount(result.data.unread_count || 0); } } catch (error) { console.error("Failed to load notifications", error); } }; const handleMarkAsRead = async (id) => { try { await fetch( `${CONFIG.API_URL}${CONFIG.API_ENDPOINTS.NOTIFICATIONS}/${id}/read`, { method: "PATCH", headers: { Authorization: `Bearer ${AuthUtils.getToken()}` }, } ); loadNotifications(); } catch (error) { console.error("Error marking as read", error); } }; const handleItemClick = (notif) => { handleMarkAsRead(notif.id); // Redirect logic based on type if (notif.entity_type === "claim") { // Trigger event custom agar UserApp bisa membuka tab Claims window.dispatchEvent(new CustomEvent("open-my-claims")); } }; return (
{notif.message}