refactor: replace pagination logic with Astro built-in pagination (#376)

This commit is contained in:
Timon Jurschitsch
2024-09-16 08:05:50 +02:00
committed by GitHub
parent 70b2486293
commit 08b1676cdd
12 changed files with 102 additions and 195 deletions
-14
View File
@@ -1,14 +0,0 @@
import { SITE } from "@config";
const getPageNumbers = (numberOfPosts: number) => {
const numberOfPages = numberOfPosts / Number(SITE.postPerPage);
let pageNumbers: number[] = [];
for (let i = 1; i <= Math.ceil(numberOfPages); i++) {
pageNumbers = [...pageNumbers, i];
}
return pageNumbers;
};
export default getPageNumbers;
-35
View File
@@ -1,35 +0,0 @@
import { SITE } from "@config";
import getPageNumbers from "./getPageNumbers";
interface GetPaginationProps<T> {
posts: T;
page: string | number;
isIndex?: boolean;
}
const getPagination = <T>({
posts,
page,
isIndex = false,
}: GetPaginationProps<T[]>) => {
const totalPagesArray = getPageNumbers(posts.length);
const totalPages = totalPagesArray.length;
const currentPage = isIndex
? 1
: page && !isNaN(Number(page)) && totalPagesArray.includes(Number(page))
? Number(page)
: 0;
const lastPost = isIndex ? SITE.postPerPage : currentPage * SITE.postPerPage;
const startPost = isIndex ? 0 : lastPost - SITE.postPerPage;
const paginatedPosts = posts.slice(startPost, lastPost);
return {
totalPages,
currentPage,
paginatedPosts,
};
};
export default getPagination;