refactor(filtros): extrae la UI de filtros a BarraFiltros (neutro)
App.jsx conserva el estado y localesFiltrados; el componente pinta exactamente los controles anteriores. 139/139 tests en verde.
This commit is contained in:
+18
-58
@@ -1,15 +1,16 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { comprobarDuplicado, contienepalabrasProhibidas, enviarPropuesta, obtenerPalabrasFiltradas, suscribirLocales } from "./firebase.js";
|
import { comprobarDuplicado, contienepalabrasProhibidas, enviarPropuesta, obtenerPalabrasFiltradas, suscribirLocales } from "./firebase.js";
|
||||||
import { CATEGORIAS, COLORES_MAPA, NOMBRES_CATEGORIAS, obtenerCategoria } from "./constants/categorias.js";
|
import { CATEGORIAS, COLORES_MAPA, NOMBRES_CATEGORIAS, obtenerCategoria } from "./constants/categorias.js";
|
||||||
import { COORDS_PROVINCIAS, PROVINCIAS, CENTRO_ESPANA } from "./constants/provincias.js";
|
import { COORDS_PROVINCIAS, CENTRO_ESPANA } from "./constants/provincias.js";
|
||||||
import { distanciaKm as calcularDistanciaKm, enlaceGoogleMapsDesdeLocal, enlaceWazeDesdeLocal } from "./utils/geo.js";
|
import { distanciaKm as calcularDistanciaKm, enlaceGoogleMapsDesdeLocal, enlaceWazeDesdeLocal } from "./utils/geo.js";
|
||||||
import { inputStyle, ANCHO_CUERPO } from "./styles/shared.js";
|
import { ANCHO_CUERPO } from "./styles/shared.js";
|
||||||
import LocalCard from "./components/LocalCard.jsx";
|
import LocalCard from "./components/LocalCard.jsx";
|
||||||
import BannerMovil from "./components/BannerMovil.jsx";
|
import BannerMovil from "./components/BannerMovil.jsx";
|
||||||
import ModalPrivacidad from "./components/ModalPrivacidad.jsx";
|
import ModalPrivacidad from "./components/ModalPrivacidad.jsx";
|
||||||
import BotonFlotante from "./components/BotonFlotante.jsx";
|
import BotonFlotante from "./components/BotonFlotante.jsx";
|
||||||
import BotonProponerArriba from "./components/BotonProponerArriba.jsx";
|
import BotonProponerArriba from "./components/BotonProponerArriba.jsx";
|
||||||
import ModalPasarela from "./components/ModalPasarela.jsx";
|
import ModalPasarela from "./components/ModalPasarela.jsx";
|
||||||
|
import BarraFiltros from "./components/BarraFiltros.jsx";
|
||||||
import useFavoritos from "./hooks/useFavoritos.js";
|
import useFavoritos from "./hooks/useFavoritos.js";
|
||||||
import useUbicacion from "./hooks/useUbicacion.js";
|
import useUbicacion from "./hooks/useUbicacion.js";
|
||||||
|
|
||||||
@@ -161,8 +162,6 @@ export default function App() {
|
|||||||
guardarPropuesta(posibleDuplicado.lat, posibleDuplicado.lng);
|
guardarPropuesta(posibleDuplicado.lat, posibleDuplicado.lng);
|
||||||
};
|
};
|
||||||
|
|
||||||
const subcatsFiltro = filtroCategoria ? CATEGORIAS[filtroCategoria]?.subcategorias || [] : [];
|
|
||||||
|
|
||||||
const localesFiltrados = locales
|
const localesFiltrados = locales
|
||||||
.filter((l) => {
|
.filter((l) => {
|
||||||
const q = busqueda.toLowerCase();
|
const q = busqueda.toLowerCase();
|
||||||
@@ -252,7 +251,6 @@ export default function App() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const conteosCat = NOMBRES_CATEGORIAS.reduce((acc, c) => { acc[c] = locales.filter((l) => l.categoria === c).length; return acc; }, {});
|
|
||||||
const provincias = [...new Set(locales.map((l) => l.provincia))].sort();
|
const provincias = [...new Set(locales.map((l) => l.provincia))].sort();
|
||||||
const mediaGlobal = locales.length ? (locales.reduce((s, l) => s + l.puntuacion, 0) / locales.length).toFixed(1) : "—";
|
const mediaGlobal = locales.length ? (locales.reduce((s, l) => s + l.puntuacion, 0) / locales.length).toFixed(1) : "—";
|
||||||
|
|
||||||
@@ -296,22 +294,21 @@ export default function App() {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Chips categorías */}
|
{/* Barra de filtros (sección única: chips de categorías y, en vista
|
||||||
<div style={{ display: "flex", flexWrap: "wrap", gap: 6, marginBottom: "1.25rem" }}>
|
lista, búsqueda + selects + acciones rápidas). El estado vive aquí
|
||||||
{NOMBRES_CATEGORIAS.map((cat) => {
|
y se pasa por props; la derivación localesFiltrados es compartida
|
||||||
const c = CATEGORIAS[cat];
|
con el mapa. */}
|
||||||
const activo = filtroCategoria === cat;
|
<BarraFiltros
|
||||||
return (
|
vista={vista} locales={locales} favoritos={favoritos}
|
||||||
<button key={cat} onClick={() => { setFiltroCategoria(activo ? "" : cat); setFiltroSubcat(""); }}
|
busqueda={busqueda} setBusqueda={setBusqueda}
|
||||||
style={{ background: activo ? c.bgLight : "var(--color-background-secondary)", color: activo ? c.textColor : "var(--color-text-secondary)", border: `0.5px solid ${activo ? c.textColor : "var(--color-border-tertiary)"}`, borderRadius: 20, padding: "5px 12px", fontSize: 12, fontWeight: activo ? 500 : 400, cursor: "pointer", display: "flex", alignItems: "center", gap: 5, transition: "all 0.15s" }}
|
filtroCategoria={filtroCategoria} setFiltroCategoria={setFiltroCategoria}
|
||||||
aria-pressed={activo}>
|
filtroSubcat={filtroSubcat} setFiltroSubcat={setFiltroSubcat}
|
||||||
{c.emoji} {cat}
|
filtroProvincia={filtroProvincia} setFiltroProvincia={setFiltroProvincia}
|
||||||
{conteosCat[cat] > 0 && <span style={{ background: activo ? c.textColor : "var(--color-border-secondary)", color: activo ? "white" : "var(--color-text-secondary)", borderRadius: 10, padding: "0 6px", fontSize: 11 }}>{conteosCat[cat]}</span>}
|
orden={orden} setOrden={setOrden}
|
||||||
</button>
|
soloFavoritos={soloFavoritos} setSoloFavoritos={setSoloFavoritos}
|
||||||
);
|
ubicacion={ubicacion} buscandoUbicacion={buscandoUbicacion} errorUbicacion={errorUbicacion}
|
||||||
})}
|
pedirUbicacion={pedirUbicacion} limpiarUbicacion={limpiarUbicacion}
|
||||||
{filtroCategoria && <button onClick={() => { setFiltroCategoria(""); setFiltroSubcat(""); }} style={{ background: "none", border: "0.5px solid var(--color-border-secondary)", borderRadius: 20, padding: "5px 10px", fontSize: 12, cursor: "pointer", color: "var(--color-text-tertiary)" }}>✕ limpiar</button>}
|
/>
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
{/* Tabs */}
|
{/* Tabs */}
|
||||||
@@ -326,43 +323,6 @@ export default function App() {
|
|||||||
|
|
||||||
{vista === "lista" && (
|
{vista === "lista" && (
|
||||||
<>
|
<>
|
||||||
<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr auto", gap: 8, marginBottom: 8 }}>
|
|
||||||
<input style={inputStyle} placeholder="🔍 Buscar por nombre, dirección..." value={busqueda} onChange={(e) => setBusqueda(e.target.value)} aria-label="Buscar por nombre, dirección o categoría" />
|
|
||||||
<select style={{ ...inputStyle, opacity: filtroCategoria ? 1 : 0.6 }} value={filtroSubcat} onChange={(e) => setFiltroSubcat(e.target.value)} disabled={!filtroCategoria} aria-label="Filtrar por tipo específico">
|
|
||||||
<option value="">{filtroCategoria ? "Todos los tipos" : "Elige categoría arriba"}</option>
|
|
||||||
{subcatsFiltro.map((s) => <option key={s} value={s}>{s}</option>)}
|
|
||||||
</select>
|
|
||||||
<select style={inputStyle} value={filtroProvincia} onChange={(e) => setFiltroProvincia(e.target.value)} aria-label="Filtrar por provincia">
|
|
||||||
<option value="">Todas las provincias</option>
|
|
||||||
{PROVINCIAS.map((p) => <option key={p} value={p}>{p}</option>)}
|
|
||||||
</select>
|
|
||||||
<select style={{ ...inputStyle, width: "auto" }} value={orden} onChange={(e) => setOrden(e.target.value)} aria-label="Ordenar locales">
|
|
||||||
<option value="fecha">Reciente</option>
|
|
||||||
<option value="puntuacion">★ Mejor</option>
|
|
||||||
<option value="nombre">A-Z</option>
|
|
||||||
{ubicacion && <option value="distancia">📍 Más cerca</option>}
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap", marginBottom: "1rem" }}>
|
|
||||||
{!ubicacion ? (
|
|
||||||
<button onClick={pedirUbicacion} disabled={buscandoUbicacion}
|
|
||||||
style={{ display: "flex", alignItems: "center", gap: 5, background: "none", border: "0.5px solid var(--color-border-secondary)", borderRadius: 20, padding: "5px 12px", fontSize: 12, color: "var(--color-text-secondary)", cursor: "pointer" }}>
|
|
||||||
<i className="ti ti-current-location" aria-hidden="true"></i> {buscandoUbicacion ? "Localizando..." : "Cerca de mí"}
|
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
<button onClick={() => { limpiarUbicacion(); if (orden === "distancia") setOrden("fecha"); }}
|
|
||||||
style={{ display: "flex", alignItems: "center", gap: 5, background: "#EAFAF1", border: "0.5px solid #A9DFBF", borderRadius: 20, padding: "5px 12px", fontSize: 12, color: "#1E8449", cursor: "pointer" }}>
|
|
||||||
<i className="ti ti-map-pin-check" aria-hidden="true"></i> Ubicación activada ✕
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
{errorUbicacion && <span style={{ fontSize: 12, color: "#922B21" }}>{errorUbicacion}</span>}
|
|
||||||
<button onClick={() => setSoloFavoritos((v) => !v)} aria-pressed={soloFavoritos}
|
|
||||||
style={{ display: "flex", alignItems: "center", gap: 5, background: soloFavoritos ? "#FDEDEC" : "none", border: `0.5px solid ${soloFavoritos ? "#8B0000" : "var(--color-border-secondary)"}`, borderRadius: 20, padding: "5px 12px", fontSize: 12, color: soloFavoritos ? "#8B0000" : "var(--color-text-secondary)", cursor: "pointer" }}>
|
|
||||||
<i className={soloFavoritos ? "ti ti-heart-filled" : "ti ti-heart"} aria-hidden="true"></i> Favoritos {favoritos.length > 0 && `(${favoritos.length})`}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<div style={{ textAlign: "center", padding: "3rem", color: "var(--color-text-tertiary)", fontSize: 14 }}>
|
<div style={{ textAlign: "center", padding: "3rem", color: "var(--color-text-tertiary)", fontSize: 14 }}>
|
||||||
<i className="ti ti-loader" style={{ fontSize: 24, display: "block", marginBottom: 8 }} aria-hidden="true"></i>
|
<i className="ti ti-loader" style={{ fontSize: 24, display: "block", marginBottom: 8 }} aria-hidden="true"></i>
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { CATEGORIAS, NOMBRES_CATEGORIAS } from "../constants/categorias.js";
|
||||||
|
import { PROVINCIAS } from "../constants/provincias.js";
|
||||||
|
import { inputStyle } from "../styles/shared.js";
|
||||||
|
|
||||||
|
// UI de filtrado del directorio. El estado vive en App.jsx (la derivación de
|
||||||
|
// localesFiltrados la comparten lista y mapa); este componente solo pinta y
|
||||||
|
// dispara los setters recibidos por props.
|
||||||
|
// Refactor neutro inicial: reproduce los controles que antes vivían en App
|
||||||
|
// (chips de categorías + grid búsqueda/selects + fila ubicación/favoritos).
|
||||||
|
export default function BarraFiltros({
|
||||||
|
vista,
|
||||||
|
locales,
|
||||||
|
favoritos,
|
||||||
|
busqueda, setBusqueda,
|
||||||
|
filtroCategoria, setFiltroCategoria,
|
||||||
|
filtroSubcat, setFiltroSubcat,
|
||||||
|
filtroProvincia, setFiltroProvincia,
|
||||||
|
orden, setOrden,
|
||||||
|
soloFavoritos, setSoloFavoritos,
|
||||||
|
ubicacion, buscandoUbicacion, errorUbicacion, pedirUbicacion, limpiarUbicacion,
|
||||||
|
}) {
|
||||||
|
const conteosCat = NOMBRES_CATEGORIAS.reduce((acc, c) => { acc[c] = locales.filter((l) => l.categoria === c).length; return acc; }, {});
|
||||||
|
const subcatsFiltro = filtroCategoria ? CATEGORIAS[filtroCategoria]?.subcategorias || [] : [];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* Chips categorías */}
|
||||||
|
<div style={{ display: "flex", flexWrap: "wrap", gap: 6, marginBottom: "1.25rem" }}>
|
||||||
|
{NOMBRES_CATEGORIAS.map((cat) => {
|
||||||
|
const c = CATEGORIAS[cat];
|
||||||
|
const activo = filtroCategoria === cat;
|
||||||
|
return (
|
||||||
|
<button key={cat} onClick={() => { setFiltroCategoria(activo ? "" : cat); setFiltroSubcat(""); }}
|
||||||
|
style={{ background: activo ? c.bgLight : "var(--color-background-secondary)", color: activo ? c.textColor : "var(--color-text-secondary)", border: `0.5px solid ${activo ? c.textColor : "var(--color-border-tertiary)"}`, borderRadius: 20, padding: "5px 12px", fontSize: 12, fontWeight: activo ? 500 : 400, cursor: "pointer", display: "flex", alignItems: "center", gap: 5, transition: "all 0.15s" }}
|
||||||
|
aria-pressed={activo}>
|
||||||
|
{c.emoji} {cat}
|
||||||
|
{conteosCat[cat] > 0 && <span style={{ background: activo ? c.textColor : "var(--color-border-secondary)", color: activo ? "white" : "var(--color-text-secondary)", borderRadius: 10, padding: "0 6px", fontSize: 11 }}>{conteosCat[cat]}</span>}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{filtroCategoria && <button onClick={() => { setFiltroCategoria(""); setFiltroSubcat(""); }} style={{ background: "none", border: "0.5px solid var(--color-border-secondary)", borderRadius: 20, padding: "5px 10px", fontSize: 12, cursor: "pointer", color: "var(--color-text-tertiary)" }}>✕ limpiar</button>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{vista === "lista" && (
|
||||||
|
<>
|
||||||
|
<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr auto", gap: 8, marginBottom: 8 }}>
|
||||||
|
<input style={inputStyle} placeholder="🔍 Buscar por nombre, dirección..." value={busqueda} onChange={(e) => setBusqueda(e.target.value)} aria-label="Buscar por nombre, dirección o categoría" />
|
||||||
|
<select style={{ ...inputStyle, opacity: filtroCategoria ? 1 : 0.6 }} value={filtroSubcat} onChange={(e) => setFiltroSubcat(e.target.value)} disabled={!filtroCategoria} aria-label="Filtrar por tipo específico">
|
||||||
|
<option value="">{filtroCategoria ? "Todos los tipos" : "Elige categoría arriba"}</option>
|
||||||
|
{subcatsFiltro.map((s) => <option key={s} value={s}>{s}</option>)}
|
||||||
|
</select>
|
||||||
|
<select style={inputStyle} value={filtroProvincia} onChange={(e) => setFiltroProvincia(e.target.value)} aria-label="Filtrar por provincia">
|
||||||
|
<option value="">Todas las provincias</option>
|
||||||
|
{PROVINCIAS.map((p) => <option key={p} value={p}>{p}</option>)}
|
||||||
|
</select>
|
||||||
|
<select style={{ ...inputStyle, width: "auto" }} value={orden} onChange={(e) => setOrden(e.target.value)} aria-label="Ordenar locales">
|
||||||
|
<option value="fecha">Reciente</option>
|
||||||
|
<option value="puntuacion">★ Mejor</option>
|
||||||
|
<option value="nombre">A-Z</option>
|
||||||
|
{ubicacion && <option value="distancia">📍 Más cerca</option>}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: "flex", alignItems: "center", gap: 10, flexWrap: "wrap", marginBottom: "1rem" }}>
|
||||||
|
{!ubicacion ? (
|
||||||
|
<button onClick={pedirUbicacion} disabled={buscandoUbicacion}
|
||||||
|
style={{ display: "flex", alignItems: "center", gap: 5, background: "none", border: "0.5px solid var(--color-border-secondary)", borderRadius: 20, padding: "5px 12px", fontSize: 12, color: "var(--color-text-secondary)", cursor: "pointer" }}>
|
||||||
|
<i className="ti ti-current-location" aria-hidden="true"></i> {buscandoUbicacion ? "Localizando..." : "Cerca de mí"}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button onClick={() => { limpiarUbicacion(); if (orden === "distancia") setOrden("fecha"); }}
|
||||||
|
style={{ display: "flex", alignItems: "center", gap: 5, background: "#EAFAF1", border: "0.5px solid #A9DFBF", borderRadius: 20, padding: "5px 12px", fontSize: 12, color: "#1E8449", cursor: "pointer" }}>
|
||||||
|
<i className="ti ti-map-pin-check" aria-hidden="true"></i> Ubicación activada ✕
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{errorUbicacion && <span style={{ fontSize: 12, color: "#922B21" }}>{errorUbicacion}</span>}
|
||||||
|
<button onClick={() => setSoloFavoritos((v) => !v)} aria-pressed={soloFavoritos}
|
||||||
|
style={{ display: "flex", alignItems: "center", gap: 5, background: soloFavoritos ? "#FDEDEC" : "none", border: `0.5px solid ${soloFavoritos ? "#8B0000" : "var(--color-border-secondary)"}`, borderRadius: 20, padding: "5px 12px", fontSize: 12, color: soloFavoritos ? "#8B0000" : "var(--color-text-secondary)", cursor: "pointer" }}>
|
||||||
|
<i className={soloFavoritos ? "ti ti-heart-filled" : "ti ti-heart"} aria-hidden="true"></i> Favoritos {favoritos.length > 0 && `(${favoritos.length})`}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user