refactor: enhance blog functionality with related blogs fetching, improved loading states, and tag filtering

This commit is contained in:
hamed
2025-11-19 11:32:09 +03:30
parent 235a64c378
commit e26d342c90
10 changed files with 251 additions and 130 deletions
+9 -2
View File
@@ -6,11 +6,18 @@ async function Blog({ params: { slug } }) {
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const blog = await fetchReq(`${API_URL}/api/v1/blog/${slug}`);
const blogs = await fetchReq(`${API_URL}/api/v1/blogs`);
// Get related blogs based on first tag
let relatedBlogs = [];
if (blog?.tag?.[0]?.id) {
const tagId = blog.tag[0].id;
const relatedResponse = await fetchReq(`${API_URL}/api/v1/blogs?page=1&limit=12&tag=${tagId}`);
relatedBlogs = relatedResponse?.blogs || [];
}
return (
<Layout>
<BlogPage blog={blog} blogs={blogs.blogs} />
<BlogPage blog={blog} blogs={relatedBlogs} />
</Layout>
);
}