Skip to content

Commit 3316e43

Browse files
unable to render totalItems in UI pagination (have to fix)
1 parent ac3fa71 commit 3316e43

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/features/product/components/ProductList.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
fetchAllProductsAsync,
55
fetchProductsByFiltersAsync,
66
selectAllProducts,
7+
selectTotalItems,
78
} from "../productSlice";
89
import { Dialog, Disclosure, Menu, Transition } from "@headlessui/react";
910
import { StarIcon, XMarkIcon } from "@heroicons/react/24/outline";
@@ -64,6 +65,7 @@ function classNames(...classes) {
6465
export default function ProductList() {
6566
const [mobileFiltersOpen, setMobileFiltersOpen] = useState(false);
6667
const products = useSelector(selectAllProducts);
68+
const totalItems = useSelector(selectTotalItems);
6769
const dispatch = useDispatch();
6870
const [filter, setFilter] = useState({});
6971
const [sort, setSort] = useState({});
@@ -208,7 +210,12 @@ export default function ProductList() {
208210

209211
{/* section of product and filters ends */}
210212

211-
<Pagination page={page} setPage={setPage} handlePage={handlePage} />
213+
<Pagination
214+
page={page}
215+
setPage={setPage}
216+
handlePage={handlePage}
217+
totalItems={totalItems}
218+
/>
212219
</main>
213220
</div>
214221
</div>
@@ -437,7 +444,7 @@ function ProductGrid({ products }) {
437444
);
438445
}
439446

440-
function Pagination({ page, setPage, handlePage, totalItems = 30 }) {
447+
function Pagination({ page, setPage, handlePage, totalItems }) {
441448
return (
442449
<div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6">
443450
<div className="flex flex-1 justify-between sm:hidden">
@@ -457,7 +464,7 @@ function Pagination({ page, setPage, handlePage, totalItems = 30 }) {
457464
<div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between">
458465
<div>
459466
<p className="text-sm text-gray-700">
460-
Showing
467+
Showing-
461468
{/* (3-1) * 6 + 1 =13 */}
462469
<span className="font-medium">
463470
{(page - 1) * ITEMS_PER_PAGE + 1}

src/features/product/productAPI.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function fetchProductsByFilters(filter, sort, pagination) {
5050
"http://localhost:8080/products?" + queryString
5151
);
5252
const data = await response.json();
53-
resolve({ data });
53+
const totalItems = await response.data.items;
54+
resolve({ data: { products: data, totalItems: totalItems } });
5455
});
5556
}

src/features/product/productSlice.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fetchAllProducts, fetchProductsByFilters } from "./productAPI";
44
const initialState = {
55
products: [],
66
status: "idle",
7+
totalItems: 0,
78
};
89

910
export const fetchAllProductsAsync = createAsyncThunk(
@@ -46,13 +47,15 @@ export const productSlice = createSlice({
4647
})
4748
.addCase(fetchProductsByFiltersAsync.fulfilled, (state, action) => {
4849
state.status = "idle";
49-
state.products = action.payload;
50+
state.products = action.payload.products;
51+
state.totalItems = action.payload.totalItems;
5052
});
5153
},
5254
});
5355

5456
export const { increment } = productSlice.actions;
5557

5658
export const selectAllProducts = (state) => state.product.products;
59+
export const selectTotalItems = (state) => state.product.totalItems;
5760

5861
export default productSlice.reducer;

0 commit comments

Comments
 (0)