Skip to content

Commit

Permalink
Merge branch 'pagination'
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRoger committed Apr 16, 2024
2 parents 8dda660 + c5875e3 commit 62f4650
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 53 deletions.
Binary file added public/images/posts/dummy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions src/components/Pagination.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
import {
Pagination as PaginationShadcn,
PaginationContent,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
} from "./ui/pagination";
interface Props {
currentPage?: number | undefined;
basePath: string;
pageCount: number;
}
const { basePath, currentPage, pageCount } = Astro.props;
---

<PaginationShadcn>
<PaginationContent>
{
currentPage && currentPage > 1 && (
<PaginationItem>
<PaginationPrevious href={`${basePath}${currentPage - 1}`} />
</PaginationItem>
)
}
{
Array.from({ length: pageCount }).map((_, index) => {
const page = index + 1;
return (
<PaginationItem>
<PaginationLink href={`${basePath}${page}`} isActive={currentPage === page}>{page}</PaginationLink>
</PaginationItem>
);
})
}
{
currentPage && currentPage < pageCount && (
<PaginationItem>
<PaginationNext href={`${basePath}${currentPage + 1}`} />
</PaginationItem>
)
}
</PaginationContent>
</PaginationShadcn>
28 changes: 28 additions & 0 deletions src/components/PostsListsPaginated.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
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 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>
10 changes: 4 additions & 6 deletions 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 All @@ -21,7 +21,7 @@ const PaginationContent = React.forwardRef<
>(({ className, ...props }, ref) => (
<ul
ref={ref}
className={cn("flex flex-row items-center gap-1", className)}
className={cn("flex flex-row items-center gap-2", className)}
{...props}
/>
))
Expand Down Expand Up @@ -67,11 +67,10 @@ const PaginationPrevious = ({
<PaginationLink
aria-label="Go to previous page"
size="default"
className={cn("gap-1 pl-2.5", className)}
className={cn("gap-1 p-3", className)}
{...props}
>
<ChevronLeft className="h-4 w-4" />
<span>Previous</span>
</PaginationLink>
)
PaginationPrevious.displayName = "PaginationPrevious"
Expand All @@ -83,10 +82,9 @@ const PaginationNext = ({
<PaginationLink
aria-label="Go to next page"
size="default"
className={cn("gap-1 pr-2.5", className)}
className={cn("gap-1 p-3", className)}
{...props}
>
<span>Next</span>
<ChevronRight className="h-4 w-4" />
</PaginationLink>
)
Expand Down
2 changes: 1 addition & 1 deletion src/content/posts/components_test.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: "components_test"
title: "Components Test"
author: rog
date: 2023-12-06
date: 2024-04-14
draft: false
tags:
- "test"
Expand Down
2 changes: 1 addition & 1 deletion src/content/posts/draft_test.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: "draft_test"
title: "Draft Test"
author: rog
date: 2023-11-26
date: 2024-04-14
draft: true
tags:
- "Test"
Expand Down
20 changes: 20 additions & 0 deletions src/content/posts/dummy1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: "dummy1"
title: "Draft Test 1"
author: rog
date: 2024-04-13
draft: false
tags:
- "dumy"
postType: "Dumy"
description: "1 - This is a dummy post, is just to fill content in the blog 😜."
image: {
"src": "/images/posts/dummy.jpg",
"alt": "Hello World",
"href": "https://www.pexels.com/pt-br/foto/papel-de-parede-digital-de-luz-roxa-e-azul-3780104/"
}
---

# Dumy 1

This is a dummy post, is just to fill content in the blog 😜.
20 changes: 20 additions & 0 deletions src/content/posts/dummy2.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: "dummy2"
title: "Draft Test 2"
author: rog
date: 2024-04-13
draft: false
tags:
- "dumy"
postType: "Dumy"
description: "2 - This is a dummy post, is just to fill content in the blog 😜."
image: {
"src": "/images/posts/dummy.jpg",
"alt": "Hello World",
"href": "https://www.pexels.com/pt-br/foto/papel-de-parede-digital-de-luz-roxa-e-azul-3780104/"
}
---

# Dumy 2

This is a dummy post, is just to fill content in the blog 😜.
20 changes: 20 additions & 0 deletions src/content/posts/dummy3.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: "dummy3"
title: "Draft Test 3"
author: rog
date: 2024-04-13
draft: false
tags:
- "dumy"
postType: "Dumy"
description: "3 - This is a dummy post, is just to fill content in the blog 😜."
image: {
"src": "/images/posts/dummy.jpg",
"alt": "Hello World",
"href": "https://www.pexels.com/pt-br/foto/papel-de-parede-digital-de-luz-roxa-e-azul-3780104/"
}
---

# Dumy 3

This is a dummy post, is just to fill content in the blog 😜.
20 changes: 20 additions & 0 deletions src/content/posts/dummy4.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: "dummy4"
title: "Draft Test 4"
author: rog
date: 2024-04-13
draft: false
tags:
- "dumy"
postType: "Dumy"
description: "4 - This is a dummy post, is just to fill content in the blog 😜."
image: {
"src": "/images/posts/dummy.jpg",
"alt": "Hello World",
"href": "https://www.pexels.com/pt-br/foto/papel-de-parede-digital-de-luz-roxa-e-azul-3780104/"
}
---

# Dumy 4

This is a dummy post, is just to fill content in the blog 😜.
20 changes: 20 additions & 0 deletions src/content/posts/dummy5.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: "dummy5"
title: "Draft Test 5"
author: rog
date: 2024-04-13
draft: false
tags:
- "dumy"
postType: "Dumy"
description: "5 - This is a dummy post, is just to fill content in the blog 😜."
image: {
"src": "/images/posts/dummy.jpg",
"alt": "Hello World",
"href": "https://www.pexels.com/pt-br/foto/papel-de-parede-digital-de-luz-roxa-e-azul-3780104/"
}
---

# Dumy 4

This is a dummy post, is just to fill content in the blog 😜.
2 changes: 1 addition & 1 deletion src/content/posts/gif_post_test.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: "gif_post_test"
title: "GIF Test"
author: rog
date: 2023-11-19
date: 2024-04-14
draft: false
tags:
- "showcase"
Expand Down
2 changes: 1 addition & 1 deletion src/content/posts/hello_world.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: "hello_world"
title: "Hello World"
author: rog
date: 2023-11-12
date: 2024-04-14
draft: false
tags:
- "showcase"
Expand Down
39 changes: 20 additions & 19 deletions src/layouts/LayoutBase.astro
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,25 @@ const { title, bttEnable = false, metaDescription, className } = Astro.props;
<BackToTopButton enable={bttEnable} />
</div>
<Footer />
</body><script>
import { getCurrentThemeMode } from "../scripts/theme";
<script>
import { getCurrentThemeMode } from "../scripts/theme";

document.addEventListener("astro:after-swap", () => {
const currentTheme = getCurrentThemeMode();
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
});
document.addEventListener("astro:page-load", () => {
const currentTheme = getCurrentThemeMode();
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
});
</script>
document.addEventListener("astro:after-swap", () => {
const currentTheme = getCurrentThemeMode();
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
});
document.addEventListener("astro:page-load", () => {
const currentTheme = getCurrentThemeMode();
if (currentTheme === "dark") {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
});
</script>
</body>
</html>
11 changes: 4 additions & 7 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
---
import PostsList from "../components/PostsList.astro";
import LayoutPage from "../layouts/LayoutPage.astro";
import { getPosts } from "../scripts/content_fetcher"
const posts = await getPosts()
import LayoutPage from "@/layouts/LayoutPage.astro";
import PostsListsPaginated from "@/components/PostsListsPaginated.astro";
---

<LayoutPage>
<PostsList posts={posts} />
</LayoutPage>
<PostsListsPaginated currentPage={1} />
</LayoutPage>
30 changes: 30 additions & 0 deletions src/pages/posts/[page].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
import type { GetStaticPaths } from "astro";
import LayoutPage from "@/layouts/LayoutPage.astro";
import { getPageCount } from "@/scripts/content_utils";
import PostsList from "@/components/PostsList.astro";
import { getPostsFromPage } from "@/scripts/content_fetcher";
import PostsListsPaginated from "@/components/PostsListsPaginated.astro";
export const getStaticPaths = (async () => {
const availablePages = await getPageCount()
const paths = Array.from<GetStaticPaths>({ length: availablePages }).map((_, index) => {
const page = index + 1
return {
params: {
page: page.toString()
},
}
})
return paths
}) satisfies GetStaticPaths;
const { page } = Astro.params
const intPage = parseInt(page)
---

<LayoutPage>
<PostsListsPaginated currentPage={intPage} />
</LayoutPage>
1 change: 1 addition & 0 deletions src/scripts/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const POSTS_PER_PAGE = 5;
Loading

0 comments on commit 62f4650

Please sign in to comment.