mirror of
https://github.com/marcogll/ap_pos.git
synced 2026-01-13 13:15:16 +00:00
Merge pull request #1 from marcogll/fix-dashboard-loop
Fix: Prevent infinite loop on dashboard chart
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user