aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/recherche
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/pages/recherche
parent0f8f963ba3eccd7fd94785bf7fb216b6287cec57 (diff)
refactor: reduce the number of data transformation
Diffstat (limited to 'src/pages/recherche')
-rw-r--r--src/pages/recherche/index.tsx76
1 files changed, 6 insertions, 70 deletions
diff --git a/src/pages/recherche/index.tsx b/src/pages/recherche/index.tsx
index 8895015..c69d931 100644
--- a/src/pages/recherche/index.tsx
+++ b/src/pages/recherche/index.tsx
@@ -1,34 +1,26 @@
import Notice from '@components/atoms/layout/notice';
import Spinner from '@components/atoms/loaders/spinner';
-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 { loadTranslation, type Messages } from '@utils/helpers/i18n';
import {
getLinksListItems,
getPageLinkFromRawData,
+ getPostsList,
} from '@utils/helpers/pages';
import useBreadcrumb from '@utils/hooks/use-breadcrumb';
import useDataFromAPI from '@utils/hooks/use-data-from-api';
@@ -146,62 +138,6 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({
);
/**
- * 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))
- );
- };
-
- /**
* Load more posts handler.
*/
const loadMore = () => {