diff --git a/index.html b/index.html index 932fc48..6f30b69 100644 --- a/index.html +++ b/index.html @@ -35,6 +35,14 @@

Conectando...

+ +
@@ -82,16 +90,36 @@ const urlParams = new URLSearchParams(window.location.search); urlToken = urlParams.get('token'); - if (!urlToken) return; + // CORRECCIÓN: Si no hay token, llamamos a showError() + if (!urlToken) { + showError(); + return; + } try { const res = await fetch(`${API_URL}/public/portal/${urlToken}`); const data = await res.json(); if (!data.ok) throw new Error("Token inválido"); renderPortal(data.client, data.company, data.services); - } catch (e) { console.error(e); } + } catch (e) { + console.error(e); + showError(); // CORRECCIÓN: Si falla la conexión o el token es falso, también mostramos error + } }); + // FUNCIÓN DE ERROR RESTAURADA + function showError() { + const loader = document.getElementById('loader'); + const errorScreen = document.getElementById('errorScreen'); + + loader.classList.add('opacity-0', 'pointer-events-none'); + setTimeout(() => { + loader.classList.add('hidden'); + errorScreen.classList.remove('hidden'); + errorScreen.classList.add('flex'); + }, 300); + } + function summarizeDescription(rawText) { if (!rawText) return "Avería reportada."; let text = rawText.replace(/\n/g, ' '); @@ -149,7 +177,6 @@
`; } else if (srv.status_name === 'Técnico de Camino') { - // MODO MAPA EN VIVO statusHtml = `
@@ -172,7 +199,7 @@
`; - mapsToInit.push(srv.id); // Guardamos para inicializar el mapa + mapsToInit.push(srv.id); } else if (srv.status_name === 'En Reparación') { statusHtml = ` @@ -236,22 +263,16 @@ document.getElementById('loader').classList.add('hidden'); document.getElementById('mainContent').classList.remove('hidden'); - // INICIALIZAMOS LOS MAPAS mapsToInit.forEach(id => initLiveMap(id)); }, 300); } - // ========================================== - // LÓGICA DE MAPA EN TIEMPO REAL - // ========================================== function initLiveMap(serviceId) { - if(!document.getElementById(`map-${serviceId}`)) return; + if(!document.getElementById(`liveMap-${serviceId}`)) return; - // 1. Configuramos el mapa de Leaflet - liveMaps[serviceId] = L.map(`map-${serviceId}`, { zoomControl: false, attributionControl: false }).setView([40.416775, -3.703790], 6); + liveMaps[serviceId] = L.map(`liveMap-${serviceId}`, { zoomControl: false, attributionControl: false }).setView([40.416775, -3.703790], 6); L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png').addTo(liveMaps[serviceId]); - // 2. Creamos un icono HTML personalizado (un puntito azul que brilla) const carIcon = L.divIcon({ className: 'custom-div-icon', html: `
`, @@ -261,7 +282,6 @@ liveMarkers[serviceId] = L.marker([40.416775, -3.703790], {icon: carIcon}); - // 3. Ejecutamos la búsqueda de coordenadas cada 10 segundos fetchWorkerLocation(serviceId); setInterval(() => fetchWorkerLocation(serviceId), 10000); } @@ -273,20 +293,22 @@ if (data.ok && data.location) { const loader = document.getElementById(`map-loader-${serviceId}`); - if(loader) loader.classList.add('hidden'); // Ocultar pantalla de "Buscando satélites" + if(loader) loader.classList.add('hidden'); const lat = parseFloat(data.location.lat); const lng = parseFloat(data.location.lng); - // Si el marcador no está en el mapa, lo añadimos if (!liveMaps[serviceId].hasLayer(liveMarkers[serviceId])) { liveMarkers[serviceId].addTo(liveMaps[serviceId]); liveMaps[serviceId].setView([lat, lng], 15); } else { - // Si ya está, lo movemos suavemente liveMarkers[serviceId].setLatLng([lat, lng]); liveMaps[serviceId].flyTo([lat, lng], 16, { animate: true, duration: 1.5 }); } + } else { + // MEJORA: Mensaje claro si el técnico no ha encendido su GPS todavía + const p = document.querySelector(`#map-loader-${serviceId} p`); + if(p) p.innerText = "ESPERANDO SEÑAL GPS DEL TÉCNICO..."; } } catch(e) {} }