fix: Hoy muestra ultimo dia disponible si no hay datos de hoy

This commit is contained in:
Tatiana Villa Ema 2026-04-27 01:03:43 +02:00
parent d14be12029
commit 9ecf4114ba
1 changed files with 19 additions and 2 deletions

View File

@ -70,9 +70,26 @@ async function loadToday() {
if (!response.ok) throw new Error("Error cargando hoy: " + response.status);
let data = await response.json();
if (!data || !data.length) throw new Error("Sin datos de hoy");
data = data ? data.filter(d => d.ciudad === ciudadActual) : [];
// Si no hay datos de hoy, buscar el último día disponible
if (!data.length) {
const hace30 = new Date(ahora);
hace30.setDate(hace30.getDate() - 30);
const y2 = hace30.getFullYear();
const m2 = String(hace30.getMonth() + 1).padStart(2, "0");
const d2 = String(hace30.getDate()).padStart(2, "0");
const urlFallback = buildApiUrl({ ciudad: ciudadActual, desde: `${y2}-${m2}-${d2}`, hasta: hoy });
const r2 = await fetch(urlFallback);
let data2 = await r2.json();
data2 = data2 ? data2.filter(d => d.ciudad === ciudadActual) : [];
if (!data2.length) throw new Error("Sin datos recientes");
// Ordenar por fecha y tomar el último día
data2.sort((a, b) => a.dia.localeCompare(b.dia));
const ultimoDia = data2[data2.length - 1].dia;
data = data2.filter(d => d.dia === ultimoDia);
}
data = data.filter(d => d.ciudad === ciudadActual);
renderLastData(data);
} catch (err) {