From 821f183999320d69ea9acc0a6812aea2f0fab1e4 Mon Sep 17 00:00:00 2001 From: Saimon Kataev Date: Fri, 7 Feb 2025 14:47:18 +0100 Subject: [PATCH] fix: remove featured guides and changelog posts from blog --- src/utils/api-posts.js | 49 +++++++++++------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/src/utils/api-posts.js b/src/utils/api-posts.js index dab5f729a7..8cbb32ad25 100644 --- a/src/utils/api-posts.js +++ b/src/utils/api-posts.js @@ -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) => {