summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-23 14:07:02 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-23 16:00:13 +0200
commit34e216546151eaf8a0a3cbb0bc8b65dae4c63bf2 (patch)
treebff34f8a1dc65f0559ddf851433f242edb092824 /src/utils
parent0f8f963ba3eccd7fd94785bf7fb216b6287cec57 (diff)
refactor: reduce the number of data transformation
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/helpers/pages.ts49
1 files changed, 32 insertions, 17 deletions
diff --git a/src/utils/helpers/pages.ts b/src/utils/helpers/pages.ts
index 62337db..1a388e1 100644
--- a/src/utils/helpers/pages.ts
+++ b/src/utils/helpers/pages.ts
@@ -1,7 +1,10 @@
import { type Post } from '@components/organisms/layout/posts-list';
import { type LinksListItems } from '@components/organisms/widgets/links-list-widget';
-import { type Meta, type PageLink } from '@ts/types/app';
+import { type EdgesResponse } from '@services/graphql/api';
+import { getArticleFromRawData } from '@services/graphql/articles';
+import { type Article, type PageLink } from '@ts/types/app';
import {
+ type RawArticle,
type RawThematicPreview,
type RawTopicPreview,
} from '@ts/types/raw-data';
@@ -52,23 +55,35 @@ export const getLinksListItems = (
};
/**
- * Retrieve the formatted meta.
+ * Retrieve the posts list with the article URL.
*
- * @param {Meta<'article'>} meta - The article meta.
- * @returns {Post['meta']} The formatted meta.
+ * @param {Article[]} posts - An array of articles.
+ * @returns {Post[]} An array of posts with full article URL.
*/
-export const getPostMeta = (meta: Meta<'article'>): Post['meta'] => {
- const { commentsCount, dates, thematics, topics, wordsCount } = meta;
+export const getPostsWithUrl = (posts: Article[]): Post[] => {
+ return posts.map((post) => {
+ return {
+ ...post,
+ url: `/article/${post.slug}`,
+ };
+ });
+};
- return {
- commentsCount,
- dates,
- readingTime: { wordsCount: wordsCount || 0, onlyMinutes: true },
- thematics: thematics?.map((thematic) => {
- return { ...thematic, url: `/thematique/${thematic.slug}` };
- }),
- topics: topics?.map((topic) => {
- return { ...topic, url: `/sujet/${topic.slug}` };
- }),
- };
+/**
+ * Retrieve the posts list from raw data.
+ *
+ * @param {EdgesResponse<RawArticle>[]} rawData - The raw data.
+ * @returns {Post[]} An array of posts.
+ */
+export const getPostsList = (rawData: EdgesResponse<RawArticle>[]): Post[] => {
+ const articlesList: RawArticle[] = [];
+ rawData.forEach((articleData) =>
+ articleData.edges.forEach((edge) => {
+ articlesList.push(edge.node);
+ })
+ );
+
+ return getPostsWithUrl(
+ articlesList.map((article) => getArticleFromRawData(article))
+ );
};