Skip to content

Commit

Permalink
perf(products): fix search behavior on the next page
Browse files Browse the repository at this point in the history
fixes #87
  • Loading branch information
riadhmouamnia committed Nov 12, 2023
1 parent 310517c commit 24ece57
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/components/ProductFiltering/Pagination.jsx
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -34,18 +35,18 @@ function Pagination({ page, pageSize, totalItems, queryParams, totalPages }) {
disabled={!hasPrev}
onClick={() => handleChangePage("prev")}
>
Previous
<BiLeftArrow /> Previous
</button>
<button
className='btn btn-sm normal-case font-normal tracking-wide'
disabled={!hasNext}
onClick={() => handleChangePage("next")}
>
Next
Next <BiRightArrow />
</button>
</div>
<div>
<p className='font-light text-sm tracking-wide'>
<p className='font-normal opacity-50 text-sm tracking-wide'>
page: {page} of {totalPages}
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,55 @@ exports[`renders correctly 1`] = `
disabled={true}
onClick={[Function]}
>
Previous
<svg
fill="currentColor"
height="1em"
stroke="currentColor"
strokeWidth="0"
style={
Object {
"color": undefined,
}
}
viewBox="0 0 24 24"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18.464 2.114a.998.998 0 0 0-1.033.063l-13 9a1.003 1.003 0 0 0 0 1.645l13 9A1 1 0 0 0 19 21V3a1 1 0 0 0-.536-.886zM17 19.091 6.757 12 17 4.909v14.182z"
/>
</svg>
Previous
</button>
<button
className="btn btn-sm normal-case font-normal tracking-wide"
disabled={true}
onClick={[Function]}
>
Next
Next
<svg
fill="currentColor"
height="1em"
stroke="currentColor"
strokeWidth="0"
style={
Object {
"color": undefined,
}
}
viewBox="0 0 24 24"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M5.536 21.886a1.004 1.004 0 0 0 1.033-.064l13-9a1 1 0 0 0 0-1.644l-13-9A.998.998 0 0 0 5 3v18a1 1 0 0 0 .536.886zM7 4.909 17.243 12 7 19.091V4.909z"
/>
</svg>
</button>
</div>
<div>
<p
className="font-light text-sm tracking-wide"
className="font-normal opacity-50 text-sm tracking-wide"
>
page:
1
Expand Down
1 change: 1 addition & 0 deletions src/components/SearchBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function SearchBar({ t, queryParams }) {
const newQueryParams = { ...queryParams };
if (query) {
newQueryParams.search = query;
delete newQueryParams.page;
} else {
delete newQueryParams.search;
}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/fetchCollectionWithQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit 24ece57

Please sign in to comment.