From 24ece57713a461315c84b9d88661c2398ac406f1 Mon Sep 17 00:00:00 2001 From: Riadh mouamnia Date: Sun, 12 Nov 2023 20:25:23 +0100 Subject: [PATCH] perf(products): fix search behavior on the next page fixes #87 --- .../ProductFiltering/Pagination.jsx | 7 ++-- .../__snapshots__/Pagination.test.js.snap | 42 +++++++++++++++++-- src/components/SearchBar/index.jsx | 1 + src/lib/fetchCollectionWithQuery.js | 10 ++--- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/components/ProductFiltering/Pagination.jsx b/src/components/ProductFiltering/Pagination.jsx index 01360f5..86f8ebb 100644 --- a/src/components/ProductFiltering/Pagination.jsx +++ b/src/components/ProductFiltering/Pagination.jsx @@ -1,4 +1,5 @@ import { useRouter } from "next/router"; +import { BiLeftArrow, BiRightArrow } from "react-icons/bi"; function Pagination({ page, pageSize, totalItems, queryParams, totalPages }) { const router = useRouter(); @@ -34,18 +35,18 @@ function Pagination({ page, pageSize, totalItems, queryParams, totalPages }) { disabled={!hasPrev} onClick={() => handleChangePage("prev")} > - Previous + Previous
-

+

page: {page} of {totalPages}

diff --git a/src/components/ProductFiltering/__test__/__snapshots__/Pagination.test.js.snap b/src/components/ProductFiltering/__test__/__snapshots__/Pagination.test.js.snap index 9ebd25b..f07726b 100644 --- a/src/components/ProductFiltering/__test__/__snapshots__/Pagination.test.js.snap +++ b/src/components/ProductFiltering/__test__/__snapshots__/Pagination.test.js.snap @@ -12,19 +12,55 @@ exports[`renders correctly 1`] = ` disabled={true} onClick={[Function]} > - Previous + + + + Previous

page: 1 diff --git a/src/components/SearchBar/index.jsx b/src/components/SearchBar/index.jsx index e974657..3c15f97 100644 --- a/src/components/SearchBar/index.jsx +++ b/src/components/SearchBar/index.jsx @@ -13,6 +13,7 @@ function SearchBar({ t, queryParams }) { const newQueryParams = { ...queryParams }; if (query) { newQueryParams.search = query; + delete newQueryParams.page; } else { delete newQueryParams.search; } diff --git a/src/lib/fetchCollectionWithQuery.js b/src/lib/fetchCollectionWithQuery.js index 16fa42f..67b57af 100644 --- a/src/lib/fetchCollectionWithQuery.js +++ b/src/lib/fetchCollectionWithQuery.js @@ -55,23 +55,21 @@ export async function fetchCollectionWithQuery( q = query(q, startAfter(startAfterDoc)); } - if (!queryParams.search) { - q = query(q, limit(pageSize)); - } + q = query(q, limit(pageSize)); try { const querySnapshot = await getDocs(q); let totalItems = 0; //get all items count - totalItems = (await getDocs(collectionRef)).size; - - //get only filtred items count if (Object.entries(queryParams).length !== 0 && !queryParams.page) { totalItems = (await getDocs(q)).size; + } else { + totalItems = (await getDocs(collectionRef)).size; } const totalPages = Math.ceil(totalItems / pageSize); + console.log({ totalPages, totalItems }); const items = [];