From 73a7b1c1ee69ea556fc0562abfecb517e7845e25 Mon Sep 17 00:00:00 2001 From: enes Date: Tue, 5 Nov 2024 14:11:11 +0100 Subject: [PATCH] feat: admin blog page pagination --- src/pages/blogs/index.tsx | 77 +++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/src/pages/blogs/index.tsx b/src/pages/blogs/index.tsx index 1d8e96d..29c8506 100644 --- a/src/pages/blogs/index.tsx +++ b/src/pages/blogs/index.tsx @@ -8,6 +8,8 @@ import '../../styles/filters.css' import '../../styles/pagination.css' import '../../styles/search.css' import '../../styles/styles.css' +import { PaginationWithPageNumbers } from 'components/Pagination' +import { scrollIntoView } from 'utils' export const BlogsPage = () => { const blogs = useLoaderData() as Partial[] | undefined @@ -80,10 +82,31 @@ export const BlogsPage = () => { return filtered }, [blogs, searchTerm, filterOptions.sort, filterOptions.nsfw]) + // Pagination logic + const [currentPage, setCurrentPage] = useState(1) + const scrollTargetRef = useRef(null) + + const MAX_BLOGS_PER_PAGE = 16 + const totalBlogs = filteredBlogs.length + const totalPages = Math.ceil(totalBlogs / MAX_BLOGS_PER_PAGE) + const startIndex = (currentPage - 1) * MAX_BLOGS_PER_PAGE + const endIndex = startIndex + MAX_BLOGS_PER_PAGE + const currentMods = filteredBlogs.slice(startIndex, endIndex) + + const handlePageChange = (page: number) => { + if (page >= 1 && page <= totalPages) { + scrollIntoView(scrollTargetRef.current) + setCurrentPage(page) + } + } + return (
-
+
@@ -156,48 +179,22 @@ export const BlogsPage = () => {
+
-
-
- {filteredBlogs && - filteredBlogs.map((b) => )} -
-
- -
- +
+
+ {currentMods && + currentMods.map((b) => )}
+ + {totalPages > 1 && ( + + )}