Skip to content

Commit

Permalink
Refractor get posts function
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinkipruto committed Sep 27, 2023
1 parent 516a9d6 commit 10df456
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function ArticlePage({
coverImage: { src: featureImage },
content,
publishedOn,
page,
path: page,
}) {
return (
<Box component="article">
Expand Down
62 changes: 7 additions & 55 deletions apps/codeforafrica/src/lib/data/pagify/stories.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
import { formatTags } from "@/codeforafrica/lib/data/utils/posts";
import formatDate from "@/codeforafrica/utils/formatDate";
import { getPost } from "@/codeforafrica/lib/data/utils/posts";

async function stories(api, context) {
const { params, locale } = context;
const { params } = context;
const page = params.slugs[1];
const slug = params.slugs[2];
const { docs } = await api.getCollection("posts", {
locale,
where: {
slug: {
equals: slug,
},
},
});
if (!docs?.length) {
return null;
}
const [story] = docs;
const {
authors,
title,
coverImage,
excerpt,
tags,
meta,
publishedOn,
...other
} = story;
const articleMeta = {
title,
description: excerpt,
image: coverImage,
...meta,
};
const post = await getPost(api, slug, page);
if (!post) return null;
return {
title,
blocks: [
{
authors: authors.map(({ fullName }) => {
return {
name: fullName,
bio: "",
};
}),
title,
coverImage,
excerpt,
tags: formatTags(tags),
meta: articleMeta,
publishedOn: formatDate(publishedOn, {
includeTime: false,
month: "short",
}),
page,
blockType: "article",
...other,
},
],
meta: articleMeta,
...post,
// TODO: get recent posts
recent: [],
};
}

Expand Down
84 changes: 70 additions & 14 deletions apps/codeforafrica/src/lib/data/utils/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ export function formatPost(post, path) {
};
}

export function formatTags(tags) {
const excludedTags = new Set(["stories", "opportunities"]);

const tagCounts = tags.reduce((counts, { name }) => {
// eslint-disable-next-line no-param-reassign
counts[name] = (counts[name] || 0) + 1;
return counts;
}, {});

const sortedTags = Object.keys(tagCounts)
.filter((tag) => !excludedTags.has(tag.toLowerCase()))
.sort((a, b) => tagCounts[b] - tagCounts[a])
.map((tag) => tags.find(({ name }) => name === tag));

return sortedTags;
}

export async function getPosts(api, params, path) {
const { page: queryPage = 1, tag, q, where = {}, ...other } = params;
const options = {
Expand Down Expand Up @@ -85,19 +102,58 @@ export async function getPosts(api, params, path) {
};
}

export function formatTags(tags) {
const excludedTags = new Set(["stories", "opportunities"]);

const tagCounts = tags.reduce((counts, { name }) => {
// eslint-disable-next-line no-param-reassign
counts[name] = (counts[name] || 0) + 1;
return counts;
}, {});

const sortedTags = Object.keys(tagCounts)
.filter((tag) => !excludedTags.has(tag.toLowerCase()))
.sort((a, b) => tagCounts[b] - tagCounts[a])
.map((tag) => tags.find(({ name }) => name === tag));
export async function getPost(api, slug, path) {
const { docs } = await api.getCollection("posts", {
where: {
slug: {
equals: slug,
},
},
});
if (!docs?.length) {
return null;
}
const [post] = docs;
const {
authors,
title,
coverImage,
excerpt,
tags,
meta,
publishedOn,
...other
} = post;

return sortedTags;
const postMeta = {
title,
description: excerpt,
image: coverImage,
...meta,
};
return {
title,
blocks: [
{
authors: authors.map(({ fullName }) => {
return {
name: fullName,
bio: "",
};
}),
title,
coverImage,
excerpt,
tags: formatTags(tags),
publishedOn: formatDate(publishedOn, {
includeTime: false,
month: "short",
}),
path,
blockType: "article",
...other,
},
],
meta: postMeta,
};
}

0 comments on commit 10df456

Please sign in to comment.