diff --git a/pages/blog/index.tsx b/pages/blog/index.tsx index 37958cec4308..9b7e73f3383c 100644 --- a/pages/blog/index.tsx +++ b/pages/blog/index.tsx @@ -35,6 +35,9 @@ export default function BlogIndexPage() { : [] ); const [isClient, setIsClient] = useState(false); + const [currentPage, setCurrentPage] = useState(1); + const [pageRange, setPageRange] = useState([1, 10]); + const postsPerPage = 12; const onFilter = (data: IBlogPost[]) => setPosts(data); const toFilter = [ @@ -59,6 +62,34 @@ export default function BlogIndexPage() { const description = 'Find the latest and greatest stories from our community'; const image = '/img/social/blog.webp'; + const handlePageChange = (pageNumber: number) => { + setCurrentPage(pageNumber); + if (pageNumber > pageRange[1] - 1) { + setPageRange([pageRange[0] + 3, pageRange[1] + 3]); + } else if (pageNumber < pageRange[0] + 1) { + const newStart = Math.max(pageRange[0] - 3, 1); + const newEnd = newStart + 9; + + setPageRange([newStart, newEnd]); + } + }; + + const handlePrevious = () => { + if (currentPage > 1) { + handlePageChange(currentPage - 1); + } + }; + + const handleNext = () => { + if (currentPage < Math.ceil(posts.length / postsPerPage)) { + handlePageChange(currentPage + 1); + } + }; + + const indexOfLastPost = currentPage * postsPerPage; + const indexOfFirstPost = indexOfLastPost - postsPerPage; + const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost); + useEffect(() => { setIsClient(true); }, []); @@ -122,7 +153,7 @@ export default function BlogIndexPage() { )} {Object.keys(posts).length > 0 && isClient && ( @@ -133,6 +164,42 @@ export default function BlogIndexPage() { )} +
+ + {pageRange[0] > 1 && ...} + {Array.from( + { + length: Math.min(12, Math.ceil(posts.length / postsPerPage) - pageRange[0] + 1) + }, + (_, index) => { + const pageNumber = pageRange[0] + index; + + return ( + + ); + } + )} + {pageRange[1] < Math.ceil(posts.length / postsPerPage) && ...} + +