Pagination component

This commit is contained in:
Brad Traversy
2024-07-15 12:36:36 -04:00
parent 3b9309e64c
commit fada26f8a3
3 changed files with 46 additions and 40 deletions
+4 -36
View File
@@ -616,48 +616,16 @@
</section> </section>
<!-- Pagination --> <!-- Pagination -->
<section class="container mx-auto flex justify-center my-8"> <section class="container mx-auto flex justify-center items-center my-8">
<nav <a class="mr-2 px-2 py-1 border border-gray-300 rounded" href="#">
class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px"
aria-label="Pagination"
>
<!-- Previous Button -->
<a
href="#"
class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50"
>
<span class="sr-only">Previous</span>
<!-- You can use an icon here, for example: <i class="fas fa-chevron-left"></i> -->
Previous Previous
</a> </a>
<!-- Page Numbers --> <span class="mx-2">Page 1 of 4</span>
<a
href="#"
class="bg-blue-500 text-white hover:bg-blue-600 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium"
>1</a
>
<a
href="#"
class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50"
>2</a
>
<a
href="#"
class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50"
>3</a
>
<!-- Next Button --> <a class="ml-2 px-2 py-1 border border-gray-300 rounded" href="#">
<a
href="#"
class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50"
>
<span class="sr-only">Next</span>
<!-- You can use an icon here, for example: <i class="fas fa-chevron-right"></i> -->
Next Next
</a> </a>
</nav>
</section> </section>
<!-- Footer --> <!-- Footer -->
+6
View File
@@ -1,5 +1,6 @@
import PropertyCard from '@/components/PropertyCard'; import PropertyCard from '@/components/PropertyCard';
import PropertySearchForm from '@/components/PropertySearchForm'; import PropertySearchForm from '@/components/PropertySearchForm';
import Pagination from '@/components/Pagination';
import Property from '@/models/Property'; import Property from '@/models/Property';
import connectDB from '@/config/database'; import connectDB from '@/config/database';
@@ -29,6 +30,11 @@ const PropertiesPage = async ({ searchParams: { pageSize = 3, page = 1 } }) => {
))} ))}
</div> </div>
)} )}
<Pagination
page={parseInt(page)}
pageSize={parseInt(pageSize)}
totalItems={total}
/>
</div> </div>
</section> </section>
</> </>
+32
View File
@@ -0,0 +1,32 @@
import Link from 'next/link';
const Pagination = ({ page, pageSize, totalItems }) => {
const totalPages = Math.ceil(totalItems / pageSize);
return (
<section className='container mx-auto flex justify-center items-center my-8'>
{page > 1 ? (
<Link
className='mr-2 px-2 py-1 border border-gray-300 rounded'
href={`/properties?page=${page - 1}`}
>
Previous
</Link>
) : null}
<span className='mx-2'>
{' '}
Page {page} of {totalPages}
</span>
{page < totalPages ? (
<Link
className='ml-2 px-2 py-1 border border-gray-300 rounded'
href={`/properties?page=${page + 1}`}
>
Next
</Link>
) : null}
</section>
);
};
export default Pagination;