Skip to content

Commit

Permalink
fix(pagination): get posts and slice after
Browse files Browse the repository at this point in the history
- Move pages to the right
  • Loading branch information
LuanRoger committed Apr 14, 2024
1 parent a591a52 commit c5875e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
32 changes: 16 additions & 16 deletions src/components/PostsListsPaginated.astro
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
---
import { getPageCount } from '@/scripts/content_utils';
import Pagination from './Pagination.astro';
import PostsList from './PostsList.astro';
import { getPostsFromPage } from '@/scripts/content_fetcher';
import { getPageCount } from "@/scripts/content_utils";
import Pagination from "./Pagination.astro";
import PostsList from "./PostsList.astro";
import { getPostsFromPage } from "@/scripts/content_fetcher";
interface Props {
currentPage: number;
}
const { currentPage } = Astro.props;
const basePath = '/posts/';
const basePath = "/posts/";
const pageCount = await getPageCount();
const pagePosts = await getPostsFromPage(currentPage);
---

<div class="flex flex-col justify-center gap-5">
<PostsList posts={pagePosts} />
{
pageCount !== 1 && (
<Pagination
pageCount={pageCount}
currentPage={currentPage}
basePath={basePath}
/>
)
}
</div>
<PostsList posts={pagePosts} />
{
pageCount !== 1 && (
<Pagination
pageCount={pageCount}
currentPage={currentPage}
basePath={basePath}
/>
)
}
</div>
2 changes: 1 addition & 1 deletion src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => (
<nav
role="navigation"
aria-label="pagination"
className={cn("mx-auto flex w-full justify-center", className)}
className={cn("mx-auto flex w-full justify-end", className)}
{...props}
/>
)
Expand Down
20 changes: 4 additions & 16 deletions src/scripts/content_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,10 @@ export async function getPostsFromPage(
page: number
): Promise<CollectionEntry<"posts">[]> {
const skip = (page - 1) * POSTS_PER_PAGE;
const firstPage = skip === 0
let postsSkiped = 0
let postsTaked = 0
let postsSkiped = 0;
let postsTaked = 0;

const pagePosts = await getPosts(({ data }) => {
if(data.draft || (!firstPage && postsSkiped <= skip)) {
postsSkiped++
return false
}
const pagePosts = await getPosts();

if(postsTaked < POSTS_PER_PAGE) {
return true
}

return false
});

return pagePosts.slice(0, POSTS_PER_PAGE);
return pagePosts.slice(skip, POSTS_PER_PAGE + skip);
}

0 comments on commit c5875e3

Please sign in to comment.