Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(punto-recogida) - Add city filtering #112

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/app/casos-activos/puntos/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Puntos() {
const [data, setData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [currentCount, setCurrentCount] = useState(0);
const [cityOptions, setCityOptions] = useState([]);

const itemsPerPage = 10;
const numPages = (count) => {
Expand All @@ -34,6 +35,7 @@ export default function Puntos() {

const [filtroData, setFiltroData] = useState({
acepta: searchParams.get('acepta') || 'todos',
ciudad: searchParams.get('ciudad') || 'todas',
});

const changeDataFilter = (type, newFilter) => {
Expand All @@ -49,6 +51,34 @@ export default function Puntos() {
updateFilter('page', newPage);
}

useEffect(() => {
async function fetchCities() {
try {
setError(null);

// Comenzamos la consulta
const query = supabase.from('distinct_collection_cities').select('city');

// Ejecutar la consulta
const { data, count, error } = await query;
if (error) {
console.log('Error fetching ciudades:', error);
setCityOptions([]);
} else {
const trimmedCities = data.map((punto) => punto.city.trim());
const cities = [...new Set(trimmedCities)].sort();
setCityOptions(cities || []);
setCurrentCount(count);
}
} catch (err) {
console.log('Error general:', err);
setError('Error de conexión.');
}
}

fetchCities();
}, []);

useEffect(() => {
async function fetchData() {
try {
Expand All @@ -62,6 +92,11 @@ export default function Puntos() {
if (filtroData.ayuda !== 'todas') {
query.contains('accepted_items', [filtroData.ayuda]);
}

if (filtroData.ciudad !== 'todas') {
query.ilike('city', filtroData.ciudad);
}

// Ejecutar la consulta con paginación
const { data, count, error } = await query
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
Expand Down Expand Up @@ -106,7 +141,8 @@ export default function Puntos() {
{/* FILTROS */}
<div className="flex flex-col sm:flex-row gap-2 items-center justify-between">
<p className="font-bold text-md">Filtros</p>
<div className="flex flex-col sm:flex-row gap-2 w-full justify-end">
<div className="flex flex-col sm:flex-row gap-2 items-center w-full justify-end">
<h2 className="font-semibold">Se acepta:</h2>
<select
value={filtroData.ayuda}
onChange={(e) => changeDataFilter('ayuda', e.target.value)}
Expand All @@ -120,6 +156,21 @@ export default function Puntos() {
))}
</select>
</div>
<div className="flex flex-col sm:flex-row gap-2 items-center w-full justify-end">
<h2 className="font-semibold">Ciudad:</h2>
<select
value={filtroData.ciudad}
onChange={(e) => changeDataFilter('ciudad', e.target.value)}
className="px-4 py-2 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white text-gray-900 shadow-sm"
>
<option value="todas">Todas las ciudades</option>
{cityOptions.map((city, i) => (
<option key={i} value={city}>
{city}
</option>
))}
</select>
</div>
</div>
<div className="grid gap-4">
{data.length === 0 ? (
Expand All @@ -135,7 +186,7 @@ export default function Puntos() {
className="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600 flex items-center gap-2 whitespace-nowrap"
>
<HeartHandshake className="w-5 h-5" />
Ofrecer ayuda a {filtroData.pueblo === 'todos' ? '' : towns[filtroData.pueblo - 1].name}
Crear punto de ayuda en {filtroData.ciudad === 'todos' ? '' : filtroData.ciudad}
</button>
</div>
) : (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create or replace view "public"."distinct_collection_cities" as SELECT DISTINCT collection_points.city
FROM collection_points;