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

fix(pagination): use url search params to set default page value #117

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
8 changes: 4 additions & 4 deletions src/app/casos-activos/ofertas/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useState, useEffect } from 'react';
import { useEffect, useState } from 'react';
import { HeartHandshake } from 'lucide-react';
import { supabase } from '@/lib/supabase/client';
import Pagination from '@/components/Pagination';
Expand All @@ -18,7 +18,7 @@ export default function Ofertas() {
const [error, setError] = useState(null);

const [data, setData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [currentPage, setCurrentPage] = useState(Number(searchParams.get('page')) || 1);
const [currentCount, setCurrentCount] = useState(0);

const itemsPerPage = 10;
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function Ofertas() {

query.neq('status', 'finished');
// Ejecutar la consulta con paginación
const { data, count, error } = await query
const { data, error } = await query
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
.order('created_at', { ascending: false });

Expand All @@ -74,7 +74,7 @@ export default function Ofertas() {
setData([]);
} else {
setData(data || []);
setCurrentCount(count);
setCurrentCount(data.length);
}
} catch (err) {
console.log('Error general:', err);
Expand Down
25 changes: 11 additions & 14 deletions src/app/casos-activos/puntos/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { useState, useEffect } from 'react';
import { MapPin, Phone, Calendar, User, HeartHandshake, Users, Truck, Search, Package, MapPinIcon } from 'lucide-react';
import { useEffect, useState } from 'react';
import { HeartHandshake } from 'lucide-react';
import { supabase } from '@/lib/supabase/client';
import { tiposAyudaAcepta } from '@/helpers/constants';
import Pagination from '@/components/Pagination';
Expand All @@ -18,7 +18,7 @@ export default function Puntos() {
const [error, setError] = useState(null);

const [data, setData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [currentPage, setCurrentPage] = useState(Number(searchParams.get('page')) || 1);
const [currentCount, setCurrentCount] = useState(0);
const [cityOptions, setCityOptions] = useState([]);

Expand Down Expand Up @@ -56,20 +56,17 @@ export default function Puntos() {
try {
setError(null);

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

// Ejecutar la consulta
const { data, count, error } = await query;
const { data, error } = await supabase.from('distinct_collection_cities').select('city');
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);
return;
}

const trimmedCities = data.map((punto) => punto.city?.trim());
const cities = [...new Set(trimmedCities)].sort();
setCityOptions(cities || []);
setCurrentCount(cities.length);
} catch (err) {
console.log('Error general:', err);
setError('Error de conexión.');
Expand Down Expand Up @@ -119,7 +116,7 @@ export default function Puntos() {

fetchData();
}, [filtroData, currentPage]);

console.log(currentCount);
if (loading) {
return (
<div className="flex justify-center items-center min-h-screen">
Expand Down
6 changes: 3 additions & 3 deletions src/app/casos-activos/solicitudes/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useState, useEffect } from 'react';
import { useEffect, useState } from 'react';
import { HeartHandshake } from 'lucide-react';
import { supabase } from '@/lib/supabase/client';
import SolicitudCard from '@/components/SolicitudCard';
Expand Down Expand Up @@ -83,7 +83,7 @@ export default function Solicitudes() {
}
query.neq('status', 'finished');
// Ejecutar la consulta con paginación
const { data, count, error } = await query
const { data, error } = await query
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
.order('created_at', { ascending: false });

Expand All @@ -92,7 +92,7 @@ export default function Solicitudes() {
setData([]);
} else {
setData(data || []);
setCurrentCount(count);
setCurrentCount(data.length);
}
} catch (err) {
console.log('Error general:', err);
Expand Down
2 changes: 0 additions & 2 deletions src/components/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const Pagination = ({ currentPage, totalPages, onPageChange }) => {
>
Anterior
</button>

{/* Números de página */}
{getPageNumbers().map((page, index) => (
<button
Expand All @@ -64,7 +63,6 @@ const Pagination = ({ currentPage, totalPages, onPageChange }) => {
{page}
</button>
))}

{/* Botón de avance */}
<button
className={`px-3 py-1 border rounded-md text-gray-600 hover:bg-gray-100 ${
Expand Down