Skip to content

Commit

Permalink
fix: remove featured guides and changelog posts from blog
Browse files Browse the repository at this point in the history
  • Loading branch information
saimonkat committed Feb 7, 2025
1 parent 667da45 commit 821f183
Showing 1 changed file with 13 additions and 36 deletions.
49 changes: 13 additions & 36 deletions src/utils/api-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,49 +222,26 @@ const getAllPosts = async () => {
getAllGuides(),
getAllChangelogs(),
]);
const allPosts = [...wpPosts, ...guides, ...changelogs];

// Separate featured wp posts, guides and changelogs and all other
const categories = {
wpPosts: [],
guides: [],
changelogs: [],
others: [],
};

// Find 2 most recent featured posts for each category
allPosts.forEach((item) => {
const { pageBlogPost, category, isFeatured } = item;
const isWpPost = !!pageBlogPost;
const isGuide = category === 'guides';
const isChangelog = category === 'changelog';
const featured = isWpPost ? pageBlogPost.isFeatured : isFeatured;

if (featured) {
if (isWpPost && categories.wpPosts.length < 2) {
categories.wpPosts.push(item);
} else if (isGuide && categories.guides.length < 2) {
categories.guides.push(item);
} else if (isChangelog && categories.changelogs.length < 2) {
categories.changelogs.push(item);
// Separate first two featured posts
const [featuredWpPosts, restWpPosts] = wpPosts.reduce(
([featured, rest], post) => {
if (post.pageBlogPost.isFeatured && featured.length < 2) {
featured.push(post);
} else {
categories.others.push(item);
rest.push(post);
}
} else {
categories.others.push(item);
}
});
return [featured, rest];
},
[[], []]
);

// Sort the rest posts by date, newest first
categories.others.sort((a, b) => new Date(b.date) - new Date(a.date));
const restPosts = [...restWpPosts, ...guides, ...changelogs];
restPosts.sort((a, b) => new Date(b.date) - new Date(a.date));

// Combine the results
return [
...categories.wpPosts,
...categories.guides,
...categories.changelogs,
...categories.others,
];
return [...featuredWpPosts, ...restPosts];
};

const getWpPostBySlug = cache(async (slug) => {
Expand Down

0 comments on commit 821f183

Please sign in to comment.