mirror of
https://github.com/marcogll/ap_pos.git
synced 2026-01-13 13:15:16 +00:00
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.
This commit is contained in:
@@ -40,9 +40,10 @@ let isDashboardLoading = false;
|
|||||||
// --- LÓGICA DE NEGOCIO ---
|
// --- LÓGICA DE NEGOCIO ---
|
||||||
|
|
||||||
async function loadDashboardData() {
|
async function loadDashboardData() {
|
||||||
// Guardia para prevenir ejecuciones múltiples
|
// Guardia para prevenir ejecuciones múltiples y re-entradas.
|
||||||
if (currentUser.role !== 'admin' || !incomeChart || isDashboardLoading) return;
|
if (currentUser.role !== 'admin' || !incomeChart || isDashboardLoading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
isDashboardLoading = true;
|
isDashboardLoading = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -50,12 +51,19 @@ async function loadDashboardData() {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
if (response.status === 403) {
|
if (response.status === 403) {
|
||||||
console.warn('Acceso al dashboard denegado.');
|
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();
|
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
|
// Actualizar tarjetas de estadísticas
|
||||||
document.getElementById('stat-total-income').textContent = `${Number(data.totalIncome || 0).toFixed(2)}`;
|
document.getElementById('stat-total-income').textContent = `${Number(data.totalIncome || 0).toFixed(2)}`;
|
||||||
document.getElementById('stat-total-movements').textContent = data.totalMovements || 0;
|
document.getElementById('stat-total-movements').textContent = data.totalMovements || 0;
|
||||||
@@ -63,11 +71,14 @@ async function loadDashboardData() {
|
|||||||
// Actualizar datos del gráfico
|
// Actualizar datos del gráfico
|
||||||
incomeChart.data.labels = data.incomeByService.map(item => item.tipo);
|
incomeChart.data.labels = data.incomeByService.map(item => item.tipo);
|
||||||
incomeChart.data.datasets[0].data = data.incomeByService.map(item => item.total);
|
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) {
|
} catch (error) {
|
||||||
console.error('Error al cargar el dashboard:', error);
|
console.error('Error al cargar el dashboard:', error);
|
||||||
} finally {
|
} finally {
|
||||||
|
// Asegurar que el bloqueo se libere sin importar el resultado.
|
||||||
isDashboardLoading = false;
|
isDashboardLoading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user