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 --- .../helpers/convert-wp-thematic-to-thematic.ts | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts (limited to 'src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts') diff --git a/src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts b/src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts new file mode 100644 index 0000000..cabfa18 --- /dev/null +++ b/src/services/graphql/helpers/convert-wp-thematic-to-thematic.ts @@ -0,0 +1,60 @@ +import type { + PageLink, + Thematic, + WPPostPreview, + WPThematic, +} from '../../../types'; +import { ROUTES } from '../../../utils/constants'; +import { + getUniquePageLinks, + sortPageLinksByName, +} from '../../../utils/helpers'; +import { convertPostPreviewToArticlePreview } from './convert-post-preview-to-article-preview'; +import { convertWPTopicPreviewToPageLink } from './convert-taxonomy-to-page-link'; +import { convertWPImgToImg } from './convert-wp-image-to-img'; + +const getRelatedTopicsFrom = (posts: WPPostPreview[]): PageLink[] => { + const topics: PageLink[] = []; + + for (const post of posts) { + if ( + post.acfPosts && + 'postsInTopic' in post.acfPosts && + post.acfPosts.postsInTopic + ) { + topics.push( + ...post.acfPosts.postsInTopic.map(convertWPTopicPreviewToPageLink) + ); + } + } + + return getUniquePageLinks(topics).sort(sortPageLinksByName); +}; + +export const convertWPThematicToThematic = (thematic: WPThematic): Thematic => { + return { + content: thematic.contentParts.afterMore, + intro: thematic.contentParts.beforeMore, + meta: { + articles: thematic.acfThematics?.postsInThematic?.map( + convertPostPreviewToArticlePreview + ), + cover: thematic.featuredImage + ? convertWPImgToImg(thematic.featuredImage.node) + : undefined, + dates: { + publication: thematic.date, + update: thematic.modified, + }, + seo: { + description: thematic.seo.metaDesc, + title: thematic.seo.title, + }, + relatedTopics: thematic.acfThematics?.postsInThematic + ? getRelatedTopicsFrom(thematic.acfThematics.postsInThematic) + : undefined, + }, + slug: `${ROUTES.THEMATICS.INDEX}/${thematic.slug}`, + title: thematic.title, + }; +}; -- cgit v1.2.3