feat: add pagination to category page

This commit is contained in:
log101 2024-06-21 12:53:17 +03:00
parent 8faa7f472d
commit 393aa52757
6 changed files with 249 additions and 1089 deletions

View File

@ -5,6 +5,9 @@ import tailwind from "@astrojs/tailwind";
// https://astro.build/config // https://astro.build/config
export default defineConfig({ export default defineConfig({
site: "https://blog.log101.dev", site: "https://blog.log101.dev",
redirects: {
"/category/[category]": "/category/[category]/1",
},
integrations: [ integrations: [
tailwind({ tailwind({
applyBaseStyles: false, applyBaseStyles: false,

1304
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
"dependencies": { "dependencies": {
"@astrojs/check": "^0.5.10", "@astrojs/check": "^0.5.10",
"@astrojs/tailwind": "^5.1.0", "@astrojs/tailwind": "^5.1.0",
"astro": "^4.7.0", "astro": "^4.10.0",
"tailwindcss": "^3.4.3", "tailwindcss": "^3.4.3",
"typescript": "^5.4.5" "typescript": "^5.4.5"
}, },

View File

@ -39,7 +39,7 @@ switch (componentType) {
// Options should not mutated further // Options should not mutated further
Object.freeze(options); Object.freeze(options);
// Format datae // Format date
const postDateFormatted = post.data.date.toLocaleDateString("tr-TR", { const postDateFormatted = post.data.date.toLocaleDateString("tr-TR", {
day: "numeric", day: "numeric",
month: "long", month: "long",
@ -47,7 +47,7 @@ const postDateFormatted = post.data.date.toLocaleDateString("tr-TR", {
}); });
// Create post link // Create post link
const postLink = `/category/${post.data.category}/${post.slug}`; const postLink = `/category/${post.data.category}/posts/${post.slug}`;
// Create post content as an astro component // Create post content as an astro component
const { Content } = await post.render(); const { Content } = await post.render();

View File

@ -7,28 +7,29 @@ import Footer from "@/components/Footer.astro";
import Layout from "@/layouts/Layout.astro"; import Layout from "@/layouts/Layout.astro";
import Post from "@/components/Post.astro"; import Post from "@/components/Post.astro";
export function getStaticPaths() { export async function getStaticPaths({ paginate }) {
return CATEGORIES.map((category) => { const blogEntries = await getCollection("blog");
return { const filteredPosts = blogEntries.filter((post) => post.data.category);
return CATEGORIES.flatMap((category) => {
return paginate(filteredPosts, {
params: { category }, params: { category },
}; pageSize: 3,
});
}); });
} }
const { category } = Astro.params; const { page } = Astro.props;
const allTeknikPosts = await getCollection( const posts = page.data as Array<any>;
"blog",
(post) => post.data.category === category
);
--- ---
<Layout title="log101"> <Layout title="log101">
<Header /> <Header />
<div class="posts"> <div class="posts">
{ {
allTeknikPosts.length > 0 ? ( posts.length > 0 ? (
allTeknikPosts posts
.sort((p1, p2) => p2.data.date.getTime() - p1.data.date.getTime()) .sort((p1, p2) => p2.data.date.getTime() - p1.data.date.getTime())
.map((p) => <Post post={p} componentType="long" />) .map((p) => <Post post={p} componentType="long" />)
) : ( ) : (

View File

@ -8,7 +8,6 @@ import Post from "@/components/Post.astro";
import EmojiReactionForm from "@/components/EmojiReactionForm.astro"; import EmojiReactionForm from "@/components/EmojiReactionForm.astro";
import CommentForm from "@/components/CommentForm.astro"; import CommentForm from "@/components/CommentForm.astro";
import HorizontalLine from "@/components/HorizontalLine.astro"; import HorizontalLine from "@/components/HorizontalLine.astro";
import Comment from "@/components/Comment.astro";
const { entry } = Astro.props; const { entry } = Astro.props;
@ -16,6 +15,7 @@ const backendHost = import.meta.env.PUBLIC_BACKEND_HOST;
export async function getStaticPaths() { export async function getStaticPaths() {
const blogEntries = await getCollection("blog"); const blogEntries = await getCollection("blog");
return blogEntries.map((entry) => ({ return blogEntries.map((entry) => ({
params: { category: entry.data.category, slug: entry.slug }, params: { category: entry.data.category, slug: entry.slug },
props: { entry }, props: { entry },