This commit is contained in:
lizandrogd
2026-01-21 02:49:42 -05:00
parent 845762f916
commit cec91ff45a
6 changed files with 209 additions and 0 deletions
+94
View File
@@ -539,6 +539,100 @@
this.loadConversations();
this.setupEventListeners();
this.setupAutoRefresh();
this.setupNotificationPolling();
}
setupNotificationPolling() {
// Poll every 7 seconds for unread notifications
this.loadNotifications();
setInterval(() => this.loadNotifications(), 7000);
}
async loadNotifications() {
try {
const resp = await fetch('api/get_notifications.php');
const json = await resp.json();
if (json && json.success && Array.isArray(json.data)) {
json.data.forEach(n => this.showNotificationToast(n));
}
} catch (e) {
console.error('Error loading notifications', e);
}
}
async showNotificationToast(notification) {
// Create toast element
const containerId = 'notification-toasts';
let container = document.getElementById(containerId);
if (!container) {
container = document.createElement('div');
container.id = containerId;
container.style.position = 'fixed';
container.style.top = '12px';
container.style.right = '12px';
container.style.zIndex = 9999;
document.body.appendChild(container);
}
const toast = document.createElement('div');
toast.className = 'notification-toast';
toast.style.background = '#fff';
toast.style.padding = '10px 14px';
toast.style.border = '1px solid #ddd';
toast.style.boxShadow = '0 2px 6px rgba(0,0,0,0.12)';
toast.style.marginTop = '8px';
toast.style.borderRadius = '6px';
toast.style.minWidth = '220px';
const msg = document.createElement('div');
msg.textContent = notification.message || 'Nuevo evento';
toast.appendChild(msg);
const actions = document.createElement('div');
actions.style.marginTop = '8px';
actions.style.display = 'flex';
actions.style.gap = '8px';
const openBtn = document.createElement('button');
openBtn.className = 'btn btn-sm btn-primary';
openBtn.textContent = 'Abrir';
openBtn.onclick = async () => {
// Mark notification read, then open conversation
await fetch('api/mark_notification_read.php', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({id: notification.id})});
// Parse data to get user_id
let data = {};
try { data = notification.data ? JSON.parse(notification.data) : {}; } catch(e) {}
const userId = data.user_id || notification.user_id;
if (userId) {
// find conversation item and open it
const conv = this.conversations.find(c => c.user_id == userId);
if (conv) {
this.openConversation(conv.user_id, conv.name, conv.phone_number);
} else {
// fallback: open by fetching user info
this.openConversation(userId, 'Usuario', '');
}
}
// remove toast
toast.remove();
};
const dismiss = document.createElement('button');
dismiss.className = 'btn btn-sm btn-secondary';
dismiss.textContent = 'Descartar';
dismiss.onclick = async () => {
await fetch('api/mark_notification_read.php', {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({id: notification.id})});
toast.remove();
};
actions.appendChild(openBtn);
actions.appendChild(dismiss);
toast.appendChild(actions);
container.appendChild(toast);
// Auto remove after 18s
setTimeout(() => { if (toast.parentNode) toast.remove(); }, 18000);
}
setupEventListeners() {