Skip to content

Commit d507391

Browse files
pagination code done but not reflecting products on UI(have to fix it)
1 parent 82c96b3 commit d507391

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

src/app/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ITEMS_PER_PAGE = 6;

src/features/product/components/ProductList.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PlusIcon,
1717
Squares2X2Icon,
1818
} from "@heroicons/react/20/solid";
19+
import { ITEMS_PER_PAGE } from "../../../app/constants";
1920

2021
const sortOptions = [
2122
{ name: "Best Rating", sort: "rating", current: false },
@@ -66,6 +67,7 @@ export default function ProductList() {
6667
const dispatch = useDispatch();
6768
const [filter, setFilter] = useState({});
6869
const [sort, setSort] = useState({});
70+
const [page, setPage] = useState(1);
6971

7072
const handleFilter = (e, section, option) => {
7173
console.log(e.target.checked);
@@ -95,10 +97,15 @@ export default function ProductList() {
9597
console.log(sort);
9698
setSort(sort);
9799
};
100+
const handlePage = (page) => {
101+
console.log({ page });
102+
setPage(page);
103+
};
98104
// making API call when dispatch or when filter is applied in a one go....
99105
useEffect(() => {
100-
dispatch(fetchProductsByFiltersAsync({ filter, sort }));
101-
}, [dispatch, filter, sort]);
106+
const pagination = { _page: page, _per_page: ITEMS_PER_PAGE };
107+
dispatch(fetchProductsByFiltersAsync({ filter, sort, pagination }));
108+
}, [dispatch, filter, sort, page]);
102109

103110
return (
104111
<div className="bg-white">
@@ -201,7 +208,7 @@ export default function ProductList() {
201208

202209
{/* section of product and filters ends */}
203210

204-
<Pagination />
211+
<Pagination page={page} setPage={setPage} handlePage={handlePage} />
205212
</main>
206213
</div>
207214
</div>
@@ -430,7 +437,7 @@ function ProductGrid({ products }) {
430437
);
431438
}
432439

433-
function Pagination() {
440+
function Pagination({ page, setPage, handlePage, totalItems = 30 }) {
434441
return (
435442
<div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6">
436443
<div className="flex flex-1 justify-between sm:hidden">
@@ -450,9 +457,15 @@ function Pagination() {
450457
<div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between">
451458
<div>
452459
<p className="text-sm text-gray-700">
453-
Showing <span className="font-medium">1</span> to{" "}
454-
<span className="font-medium">10</span> of{" "}
455-
<span className="font-medium">97</span> results
460+
Showing
461+
{/* (3-1) * 6 + 1 =13 */}
462+
<span className="font-medium">
463+
{(page - 1) * ITEMS_PER_PAGE + 1}
464+
</span>{" "}
465+
{/* 3 * 6 = 18 */}
466+
to <span className="font-medium">
467+
{page * ITEMS_PER_PAGE}{" "}
468+
</span> of <span className="font-medium">{totalItems}</span> results
456469
</p>
457470
</div>
458471
<div>
@@ -468,19 +481,22 @@ function Pagination() {
468481
<ChevronLeftIcon className="h-5 w-5" aria-hidden="true" />
469482
</a>
470483
{/* Current: "z-10 bg-indigo-600 text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600", Default: "text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:outline-offset-0" */}
471-
<a
472-
href="#"
473-
aria-current="page"
474-
className="relative z-10 inline-flex items-center bg-indigo-600 px-4 py-2 text-sm font-semibold text-white focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
475-
>
476-
1
477-
</a>
478-
<a
479-
href="#"
480-
className="relative inline-flex items-center px-4 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0"
481-
>
482-
2
483-
</a>
484+
{Array.from({ length: Math.ceil(totalItems / ITEMS_PER_PAGE) }).map(
485+
//extracting all the indexes of array
486+
(el, index) => (
487+
<div
488+
onClick={() => handlePage(index + 1)}
489+
aria-current="page"
490+
className={`relative z-10 inline-flex items-center ${
491+
index + 1 === page
492+
? "bg-indigo-600 text-white"
493+
: "text-gray-400 "
494+
} px-4 py-2 text-sm font-semibold cursor-pointer focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600`}
495+
>
496+
{index + 1}
497+
</div>
498+
)
499+
)}
484500

485501
<a
486502
href="#"

src/features/product/productAPI.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function fetchAllProducts() {
66
resolve({ data });
77
});
88
}
9-
export function fetchProductsByFilters(filter, sort) {
9+
export function fetchProductsByFilters(filter, sort, pagination) {
1010
// filter ={"brand":"Essence"}
1111
// TODO:we will on server support mutilple value
1212

@@ -28,6 +28,7 @@ export function fetchProductsByFilters(filter, sort) {
2828
// in server side:-
2929
// filter ={"category":["frangrances","furniture"]}
3030
// sort={_sort:"price", order="desc"}
31+
// sort={_page:3, _per_page=6}
3132
let queryString = "";
3233
for (let key in filter) {
3334
const categoryValues = filter[key]; // "[furniture]" << array
@@ -39,6 +40,10 @@ export function fetchProductsByFilters(filter, sort) {
3940
for (let key in sort) {
4041
queryString += `${key}=${sort[key]}&`; //_sort:"price"
4142
}
43+
console.log(pagination);
44+
for (let key in pagination) {
45+
queryString += `${pagination}=${pagination[key]}&`; //_page:3
46+
}
4247
return new Promise(async (resolve) => {
4348
// TODO: we will not hard coded server url here...
4449
const response = await fetch(

src/features/product/productSlice.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export const fetchAllProductsAsync = createAsyncThunk(
1616
);
1717
export const fetchProductsByFiltersAsync = createAsyncThunk(
1818
"product/fetchProductsByFilters",
19-
async ({ filter, sort }) => {
20-
const response = await fetchProductsByFilters(filter, sort);
19+
async ({ filter, sort, pagination }) => {
20+
const response = await fetchProductsByFilters(filter, sort, pagination);
21+
2122
// The value we return becomes the `fulfilled` action payload
2223
return response.data;
2324
}

0 commit comments

Comments
 (0)