|
| 1 | +import { Col, Pagination, Row, Spinner } from "react-bootstrap"; |
| 2 | +import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 3 | +import { setUserProducts } from "../sidebar/actions/productSlice"; |
| 4 | +import { productsPerPage } from "../../constants/rasterParameterConstants"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | + |
| 8 | +const DataPagination = (props: {totalNumberOfProducts: number, totalNumberOfFilteredProducts: number, }) => { |
| 9 | + const {totalNumberOfProducts, totalNumberOfFilteredProducts} = props |
| 10 | + const dispatch = useAppDispatch() |
| 11 | + const userProducts = useAppSelector((state) => state.product.userProducts) |
| 12 | + const allUserProducts = useAppSelector((state) => state.product.allUserProducts) |
| 13 | + const [noNextPage, setNoNextPage] = useState<boolean>(false) |
| 14 | + const [noPreviousPage, setNoPreviousPage] = useState<boolean>(true) |
| 15 | + const [waitingForPagination, setWaitingForPagination] = useState<boolean>(false) |
| 16 | + const [currentPageNumber, setCurrentPageNumber] = useState<number>(1) |
| 17 | + const numberOfTotalPages = Math.ceil(allUserProducts.length / parseInt(productsPerPage)) |
| 18 | + |
| 19 | + const handleSelectPage = async (pageNumber: number) => { |
| 20 | + const firstNumber = (pageNumber-1) * parseInt(productsPerPage) |
| 21 | + const lastNumber = firstNumber + parseInt(productsPerPage) |
| 22 | + dispatch(setUserProducts(allUserProducts.slice(firstNumber, lastNumber))) |
| 23 | + setCurrentPageNumber(pageNumber) |
| 24 | + if(pageNumber === 1) { |
| 25 | + setNoPreviousPage(true) |
| 26 | + setNoNextPage(false) |
| 27 | + } else if (pageNumber === numberOfTotalPages) { |
| 28 | + setNoPreviousPage(false) |
| 29 | + setNoNextPage(true) |
| 30 | + } else { |
| 31 | + setNoPreviousPage(false) |
| 32 | + setNoNextPage(false) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const waitingForPaginationSpinner = () => { |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <Spinner animation="border" role="status"> |
| 40 | + <span className="visually-hidden">Loading...</span> |
| 41 | + </Spinner> |
| 42 | + </div> |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + const getPaginationItemsWithEllipsis = () => { |
| 47 | + let numberOfSlotsFreeLeft = 0 |
| 48 | + if(currentPageNumber >= numberOfTotalPages-4) { |
| 49 | + numberOfSlotsFreeLeft = 4 - (numberOfTotalPages - currentPageNumber) |
| 50 | + } |
| 51 | + |
| 52 | + let numberOfSlotsFreeRight = 0 |
| 53 | + if(currentPageNumber <= 4) { |
| 54 | + numberOfSlotsFreeRight = 5 - currentPageNumber |
| 55 | + } |
| 56 | + const pagesAllowed = [currentPageNumber-2, currentPageNumber-1, currentPageNumber, currentPageNumber+1, currentPageNumber+2] |
| 57 | + const pagesAllowedToLeftOfCurrent: number[] = [] |
| 58 | + for(let index=0; index<numberOfTotalPages; index++) { |
| 59 | + if(numberOfSlotsFreeLeft !== 0 && index+1 <= numberOfTotalPages - 3) { |
| 60 | + // let another number go on right |
| 61 | + pagesAllowedToLeftOfCurrent.push(currentPageNumber - index - 3) |
| 62 | + numberOfSlotsFreeLeft -= 1 |
| 63 | + } |
| 64 | + if(numberOfSlotsFreeRight !== 0 && index+1 > currentPageNumber + 3) { |
| 65 | + // let another number go on left |
| 66 | + pagesAllowed.push(index) |
| 67 | + numberOfSlotsFreeRight -= 1 |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + pagesAllowed.unshift(...pagesAllowedToLeftOfCurrent.reverse()) |
| 72 | + const pagesAllowedFiltered = pagesAllowed.filter(value => value > 1) |
| 73 | + const pagesToShow = [] |
| 74 | + for(let pageIndex = 1; pageIndex < numberOfTotalPages-1; pageIndex++) { |
| 75 | + const pageNumberOfIndex = pageIndex + 1 |
| 76 | + if(pagesAllowedFiltered.includes(pageNumberOfIndex)) { |
| 77 | + pagesToShow.push( <Pagination.Item key={`${pageNumberOfIndex}-pagination-item`} active={currentPageNumber === pageNumberOfIndex} onClick={() => handleSelectPage(pageNumberOfIndex)}>{pageNumberOfIndex}</Pagination.Item>) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if(pagesAllowed[0] > 2) pagesToShow.unshift(<Pagination.Ellipsis />) |
| 82 | + if(pagesAllowed[pagesAllowed.length-1] < numberOfTotalPages-1) pagesToShow.push(<Pagination.Ellipsis />) |
| 83 | + return pagesToShow |
| 84 | + } |
| 85 | + |
| 86 | + return waitingForPagination ? waitingForPaginationSpinner() : ( |
| 87 | + <Row> |
| 88 | + <Col xs={2}></Col> |
| 89 | + <Col xs={7}> |
| 90 | + { |
| 91 | + totalNumberOfFilteredProducts !== 0 ? |
| 92 | + <Pagination data-bs-theme='dark' style={{paddingTop: '15px'}} className="center"> |
| 93 | + <Pagination.Prev onClick={() => handleSelectPage(currentPageNumber-1)} disabled={noPreviousPage} /> |
| 94 | + <Pagination.Item active={currentPageNumber === 1} onClick={() => handleSelectPage(1)}>1</Pagination.Item> |
| 95 | + {getPaginationItemsWithEllipsis()} |
| 96 | + {numberOfTotalPages > 1 ? <Pagination.Item active={currentPageNumber === numberOfTotalPages} onClick={() => handleSelectPage(numberOfTotalPages)}>{numberOfTotalPages}</Pagination.Item> : null} |
| 97 | + <Pagination.Next onClick={() => handleSelectPage(currentPageNumber+1)} disabled={userProducts.length < parseInt(productsPerPage) || noNextPage} /> |
| 98 | + </Pagination> |
| 99 | + : null |
| 100 | + } |
| 101 | + </Col> |
| 102 | + <Col xs={3} style={{paddingTop: '15px'}}><h6><b>{totalNumberOfProducts}</b> Total Generated Products</h6></Col> |
| 103 | + </Row> |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +export default DataPagination; |
| 108 | + |
0 commit comments