Skip to content

Commit 434e484

Browse files
committed
Refactor category filtering logic and sort categories alphabetically
1 parent 12399f1 commit 434e484

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/app/categories/[slug]/page.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,27 @@ export async function generateMetadata({ params }) {
3333

3434

3535
const CategoryPage = ({ params }) => {
36-
const allCategories = ["all"];
37-
const blogs = allBlogs.filter((blog) => {
38-
return blog.tags.some((tag) => {
39-
const slugified = slug(tag);
40-
if (!allCategories.includes(slugified)) {
41-
allCategories.push(slugified);
42-
}
43-
if (params.slug === "all") {
44-
return true;
45-
}
46-
return slugified === params.slug;
47-
});
36+
// Separating logic to create list of categories from all blogs
37+
const allCategories = ["all"]; // Initialize with 'all' category
38+
allBlogs.forEach(blog => {
39+
blog.tags.forEach(tag => {
40+
const slugified = slug(tag);
41+
if (!allCategories.includes(slugified)) {
42+
allCategories.push(slugified);
43+
}
4844
});
45+
});
46+
47+
// Sort allCategories to ensure they are in alphabetical order
48+
allCategories.sort();
49+
50+
// Step 2: Filter blogs based on the current category (params.slug)
51+
const blogs = allBlogs.filter(blog => {
52+
if (params.slug === "all") {
53+
return true; // Include all blogs if 'all' category is selected
54+
}
55+
return blog.tags.some(tag => slug(tag) === params.slug);
56+
});
4957

5058
return (
5159
<article className="mt-12 flex flex-col text-dark dark:text-light">

0 commit comments

Comments
 (0)