El objetivo de estos cambios es, por un lado, mantener los datos
actualizados y, por otro, mejorar la experiencia para quienes lo usan:
- Fiabilidad: los locales se pueden confirmar ("Sigue aquí") y
reportar (cerrado, datos incorrectos...), y se avisa de duplicados
antes de publicar, para que el contenido no se quede obsoleto ni
repetido.
- Usabilidad: autocompletado al proponer, favoritos, "cerca de mí",
compartir fichas, enlaces a Waze y una política de privacidad
visible, para que sea más sencillo aportar y encontrar locales.
104 lines
4.1 KiB
React
104 lines
4.1 KiB
React
import { useEffect, useRef, useState } from "react";
|
|
import { buscarLugares } from "../utils/geo.js";
|
|
import { PROVINCIAS } from "../constants/provincias.js";
|
|
import { inputStyle } from "../styles/shared.js";
|
|
|
|
const normaliza = (s) => (s || "").toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").trim();
|
|
|
|
function emparejarProvincia(texto) {
|
|
const t = normaliza(texto);
|
|
if (!t) return "";
|
|
return PROVINCIAS.find((p) => normaliza(p) === t || t.includes(normaliza(p))) || "";
|
|
}
|
|
|
|
// Campo "Nombre del local" con autocompletar: al escribir, busca lugares (OpenStreetMap)
|
|
// y permite seleccionar uno para rellenar automáticamente nombre, dirección, provincia y coordenadas.
|
|
export default function AutocompletarLocal({ form, setForm }) {
|
|
const [sugerencias, setSugerencias] = useState([]);
|
|
const [abierto, setAbierto] = useState(false);
|
|
const [buscando, setBuscando] = useState(false);
|
|
const debounceRef = useRef(null);
|
|
const cajaRef = useRef(null);
|
|
|
|
useEffect(() => {
|
|
function alClicFuera(e) {
|
|
if (cajaRef.current && !cajaRef.current.contains(e.target)) setAbierto(false);
|
|
}
|
|
document.addEventListener("mousedown", alClicFuera);
|
|
return () => document.removeEventListener("mousedown", alClicFuera);
|
|
}, []);
|
|
|
|
const handleChange = (val) => {
|
|
setForm((f) => ({ ...f, nombre: val }));
|
|
setAbierto(true);
|
|
clearTimeout(debounceRef.current);
|
|
if (val.trim().length < 3) { setSugerencias([]); return; }
|
|
debounceRef.current = setTimeout(async () => {
|
|
setBuscando(true);
|
|
try {
|
|
setSugerencias(await buscarLugares(val));
|
|
} catch {
|
|
setSugerencias([]);
|
|
}
|
|
setBuscando(false);
|
|
}, 450);
|
|
};
|
|
|
|
const seleccionar = (lugar) => {
|
|
setForm((f) => ({
|
|
...f,
|
|
nombre: lugar.nombre,
|
|
direccion: lugar.direccion,
|
|
enlaceGoogleMaps: "",
|
|
lat: lugar.lat,
|
|
lng: lugar.lng,
|
|
provincia: emparejarProvincia(lugar.provincia) || f.provincia,
|
|
}));
|
|
setSugerencias([]);
|
|
setAbierto(false);
|
|
};
|
|
|
|
return (
|
|
<div ref={cajaRef} style={{ position: "relative" }}>
|
|
<label htmlFor="campo-nombre" style={{ fontSize: 12, color: "var(--color-text-secondary)", display: "block", marginBottom: 4 }}>Nombre del local *</label>
|
|
<input
|
|
id="campo-nombre"
|
|
style={inputStyle}
|
|
autoComplete="off"
|
|
placeholder="Bar El Olivo, Clínica San José..."
|
|
value={form.nombre}
|
|
onChange={(e) => handleChange(e.target.value)}
|
|
onFocus={() => sugerencias.length && setAbierto(true)}
|
|
role="combobox"
|
|
aria-expanded={abierto && sugerencias.length > 0}
|
|
aria-autocomplete="list"
|
|
/>
|
|
{buscando && (
|
|
<i className="ti ti-loader" aria-hidden="true" style={{ position: "absolute", right: 10, top: 32, fontSize: 14, color: "var(--color-text-tertiary)" }}></i>
|
|
)}
|
|
{abierto && sugerencias.length > 0 && (
|
|
<div role="listbox" style={{
|
|
position: "absolute", zIndex: 20, top: "100%", left: 0, right: 0, marginTop: 4,
|
|
background: "var(--color-background-primary)", border: "0.5px solid var(--color-border-secondary)",
|
|
borderRadius: "var(--border-radius-md)", boxShadow: "0 6px 18px rgba(0,0,0,0.15)",
|
|
maxHeight: 240, overflowY: "auto",
|
|
}}>
|
|
{sugerencias.map((s) => (
|
|
<button key={s.id} type="button" role="option" onClick={() => seleccionar(s)}
|
|
style={{
|
|
display: "block", width: "100%", textAlign: "left", background: "none", border: "none",
|
|
borderBottom: "0.5px solid var(--color-border-tertiary)", padding: "8px 10px", cursor: "pointer",
|
|
}}>
|
|
<div style={{ fontSize: 13, fontWeight: 500, color: "var(--color-text-primary)" }}>{s.nombre}</div>
|
|
<div style={{ fontSize: 11, color: "var(--color-text-secondary)", marginTop: 2 }}>{s.direccion}</div>
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
<p style={{ fontSize: 11, color: "var(--color-text-tertiary)", margin: "4px 0 0" }}>
|
|
Escribe el nombre y elige una opción para rellenar dirección y ubicación automáticamente.
|
|
</p>
|
|
</div>
|
|
);
|
|
}
|