diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-23 14:07:02 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-23 16:00:13 +0200 |
| commit | 34e216546151eaf8a0a3cbb0bc8b65dae4c63bf2 (patch) | |
| tree | bff34f8a1dc65f0559ddf851433f242edb092824 /src/pages/blog | |
| parent | 0f8f963ba3eccd7fd94785bf7fb216b6287cec57 (diff) | |
refactor: reduce the number of data transformation
Diffstat (limited to 'src/pages/blog')
| -rw-r--r-- | src/pages/blog/index.tsx | 75 | ||||
| -rw-r--r-- | src/pages/blog/page/[number].tsx | 66 |
2 files changed, 9 insertions, 132 deletions
diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx index 90f56be..dd04fad 100644 --- a/src/pages/blog/index.tsx +++ b/src/pages/blog/index.tsx @@ -1,34 +1,27 @@ import Notice from '@components/atoms/layout/notice'; -import PostsList, { type Post } from '@components/organisms/layout/posts-list'; +import PostsList from '@components/organisms/layout/posts-list'; import LinksListWidget from '@components/organisms/widgets/links-list-widget'; import { getLayout } from '@components/templates/layout/layout'; import PageLayout from '@components/templates/page/page-layout'; import { type EdgesResponse } from '@services/graphql/api'; -import { - getArticleFromRawData, - getArticles, - getTotalArticles, -} from '@services/graphql/articles'; +import { getArticles, getTotalArticles } from '@services/graphql/articles'; import { getThematicsPreview, getTotalThematics, } from '@services/graphql/thematics'; import { getTopicsPreview, getTotalTopics } from '@services/graphql/topics'; +import { type NextPageWithLayout } from '@ts/types/app'; import { - type Article, - type Meta, - type NextPageWithLayout, -} from '@ts/types/app'; -import { - RawThematicPreview, - RawTopicPreview, type RawArticle, + type RawThematicPreview, + type RawTopicPreview, } from '@ts/types/raw-data'; import { settings } from '@utils/config'; import { loadTranslation, type Messages } from '@utils/helpers/i18n'; import { getLinksListItems, getPageLinkFromRawData, + getPostsList, } from '@utils/helpers/pages'; import useBreadcrumb from '@utils/hooks/use-breadcrumb'; import usePagination from '@utils/hooks/use-pagination'; @@ -119,62 +112,6 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ '@graph': [webpageSchema, blogSchema], }; - /** - * Retrieve the formatted meta. - * - * @param {Meta<'article'>} meta - The article meta. - * @returns {Post['meta']} The formatted meta. - */ - const getPostMeta = (meta: Meta<'article'>): Post['meta'] => { - const { commentsCount, dates, thematics, wordsCount } = meta; - - return { - commentsCount, - dates, - readingTime: { wordsCount: wordsCount || 0, onlyMinutes: true }, - thematics: thematics?.map((thematic) => { - return { ...thematic, url: `/thematique/${thematic.slug}` }; - }), - }; - }; - - /** - * Retrieve the formatted posts. - * - * @param {Article[]} posts - An array of articles. - * @returns {Post[]} An array of formatted posts. - */ - const getPosts = (posts: Article[]): Post[] => { - return posts.map((post) => { - return { - ...post, - cover: post.meta.cover, - excerpt: post.intro, - meta: getPostMeta(post.meta), - url: `/article/${post.slug}`, - }; - }); - }; - - /** - * Retrieve the posts list from raw data. - * - * @param {EdgesResponse<RawArticle>[]} rawData - The raw data. - * @returns {Post[]} An array of posts. - */ - const getPostsList = (rawData: EdgesResponse<RawArticle>[]): Post[] => { - const articlesList: RawArticle[] = []; - rawData.forEach((articleData) => - articleData.edges.forEach((edge) => { - articlesList.push(edge.node); - }) - ); - - return getPosts( - articlesList.map((article) => getArticleFromRawData(article)) - ); - }; - const { data, error, diff --git a/src/pages/blog/page/[number].tsx b/src/pages/blog/page/[number].tsx index e8c93f7..8f50ddd 100644 --- a/src/pages/blog/page/[number].tsx +++ b/src/pages/blog/page/[number].tsx @@ -1,10 +1,9 @@ -import PostsList, { type Post } from '@components/organisms/layout/posts-list'; +import PostsList from '@components/organisms/layout/posts-list'; import LinksListWidget from '@components/organisms/widgets/links-list-widget'; import { getLayout } from '@components/templates/layout/layout'; import PageLayout from '@components/templates/page/page-layout'; import { type EdgesResponse } from '@services/graphql/api'; import { - getArticleFromRawData, getArticles, getArticlesEndCursor, getTotalArticles, @@ -14,11 +13,7 @@ import { getTotalThematics, } from '@services/graphql/thematics'; import { getTopicsPreview, getTotalTopics } from '@services/graphql/topics'; -import { - type Article, - type Meta, - type NextPageWithLayout, -} from '@ts/types/app'; +import { type NextPageWithLayout } from '@ts/types/app'; import { type RawArticle, type RawThematicPreview, @@ -29,6 +24,7 @@ import { loadTranslation, type Messages } from '@utils/helpers/i18n'; import { getLinksListItems, getPageLinkFromRawData, + getPostsList, } from '@utils/helpers/pages'; import useBreadcrumb from '@utils/hooks/use-breadcrumb'; import useRedirection from '@utils/hooks/use-redirection'; @@ -131,62 +127,6 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ '@graph': [webpageSchema, blogSchema], }; - /** - * Retrieve the formatted meta. - * - * @param {Meta<'article'>} meta - The article meta. - * @returns {Post['meta']} The formatted meta. - */ - const getPostMeta = (meta: Meta<'article'>): Post['meta'] => { - const { commentsCount, dates, thematics, wordsCount } = meta; - - return { - commentsCount, - dates, - readingTime: { wordsCount: wordsCount || 0, onlyMinutes: true }, - thematics: thematics?.map((thematic) => { - return { ...thematic, url: `/thematique/${thematic.slug}` }; - }), - }; - }; - - /** - * Retrieve the formatted posts. - * - * @param {Article[]} posts - An array of articles. - * @returns {Post[]} An array of formatted posts. - */ - const getPosts = (posts: Article[]): Post[] => { - return posts.map((post) => { - return { - ...post, - cover: post.meta.cover, - excerpt: post.intro, - meta: getPostMeta(post.meta), - url: `/article/${post.slug}`, - }; - }); - }; - - /** - * Retrieve the posts list from raw data. - * - * @param {EdgesResponse<RawArticle>[]} rawData - The raw data. - * @returns {Post[]} An array of posts. - */ - const getPostsList = (rawData: EdgesResponse<RawArticle>[]): Post[] => { - const articlesList: RawArticle[] = []; - rawData.forEach((articleData) => - articleData.edges.forEach((edge) => { - articlesList.push(edge.node); - }) - ); - - return getPosts( - articlesList.map((article) => getArticleFromRawData(article)) - ); - }; - const thematicsListTitle = intl.formatMessage({ defaultMessage: 'Thematics', description: 'BlogPage: thematics list widget title', |
