Actualizar usuarios.html

This commit is contained in:
2026-02-15 16:39:01 +00:00
parent e7d386cb80
commit 7e8c2f1c33

View File

@@ -186,80 +186,46 @@
loader.classList.remove('hidden'); loader.classList.remove('hidden');
try { try {
// Llamada a tu propio endpoint del server // Llamada a tu propio endpoint del server
const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, { async function fetchMunicipios(provName) {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } const selMun = document.getElementById('selMunicipio');
}); const loader = document.getElementById('loaderMun');
const data = await res.json();
selMun.innerHTML = '<option value="">-- Cargando... --</option>';
if (data.ok) { selMun.disabled = true;
selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>'; if(!provName) return;
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 += '<option value="OTRO">-- NO ESTÁ EN LA LISTA (MANUAL) --</option>';
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) { if (loader) loader.classList.remove('hidden');
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;
}
}
function addZoneToUser() { try {
const prov = document.getElementById('selProvince').value; // Llamamos a tu propia base de datos a través del servidor
const mun = document.getElementById('selMunicipio').value; const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, {
const cp = document.getElementById('cpInput').value.trim(); headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
if (!prov || !mun || !cp || mun === "OTRO") { showToast("Completa los datos de zona", true); return; } });
const data = await res.json();
if (tempUserZones.some(z => z.city === mun && z.cps === cp)) {
showToast("Esta zona ya está añadida", true); return; if (data.ok && data.municipios.length > 0) {
} selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>';
data.municipios.forEach(m => {
tempUserZones.push({ province: prov, city: mun, cps: cp }); const opt = document.createElement('option');
renderTempZones(); opt.value = m.municipio;
document.getElementById('cpInput').value = ""; opt.dataset.cp = m.codigo_postal;
} opt.innerText = `${m.municipio.toUpperCase()} (${m.codigo_postal})`;
selMun.appendChild(opt);
function removeTempZone(idx) {
tempUserZones.splice(idx, 1);
renderTempZones();
}
function renderTempZones() {
const area = document.getElementById('userZonesArea');
area.innerHTML = tempUserZones.length === 0 ? '<p class="text-[10px] text-gray-300 italic p-1">Sin zonas añadidas...</p>' : "";
tempUserZones.forEach((z, i) => {
area.innerHTML += `
<span class="bg-blue-100 text-blue-700 px-3 py-1.5 rounded-lg text-[10px] font-black border border-blue-200 flex items-center gap-2">
${z.city} (${z.cps})
<button type="button" onclick="removeTempZone(${i})" class="text-blue-400 hover:text-red-500"><i data-lucide="x" class="w-3 h-3"></i></button>
</span>`;
}); });
lucide.createIcons(); selMun.innerHTML += '<option value="OTRO">-- NO ESTÁ EN LA LISTA (MANUAL) --</option>';
selMun.disabled = false;
} else {
// Si la provincia no tiene datos aún en la DB, mostramos la opción manual
selMun.innerHTML = '<option value="OTRO">-- ESCRIBIR MANUALMENTE --</option>';
selMun.disabled = false;
} }
} catch (e) {
console.error("Error de conexión con la DB geográfica");
} finally {
if (loader) loader.classList.add('hidden');
lucide.createIcons();
}
}
async function fetchGuilds() { async function fetchGuilds() {
try { try {