From e7d386cb80fe855c1f9ad2b24b0ae90326bb734a Mon Sep 17 00:00:00 2001 From: marsalva Date: Sun, 15 Feb 2026 16:31:04 +0000 Subject: [PATCH] Actualizar usuarios.html --- usuarios.html | 56 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/usuarios.html b/usuarios.html index c7b8c41..3072bd6 100644 --- a/usuarios.html +++ b/usuarios.html @@ -169,13 +169,13 @@ sel.innerHTML = ''; provinciasES.sort().forEach(p => { const opt = document.createElement('option'); - opt.value = p; + opt.value = p.toUpperCase(); opt.innerText = p.toUpperCase(); sel.appendChild(opt); }); } - // LÓGICA DE CARGA CORREGIDA (NUEVA API) + // LÓGICA DE CARGA USANDO TU PROPIA BASE DE DATOS async function fetchMunicipios(provName) { const selMun = document.getElementById('selMunicipio'); const loader = document.getElementById('loaderMun'); @@ -185,32 +185,46 @@ loader.classList.remove('hidden'); try { - // Fuente estable de municipios de España - const res = await fetch(`https://raw.githubusercontent.com/palmerabollo/municipios-es/master/municipios.json`); - const allData = await res.json(); - const filtered = allData.filter(m => m.provincia.toUpperCase() === provName.toUpperCase()); - - selMun.innerHTML = ''; - filtered.sort((a,b) => a.nombre.localeCompare(b.nombre)).forEach(m => { - const opt = document.createElement('option'); - opt.value = m.nombre; - // Sugerimos el código postal si viene en la API - opt.dataset.cp = m.codigo_postal || ""; - opt.innerText = m.nombre.toUpperCase(); - selMun.appendChild(opt); + // Llamada a tu propio endpoint del server + const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, { + headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); - selMun.disabled = false; - } catch (e) { showToast("Error cargando municipios", true); } + const data = await res.json(); + + if (data.ok) { + selMun.innerHTML = ''; + data.municipios.forEach(m => { + const opt = document.createElement('option'); + opt.value = m.municipio; + opt.dataset.cp = m.codigo_postal; + opt.innerText = m.municipio.toUpperCase(); + selMun.appendChild(opt); + }); + // Opción de respaldo manual + selMun.innerHTML += ''; + selMun.disabled = false; + } + } catch (e) { showToast("Error al cargar municipios desde la DB local", true); } finally { loader.classList.add('hidden'); lucide.createIcons(); } } function autoFillCP(munName) { const sel = document.getElementById('selMunicipio'); const selectedOpt = sel.options[sel.selectedIndex]; + if (munName === "OTRO") { + const customMun = prompt("Nombre del Municipio:"); + if (customMun) { + const newOpt = document.createElement('option'); + newOpt.value = customMun.toUpperCase(); + newOpt.innerText = customMun.toUpperCase(); + newOpt.selected = true; + sel.appendChild(newOpt); + document.getElementById('cpInput').focus(); + } + return; + } if(selectedOpt && selectedOpt.dataset.cp) { document.getElementById('cpInput').value = selectedOpt.dataset.cp; - } else { - document.getElementById('cpInput').value = ""; } } @@ -218,7 +232,7 @@ const prov = document.getElementById('selProvince').value; const mun = document.getElementById('selMunicipio').value; const cp = document.getElementById('cpInput').value.trim(); - if (!prov || !mun || !cp) { showToast("Completa los datos de zona", true); return; } + if (!prov || !mun || !cp || mun === "OTRO") { showToast("Completa los datos de zona", true); return; } if (tempUserZones.some(z => z.city === mun && z.cps === cp)) { showToast("Esta zona ya está añadida", true); return; @@ -372,7 +386,7 @@ const list = document.getElementById('guildsList'); list.innerHTML = ""; availableGuilds.forEach(g => { - list.innerHTML += `
${g.name}
`; + list.innerHTML += `
${g.name}
`; }); lucide.createIcons(); }