From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 24 Nov 2023 20:00:08 +0100
Subject: refactor(services, types): reorganize GraphQL fetchers and data types

The Typescript mapped types was useful for autocompletion in fetchers
but their are harder to maintain. I think it's better to keep each
query close to its fetcher to have a better understanding of the
fetched data. So I:
* colocate queries with their own fetcher
* colocate mutations with their own mutator
* remove Typescript mapped types for queries and mutations
* move data convertors inside graphql services
* rename most of data types and fetchers
---
 src/utils/helpers/pages.tsx | 83 ++++++---------------------------------------
 1 file changed, 10 insertions(+), 73 deletions(-)

(limited to 'src/utils/helpers/pages.tsx')

diff --git a/src/utils/helpers/pages.tsx b/src/utils/helpers/pages.tsx
index 7b6bdca..9e015db 100644
--- a/src/utils/helpers/pages.tsx
+++ b/src/utils/helpers/pages.tsx
@@ -1,43 +1,7 @@
 import NextImage from 'next/image';
 import type { LinksWidgetItemData, PostData } from '../../components';
-import { getArticleFromRawData } from '../../services/graphql';
-import type {
-  Article,
-  EdgesResponse,
-  PageLink,
-  RawArticle,
-  RawThematicPreview,
-  RawTopicPreview,
-} from '../../types';
+import type { ArticlePreview, PageLink } from '../../types';
 import { ROUTES } from '../constants';
-import { getImageFromRawData } from './images';
-
-/**
- * Convert raw data to a Link object.
- *
- * @param data - An object.
- * @param {number} data.databaseId - The data id.
- * @param {number} [data.logo] - The data logo.
- * @param {string} data.slug - The data slug.
- * @param {string} data.title - The data name.
- * @returns {PageLink} The link data (id, slug and title).
- */
-export const getPageLinkFromRawData = (
-  data: RawThematicPreview | RawTopicPreview,
-  kind: 'thematic' | 'topic'
-): PageLink => {
-  const { databaseId, featuredImage, slug, title } = data;
-  const baseUrl = `${
-    kind === 'thematic' ? ROUTES.THEMATICS.INDEX : ROUTES.TOPICS
-  }/`;
-
-  return {
-    id: databaseId,
-    logo: featuredImage ? getImageFromRawData(featuredImage.node) : undefined,
-    name: title,
-    url: `${baseUrl}${slug}`,
-  };
-};
 
 /**
  * Method to sort PageLink objects by name.
@@ -73,55 +37,28 @@ export const getLinksItemData = (links: PageLink[]): LinksWidgetItemData[] =>
 /**
  * Retrieve the posts list with the article URL.
  *
- * @param {Article[]} posts - An array of articles.
+ * @param {ArticlePreview[]} posts - An array of articles.
  * @returns {PostData[]} An array of posts with full article URL.
  */
-export const getPostsWithUrl = (posts: Article[]): PostData[] =>
-  posts.map(({ intro, meta, slug, title, ...post }) => {
+export const getPostsWithUrl = (posts: ArticlePreview[]): PostData[] =>
+  posts.map(({ id, intro, meta, slug, title, ...post }) => {
     return {
       ...post,
       cover: meta.cover ? <NextImage {...meta.cover} /> : undefined,
       excerpt: intro,
       heading: title,
+      id,
       meta: {
         publicationDate: meta.dates.publication,
         updateDate: meta.dates.update,
         wordsCount: meta.wordsCount,
-        author: meta.author?.name,
         thematics: meta.thematics,
-        topics: meta.topics,
-        comments:
-          meta.commentsCount === undefined
-            ? undefined
-            : {
-                count: meta.commentsCount,
-                postHeading: title,
-                url: `${ROUTES.ARTICLE}/${slug}#comments`,
-              },
+        comments: {
+          count: meta.commentsCount ?? 0,
+          postHeading: title,
+          url: `${ROUTES.ARTICLE}/${slug}#comments`,
+        },
       },
       url: `${ROUTES.ARTICLE}/${slug}`,
     };
   });
-
-/**
- * Retrieve the posts list from raw data.
- *
- * @param {EdgesResponse<RawArticle>[]} rawData - The raw data.
- * @returns {PostData[]} An array of posts.
- */
-export const getPostsList = async (
-  rawData: EdgesResponse<RawArticle>[]
-): Promise<PostData[]> => {
-  const articlesList: RawArticle[] = [];
-  rawData.forEach((articleData) => {
-    articleData.edges.forEach((edge) => {
-      articlesList.push(edge.node);
-    });
-  });
-
-  return getPostsWithUrl(
-    await Promise.all(
-      articlesList.map(async (article) => getArticleFromRawData(article))
-    )
-  );
-};
-- 
cgit v1.2.3

