diff --git a/apps/codeforafrica/src/components/ArticlePage/ArticlePage.js b/apps/codeforafrica/src/components/ArticlePage/ArticlePage.js index c34cccbf5..14a97e01d 100644 --- a/apps/codeforafrica/src/components/ArticlePage/ArticlePage.js +++ b/apps/codeforafrica/src/components/ArticlePage/ArticlePage.js @@ -17,7 +17,7 @@ function ArticlePage({ coverImage: { src: featureImage }, content, publishedOn, - page, + path: page, }) { return ( diff --git a/apps/codeforafrica/src/lib/data/pagify/stories.js b/apps/codeforafrica/src/lib/data/pagify/stories.js index 9e81d4b6d..74a7496b2 100644 --- a/apps/codeforafrica/src/lib/data/pagify/stories.js +++ b/apps/codeforafrica/src/lib/data/pagify/stories.js @@ -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: [], }; } diff --git a/apps/codeforafrica/src/lib/data/utils/posts.js b/apps/codeforafrica/src/lib/data/utils/posts.js index c071076f5..329794669 100644 --- a/apps/codeforafrica/src/lib/data/utils/posts.js +++ b/apps/codeforafrica/src/lib/data/utils/posts.js @@ -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 = { @@ -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, + }; }