@@ -16,6 +16,7 @@ import {
1616 PlusIcon ,
1717 Squares2X2Icon ,
1818} from "@heroicons/react/20/solid" ;
19+ import { ITEMS_PER_PAGE } from "../../../app/constants" ;
1920
2021const 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 = "#"
0 commit comments