From dfa894b76ee3584bf169710c78c57330c5d6ee67 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 29 Nov 2023 12:28:03 +0100 Subject: fix(pages,services): make thematics & topics pages usable again When I refactored the fetchers and convertors in #f111685 I forgot to convert WPThematicPreview and WPTopicPreview so the thematics and topics pages was broken. I also: * removed the ToC added by error in #70b4f63 * fix heading styles * fix website url in topics pages --- .../graphql/helpers/convert-wp-topic-to-topic.ts | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/services/graphql/helpers/convert-wp-topic-to-topic.ts (limited to 'src/services/graphql/helpers/convert-wp-topic-to-topic.ts') diff --git a/src/services/graphql/helpers/convert-wp-topic-to-topic.ts b/src/services/graphql/helpers/convert-wp-topic-to-topic.ts new file mode 100644 index 0000000..b0136c7 --- /dev/null +++ b/src/services/graphql/helpers/convert-wp-topic-to-topic.ts @@ -0,0 +1,56 @@ +import type { PageLink, Topic, WPPostPreview, WPTopic } from '../../../types'; +import { ROUTES } from '../../../utils/constants'; +import { + getUniquePageLinks, + sortPageLinksByName, +} from '../../../utils/helpers'; +import { convertPostPreviewToArticlePreview } from './convert-post-preview-to-article-preview'; +import { convertWPThematicPreviewToPageLink } from './convert-taxonomy-to-page-link'; +import { convertWPImgToImg } from './convert-wp-image-to-img'; + +const getRelatedThematicsFrom = (posts: WPPostPreview[]): PageLink[] => { + const thematics: PageLink[] = []; + + for (const post of posts) { + if ( + post.acfPosts && + 'postsInThematic' in post.acfPosts && + post.acfPosts.postsInThematic + ) { + thematics.push( + ...post.acfPosts.postsInThematic.map(convertWPThematicPreviewToPageLink) + ); + } + } + + return getUniquePageLinks(thematics).sort(sortPageLinksByName); +}; + +export const convertWPTopicToTopic = (topic: WPTopic): Topic => { + return { + content: topic.contentParts.afterMore, + intro: topic.contentParts.beforeMore, + meta: { + articles: topic.acfTopics?.postsInTopic?.map( + convertPostPreviewToArticlePreview + ), + cover: topic.featuredImage + ? convertWPImgToImg(topic.featuredImage.node) + : undefined, + dates: { + publication: topic.date, + update: topic.modified, + }, + seo: { + description: topic.seo.metaDesc, + title: topic.seo.title, + }, + relatedThematics: topic.acfTopics?.postsInTopic + ? getRelatedThematicsFrom(topic.acfTopics.postsInTopic) + : undefined, + website: topic.acfTopics?.officialWebsite ?? undefined, + }, + slug: `${ROUTES.TOPICS}/${topic.slug}`, + title: topic.title, + }; +}; -- cgit v1.2.3