diff --git a/src/App.tsx b/src/App.tsx index a02a9fb..e510dbb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react' import './App.css' import q from 'qjuul' import { PaginationButton } from './components' +import Pagination from './components/Pagination' function App() { const API_MOVIE_KEY = 'd92949d8' @@ -30,15 +31,12 @@ function App() { All movies - Pages - {calculatePages(totalPages)} - - {Array.from(Array(calculatePages(totalPages)).keys()).map( - (page) => ( - - ) - )} - + {!loading && totalPages && ( + + )} {!loading && movies ? ( diff --git a/src/components/Pagination/index.tsx b/src/components/Pagination/index.tsx new file mode 100644 index 0000000..53725d6 --- /dev/null +++ b/src/components/Pagination/index.tsx @@ -0,0 +1,70 @@ +import q from 'qjuul' +import PaginationButton from '../PaginationButton' +import { useEffect, useState } from 'react' + +interface PaginationProps { + currentPage: number + totalPages: number +} + +const Pagination: React.FC = ({ currentPage, totalPages }) => { + const [showLeftDots, setShowLeftDots] = useState(false) + const [showRightDots, setShowRightDots] = useState(false) + const [showingNumbers, setShowingNumbers] = useState([0]) + + useEffect(() => { + if (totalPages > 5) { + setShowLeftDots(currentPage > 4 ? true : false) + setShowRightDots(currentPage < totalPages - 4 ? true : false) + } + if (!showLeftDots && !showRightDots) { + setShowingNumbers(Array.from(Array(totalPages).keys())) + } + if (!showLeftDots && showRightDots) { + setShowingNumbers([1, 2, 3, 4, 5]) + } + + if (showLeftDots && !showRightDots) { + setShowingNumbers([ + totalPages - 4, + totalPages - 3, + totalPages - 2, + totalPages - 1, + totalPages, + ]) + } + + if (showLeftDots && showRightDots) { + setShowingNumbers([currentPage - 1, currentPage, currentPage + 1]) + } + }, []) + + return ( + + {totalPages > 0 && ( + + {showLeftDots && ( + + + ... + + )} + + {showingNumbers.map((page) => ( + + + + ))} + {showRightDots && ( + + ... + {' '} + + )} + + )} + + ) +} + +export default Pagination diff --git a/src/components/Pagination/styles.css b/src/components/Pagination/styles.css new file mode 100644 index 0000000..e69de29