From 9ea734abe640be652d4b1fb74359e909ad60c209 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:42:05 +0000 Subject: [PATCH] Fix: Prevent infinite loop on dashboard chart The dashboard was entering an infinite rendering loop, causing the browser to hang and eventually crash. This was likely due to Chart.js's responsive feature triggering a continuous cycle of updates and resizes. This commit addresses the issue by: - Modifying `loadDashboardData` to be more robust. - Calling `incomeChart.update('none')` to prevent animation-related rendering loops. - Adding a check to ensure the dashboard is still active before updating the DOM, preventing race conditions. - Improving the re-entrancy guard and error handling within the function. --- ap-pos/app.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/ap-pos/app.js b/ap-pos/app.js index 064ff82..f2c997d 100644 --- a/ap-pos/app.js +++ b/ap-pos/app.js @@ -40,9 +40,10 @@ let isDashboardLoading = false; // --- LÓGICA DE NEGOCIO --- async function loadDashboardData() { - // Guardia para prevenir ejecuciones múltiples - if (currentUser.role !== 'admin' || !incomeChart || isDashboardLoading) return; - + // Guardia para prevenir ejecuciones múltiples y re-entradas. + if (currentUser.role !== 'admin' || !incomeChart || isDashboardLoading) { + return; + } isDashboardLoading = true; try { @@ -50,12 +51,19 @@ async function loadDashboardData() { if (!response.ok) { if (response.status === 403) { console.warn('Acceso al dashboard denegado.'); - return; + } else { + throw new Error('Falló la carga de datos del dashboard'); } - throw new Error('Falló la carga de datos del dashboard'); + return; // Salir aquí después de manejar el error } const data = await response.json(); + // Antes de actualizar, verificar que el dashboard sigue activo. + const dashboardTab = document.getElementById('tab-dashboard'); + if (!dashboardTab.classList.contains('active')) { + return; + } + // Actualizar tarjetas de estadísticas document.getElementById('stat-total-income').textContent = `${Number(data.totalIncome || 0).toFixed(2)}`; document.getElementById('stat-total-movements').textContent = data.totalMovements || 0; @@ -63,11 +71,14 @@ async function loadDashboardData() { // Actualizar datos del gráfico incomeChart.data.labels = data.incomeByService.map(item => item.tipo); incomeChart.data.datasets[0].data = data.incomeByService.map(item => item.total); - incomeChart.update(); + + // Usar 'none' para el modo de actualización previene bucles de renderizado por animación/responsividad. + incomeChart.update('none'); } catch (error) { console.error('Error al cargar el dashboard:', error); } finally { + // Asegurar que el bloqueo se libere sin importar el resultado. isDashboardLoading = false; } }