Skip to content

Commit

Permalink
feat(blog): implement pagination for blog posts (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
1chooo committed Sep 12, 2024
1 parent 8e859c4 commit a9550fb
Showing 1 changed file with 64 additions and 37 deletions.
101 changes: 64 additions & 37 deletions apps/web/src/app/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,36 @@ export const metadata = {

let blogTags: string[] = ['All'];

export default function BlogPage({ searchParams }: { searchParams: { tag?: string } }) {
const POSTS_PER_PAGE = 4;

export default function BlogPage({ searchParams }: { searchParams: { tag?: string; page?: string } }) {
let allBlogs = getBlogPosts();
blogTags = ['All', ...Array.from(new Set(allBlogs.map(post => post.metadata.category ?? '')))];
const selectedTag = searchParams.tag || 'All';
const currentPage = parseInt(searchParams.page || '1', 10);

// Filter blogs based on the selected tag
const filteredBlogs = selectedTag === 'All'
? allBlogs
: allBlogs.filter(post => post.metadata.category === selectedTag);

// Sort blogs by date
const sortedBlogs = filteredBlogs.sort((a, b) => {
if (new Date(a.metadata.publishedAt) > new Date(b.metadata.publishedAt)) {
return -1;
}
return 1;
});

// Calculate total pages
const totalPages = Math.ceil(sortedBlogs.length / POSTS_PER_PAGE);

// Get blogs for current page
const paginatedBlogs = sortedBlogs.slice(
(currentPage - 1) * POSTS_PER_PAGE,
currentPage * POSTS_PER_PAGE
);

return (
<article>
<PageHeader title="Hugo's Blog" />
Expand All @@ -30,44 +50,51 @@ export default function BlogPage({ searchParams }: { searchParams: { tag?: strin
<FilterSelectBox selectedTag={selectedTag} blogTags={blogTags} />

<ul className="blog-posts-list">
{filteredBlogs
.sort((a, b) => {
if (new Date(a.metadata.publishedAt) > new Date(b.metadata.publishedAt)) {
return -1;
}
return 1;
})
.map((post, index) => (
<li
key={index}
className="blog-post-item active"
data-category={post.metadata.category}
>
<Link href={`/blog/${post.slug}`} rel="noopener noreferrer">
<figure className="blog-banner-box">
<Image
src={post.metadata.banner}
alt={post.metadata.alt || 'Blog post image'}
loading="lazy"
width={1600}
height={900}
/>
</figure>
<div className="blog-content">
<div className="blog-meta">
<p className="blog-category">{post.metadata.category}</p>
<span className="dot"></span>
<time dateTime={post.metadata.publishedAt}>
{new Date(post.metadata.publishedAt).toLocaleDateString()}
</time>
</div>
<h3 className="h3 blog-item-title">{post.metadata.title}</h3>
<p className="blog-text">{post.metadata.summary}</p>
{paginatedBlogs.map((post, index) => (
<li
key={index}
className="blog-post-item active"
data-category={post.metadata.category}
>
<Link href={`/blog/${post.slug}`} rel="noopener noreferrer">
<figure className="blog-banner-box">
<Image
src={post.metadata.banner}
alt={post.metadata.alt || 'Blog post image'}
loading="lazy"
width={1600}
height={900}
/>
</figure>
<div className="blog-content">
<div className="blog-meta">
<p className="blog-category">{post.metadata.category}</p>
<span className="dot"></span>
<time dateTime={post.metadata.publishedAt}>
{new Date(post.metadata.publishedAt).toLocaleDateString()}
</time>
</div>
</Link>
</li>
))}
<h3 className="h3 blog-item-title">{post.metadata.title}</h3>
<p className="blog-text">{post.metadata.summary}</p>
</div>
</Link>
</li>
))}
</ul>
<div className="pagination">
{Array.from({ length: totalPages }, (_, i) => i + 1).map((pageNum) => (
<Link
key={pageNum}
href={{
pathname: '/blog',
query: { ...searchParams, page: pageNum.toString() },
}}
className={`pagination-btn ${pageNum === currentPage ? 'active' : ''}`}
>
{pageNum}
</Link>
))}
</div>
</section>
</article>
);
Expand Down

0 comments on commit a9550fb

Please sign in to comment.