diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-24 20:00:08 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-27 14:47:51 +0100 |
| commit | f111685c5886f3e77edfd3621c98d8ac1b9bcce4 (patch) | |
| tree | 62a541fe3afeb64bf745443706fbfb02e96c5230 /src/pages | |
| parent | bee515641cb144be9a855ff2cac258d2fedab21d (diff) | |
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
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/404.tsx | 37 | ||||
| -rw-r--r-- | src/pages/article/[slug].tsx | 39 | ||||
| -rw-r--r-- | src/pages/blog/index.tsx | 69 | ||||
| -rw-r--r-- | src/pages/blog/page/[number].tsx | 77 | ||||
| -rw-r--r-- | src/pages/index.tsx | 17 | ||||
| -rw-r--r-- | src/pages/projets/[slug].tsx | 4 | ||||
| -rw-r--r-- | src/pages/projets/index.tsx | 4 | ||||
| -rw-r--r-- | src/pages/recherche/index.tsx | 57 | ||||
| -rw-r--r-- | src/pages/sujet/[slug].tsx | 20 | ||||
| -rw-r--r-- | src/pages/thematique/[slug].tsx | 22 |
10 files changed, 174 insertions, 172 deletions
diff --git a/src/pages/404.tsx b/src/pages/404.tsx index d6785b6..5f4f89d 100644 --- a/src/pages/404.tsx +++ b/src/pages/404.tsx @@ -18,25 +18,26 @@ import { type SearchFormSubmit, } from '../components'; import { - getThematicsPreview, - getTopicsPreview, - getTotalThematics, - getTotalTopics, + convertTaxonomyToPageLink, + fetchThematicsCount, + fetchThematicsList, + fetchTopicsCount, + fetchTopicsList, } from '../services/graphql'; import type { NextPageWithLayout, - RawThematicPreview, - RawTopicPreview, + WPThematicPreview, + WPTopicPreview, } from '../types'; import { CONFIG } from '../utils/config'; import { ROUTES } from '../utils/constants'; -import { getLinksItemData, getPageLinkFromRawData } from '../utils/helpers'; +import { getLinksItemData } from '../utils/helpers'; import { loadTranslation, type Messages } from '../utils/helpers/server'; import { useBreadcrumb } from '../utils/hooks'; type Error404PageProps = { - thematicsList: RawThematicPreview[]; - topicsList: RawTopicPreview[]; + thematicsList: WPThematicPreview[]; + topicsList: WPTopicPreview[]; translation: Messages; }; @@ -146,11 +147,7 @@ const Error404Page: NextPageWithLayout<Error404PageProps> = ({ {thematicsListTitle} </Heading> } - items={getLinksItemData( - thematicsList.map((thematic) => - getPageLinkFromRawData(thematic, 'thematic') - ) - )} + items={getLinksItemData(thematicsList.map(convertTaxonomyToPageLink))} /> <LinksWidget heading={ @@ -158,9 +155,7 @@ const Error404Page: NextPageWithLayout<Error404PageProps> = ({ {topicsListTitle} </Heading> } - items={getLinksItemData( - topicsList.map((topic) => getPageLinkFromRawData(topic, 'topic')) - )} + items={getLinksItemData(topicsList.map(convertTaxonomyToPageLink))} /> </PageSidebar> </Page> @@ -172,10 +167,10 @@ Error404Page.getLayout = (page) => getLayout(page); export const getStaticProps: GetStaticProps<Error404PageProps> = async ({ locale, }) => { - const totalThematics = await getTotalThematics(); - const thematics = await getThematicsPreview({ first: totalThematics }); - const totalTopics = await getTotalTopics(); - const topics = await getTopicsPreview({ first: totalTopics }); + const totalThematics = await fetchThematicsCount(); + const thematics = await fetchThematicsList({ first: totalThematics }); + const totalTopics = await fetchTopicsCount(); + const topics = await fetchTopicsList({ first: totalTopics }); const translation = await loadTranslation(locale); return { diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx index 224b1c5..f228ff0 100644 --- a/src/pages/article/[slug].tsx +++ b/src/pages/article/[slug].tsx @@ -21,9 +21,11 @@ import { TocWidget, } from '../../components'; import { - getAllArticlesSlugs, - getAllComments, - getArticleBySlug, + convertPostToArticle, + convertWPCommentToComment, + fetchAllPostsSlugs, + fetchCommentsList, + fetchPost, } from '../../services/graphql'; import styles from '../../styles/pages/article.module.scss'; import type { Article, NextPageWithLayout, SingleComment } from '../../types'; @@ -63,8 +65,11 @@ const ArticlePage: NextPageWithLayout<ArticlePageProps> = ({ const intl = useIntl(); const article = useArticle({ slug, fallback: post }); const commentsData = useComments({ - contentId: article?.id, fallback: comments, + first: article?.meta.commentsCount, + where: { + contentId: article?.id ?? post.id, + }, }); const getComments = (data?: SingleComment[]) => @@ -73,7 +78,7 @@ const ArticlePage: NextPageWithLayout<ArticlePageProps> = ({ author: comment.meta.author, content: comment.content, id: comment.id, - isApproved: comment.approved, + isApproved: comment.isApproved, publicationDate: comment.meta.date, replies: getComments(comment.replies), }; @@ -255,7 +260,7 @@ const ArticlePage: NextPageWithLayout<ArticlePageProps> = ({ heading={title} intro={intro} meta={{ - author: author?.name, + author, publicationDate: dates.publication, thematics, updateDate: dates.update, @@ -292,11 +297,7 @@ const ArticlePage: NextPageWithLayout<ArticlePageProps> = ({ ]} /> </PageSidebar> - <PageComments - comments={articleComments ?? []} - depth={2} - pageId={id as number} - /> + <PageComments comments={articleComments ?? []} depth={2} pageId={id} /> </Page> ); }; @@ -311,14 +312,20 @@ export const getStaticProps: GetStaticProps<ArticlePageProps> = async ({ locale, params, }) => { - const post = await getArticleBySlug((params as PostParams).slug); - const comments = await getAllComments({ contentId: post.id as number }); + const post = await fetchPost((params as PostParams).slug); + const article = await convertPostToArticle(post); + const comments = await fetchCommentsList({ + first: post.commentCount ?? 1, + where: { contentId: post.databaseId }, + }); const translation = await loadTranslation(locale); return { props: { - comments: JSON.parse(JSON.stringify(comments)), - post: JSON.parse(JSON.stringify(post)), + comments: JSON.parse( + JSON.stringify(comments.map(convertWPCommentToComment)) + ), + post: JSON.parse(JSON.stringify(article)), slug: post.slug, translation, }, @@ -326,7 +333,7 @@ export const getStaticProps: GetStaticProps<ArticlePageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await getAllArticlesSlugs(); + const slugs = await fetchAllPostsSlugs(); const paths = slugs.map((slug) => { return { params: { slug } }; }); diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx index 0de5523..56cbb02 100644 --- a/src/pages/blog/index.tsx +++ b/src/pages/blog/index.tsx @@ -20,27 +20,28 @@ import { PageSidebar, } from '../../components'; import { - getArticles, - getThematicsPreview, - getTopicsPreview, - getTotalArticles, - getTotalThematics, - getTotalTopics, + convertTaxonomyToPageLink, + fetchPostsCount, + fetchPostsList, + fetchThematicsCount, + fetchThematicsList, + fetchTopicsCount, + fetchTopicsList, } from '../../services/graphql'; import styles from '../../styles/pages/blog.module.scss'; import type { - EdgesResponse, + GraphQLConnection, NextPageWithLayout, - RawArticle, - RawThematicPreview, - RawTopicPreview, + WPPostPreview, + WPThematicPreview, + WPTopicPreview, } from '../../types'; import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { getBlogSchema, getLinksItemData, - getPageLinkFromRawData, + getPostsWithUrl, getSchemaJson, getWebPageSchema, } from '../../utils/helpers'; @@ -48,9 +49,9 @@ import { loadTranslation, type Messages } from '../../utils/helpers/server'; import { useBreadcrumb, useIsMounted, usePostsList } from '../../utils/hooks'; type BlogPageProps = { - articles: EdgesResponse<RawArticle>; - thematicsList: RawThematicPreview[]; - topicsList: RawTopicPreview[]; + posts: GraphQLConnection<WPPostPreview>; + thematicsList: WPThematicPreview[]; + topicsList: WPTopicPreview[]; totalArticles: number; translation: Messages; }; @@ -59,7 +60,7 @@ type BlogPageProps = { * Blog index page. */ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ - articles, + posts, thematicsList, topicsList, totalArticles, @@ -111,6 +112,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ const schemaJsonLd = getSchemaJson([webpageSchema, blogSchema]); const { + articles, error, firstNewResultIndex, isLoading, @@ -118,10 +120,9 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ isRefreshing, hasNextPage, loadMore, - posts, } = usePostsList({ - fallback: [articles], - fetcher: getArticles, + fallback: [posts], + fetcher: fetchPostsList, perPage: CONFIG.postsPerPage, }); @@ -191,6 +192,10 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ id: 'AXe1Iz', }); + const blogArticles = articles?.flatMap((p) => + p.edges.map((edge) => edge.node) + ); + return ( <Page breadcrumbs={breadcrumbItems} isBodyLastChild> <Head> @@ -218,13 +223,13 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ /> <PageHeader heading={title} meta={{ total: totalArticles }} /> <PageBody className={styles.body}> - {posts ? ( + {blogArticles ? ( <PostsList className={styles.list} firstNewResult={firstNewResultIndex} isLoading={isLoading || isLoadingMore || isRefreshing} onLoadMore={hasNextPage && isMounted ? loadMore : undefined} - posts={posts} + posts={getPostsWithUrl(blogArticles)} ref={postsListRef} sortByYear total={isMounted ? totalArticles : undefined} @@ -260,11 +265,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ {thematicsListTitle} </Heading> } - items={getLinksItemData( - thematicsList.map((thematic) => - getPageLinkFromRawData(thematic, 'thematic') - ) - )} + items={getLinksItemData(thematicsList.map(convertTaxonomyToPageLink))} /> <LinksWidget heading={ @@ -272,9 +273,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ {topicsListTitle} </Heading> } - items={getLinksItemData( - topicsList.map((topic) => getPageLinkFromRawData(topic, 'topic')) - )} + items={getLinksItemData(topicsList.map(convertTaxonomyToPageLink))} /> </PageSidebar> </Page> @@ -286,17 +285,17 @@ BlogPage.getLayout = (page) => getLayout(page); export const getStaticProps: GetStaticProps<BlogPageProps> = async ({ locale, }) => { - const articles = await getArticles({ first: CONFIG.postsPerPage }); - const totalArticles = await getTotalArticles(); - const totalThematics = await getTotalThematics(); - const thematics = await getThematicsPreview({ first: totalThematics }); - const totalTopics = await getTotalTopics(); - const topics = await getTopicsPreview({ first: totalTopics }); + const posts = await fetchPostsList({ first: CONFIG.postsPerPage }); + const totalArticles = await fetchPostsCount(); + const totalThematics = await fetchThematicsCount(); + const thematics = await fetchThematicsList({ first: totalThematics }); + const totalTopics = await fetchTopicsCount(); + const topics = await fetchTopicsList({ first: totalTopics }); const translation = await loadTranslation(locale); return { props: { - articles: JSON.parse(JSON.stringify(articles)), + posts: JSON.parse(JSON.stringify(posts)), thematicsList: thematics.edges.map((edge) => edge.node), topicsList: topics.edges.map((edge) => edge.node), totalArticles, diff --git a/src/pages/blog/page/[number].tsx b/src/pages/blog/page/[number].tsx index b254603..d6071d1 100644 --- a/src/pages/blog/page/[number].tsx +++ b/src/pages/blog/page/[number].tsx @@ -20,27 +20,28 @@ import { PageSidebar, } from '../../../components'; import { - getArticles, - getArticlesEndCursor, - getThematicsPreview, - getTopicsPreview, - getTotalArticles, - getTotalThematics, - getTotalTopics, + convertTaxonomyToPageLink, + fetchLastPostCursor, + fetchPostsCount, + fetchPostsList, + fetchThematicsCount, + fetchThematicsList, + fetchTopicsCount, + fetchTopicsList, } from '../../../services/graphql'; import type { - EdgesResponse, + GraphQLConnection, NextPageWithLayout, - RawArticle, - RawThematicPreview, - RawTopicPreview, + WPPostPreview, + WPThematicPreview, + WPTopicPreview, } from '../../../types'; import { CONFIG } from '../../../utils/config'; import { ROUTES } from '../../../utils/constants'; import { getBlogSchema, getLinksItemData, - getPageLinkFromRawData, + getPostsWithUrl, getSchemaJson, getWebPageSchema, } from '../../../utils/helpers'; @@ -52,10 +53,10 @@ import { } from '../../../utils/hooks'; type BlogPageProps = { - articles: EdgesResponse<RawArticle>; pageNumber: number; - thematicsList: RawThematicPreview[]; - topicsList: RawTopicPreview[]; + posts: GraphQLConnection<WPPostPreview>; + thematicsList: WPThematicPreview[]; + topicsList: WPTopicPreview[]; totalArticles: number; translation: Messages; }; @@ -64,8 +65,8 @@ type BlogPageProps = { * Blog index page. */ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ - articles, pageNumber, + posts, thematicsList, topicsList, totalArticles, @@ -75,9 +76,9 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ redirectTo: ROUTES.BLOG, }); - const { posts } = usePostsList({ - fallback: [articles], - fetcher: getArticles, + const { articles } = usePostsList({ + fallback: [posts], + fetcher: fetchPostsList, perPage: CONFIG.postsPerPage, }); const intl = useIntl(); @@ -195,6 +196,10 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ id: 'AXe1Iz', }); + const blogPageArticles = articles?.flatMap((p) => + p.edges.map((edge) => edge.node) + ); + return ( <Page breadcrumbs={breadcrumbItems} isBodyLastChild> <Head> @@ -225,7 +230,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ meta={{ total: totalArticles }} /> <PageBody> - <PostsList posts={posts ?? []} sortByYear /> + <PostsList posts={getPostsWithUrl(blogPageArticles ?? [])} sortByYear /> <Pagination aria-label={paginationAriaLabel} current={pageNumber} @@ -242,11 +247,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ {thematicsListTitle} </Heading> } - items={getLinksItemData( - thematicsList.map((thematic) => - getPageLinkFromRawData(thematic, 'thematic') - ) - )} + items={getLinksItemData(thematicsList.map(convertTaxonomyToPageLink))} /> <LinksWidget heading={ @@ -254,9 +255,7 @@ const BlogPage: NextPageWithLayout<BlogPageProps> = ({ {topicsListTitle} </Heading> } - items={getLinksItemData( - topicsList.map((topic) => getPageLinkFromRawData(topic, 'topic')) - )} + items={getLinksItemData(topicsList.map(convertTaxonomyToPageLink))} /> </PageSidebar> </Page> @@ -274,23 +273,23 @@ export const getStaticProps: GetStaticProps<BlogPageProps> = async ({ params, }) => { const pageNumber = Number((params as BlogPageParams).number); - const lastCursor = await getArticlesEndCursor({ - first: CONFIG.postsPerPage * pageNumber, - }); - const articles = await getArticles({ + const lastCursor = await fetchLastPostCursor( + CONFIG.postsPerPage * pageNumber + ); + const posts = await fetchPostsList({ first: CONFIG.postsPerPage, after: lastCursor, }); - const totalArticles = await getTotalArticles(); - const totalThematics = await getTotalThematics(); - const thematics = await getThematicsPreview({ first: totalThematics }); - const totalTopics = await getTotalTopics(); - const topics = await getTopicsPreview({ first: totalTopics }); + const totalArticles = await fetchPostsCount(); + const totalThematics = await fetchThematicsCount(); + const thematics = await fetchThematicsList({ first: totalThematics }); + const totalTopics = await fetchTopicsCount(); + const topics = await fetchTopicsList({ first: totalTopics }); const translation = await loadTranslation(locale); return { props: { - articles: JSON.parse(JSON.stringify(articles)), + posts: JSON.parse(JSON.stringify(posts)), pageNumber, thematicsList: thematics.edges.map((edge) => edge.node), topicsList: topics.edges.map((edge) => edge.node), @@ -301,7 +300,7 @@ export const getStaticProps: GetStaticProps<BlogPageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const totalArticles = await getTotalArticles(); + const totalArticles = await fetchPostsCount(); const totalPages = Math.ceil(totalArticles / CONFIG.postsPerPage); const pagesArray = Array.from( { length: totalPages }, diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 56de5b5..7bd8aec 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -26,9 +26,12 @@ import { } from '../components'; import { mdxComponents } from '../components/mdx'; import HomePageContent from '../content/pages/homepage.mdx'; -import { getArticlesCard } from '../services/graphql'; +import { + convertRecentPostToRecentArticle, + fetchRecentPosts, +} from '../services/graphql'; import styles from '../styles/pages/home.module.scss'; -import type { ArticleCard, NextPageWithLayout } from '../types'; +import type { NextPageWithLayout, RecentArticle } from '../types'; import { CONFIG } from '../utils/config'; import { PERSONAL_LINKS, ROUTES } from '../utils/constants'; import { getSchemaJson, getWebPageSchema } from '../utils/helpers'; @@ -229,7 +232,7 @@ const HomePageSection: FC<PageSectionProps> = ({ ); type HomeProps = { - recentPosts: ArticleCard[]; + recentPosts: RecentArticle[]; translation?: Messages; }; @@ -277,7 +280,7 @@ const HomePage: NextPageWithLayout<HomeProps> = ({ recentPosts }) => { hasBorderedValues isCentered label={publicationDate} - value={<Time date={post.dates.publication} />} + value={<Time date={post.publicationDate} />} /> </CardMeta> } @@ -365,11 +368,13 @@ HomePage.getLayout = (page) => getLayout(page, { isHome: true }); export const getStaticProps: GetStaticProps<HomeProps> = async ({ locale }) => { const translation = await loadTranslation(locale); - const recentPosts = await getArticlesCard({ first: 3 }); + const recentPosts = await fetchRecentPosts({ first: 3 }); return { props: { - recentPosts, + recentPosts: recentPosts.edges.map((edge) => + convertRecentPostToRecentArticle(edge.node) + ), translation, }, }; diff --git a/src/pages/projets/[slug].tsx b/src/pages/projets/[slug].tsx index 2911951..ee88638 100644 --- a/src/pages/projets/[slug].tsx +++ b/src/pages/projets/[slug].tsx @@ -24,7 +24,7 @@ import { } from '../../components'; import { mdxComponents } from '../../components/mdx'; import styles from '../../styles/pages/project.module.scss'; -import type { NextPageWithLayout, ProjectPreview, Repos } from '../../types'; +import type { NextPageWithLayout, Project, Repos } from '../../types'; import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { @@ -45,7 +45,7 @@ import { } from '../../utils/hooks'; type ProjectPageProps = { - project: ProjectPreview; + project: Project; translation: Messages; }; diff --git a/src/pages/projets/index.tsx b/src/pages/projets/index.tsx index 00c5a70..4e0bf92 100644 --- a/src/pages/projets/index.tsx +++ b/src/pages/projets/index.tsx @@ -22,7 +22,7 @@ import { import { mdxComponents } from '../../components/mdx'; import PageContent, { meta } from '../../content/pages/projects.mdx'; import styles from '../../styles/pages/projects.module.scss'; -import type { NextPageWithLayout, ProjectCard } from '../../types'; +import type { NextPageWithLayout, ProjectPreview } from '../../types'; import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { @@ -38,7 +38,7 @@ import { import { useBreadcrumb } from '../../utils/hooks'; type ProjectsPageProps = { - projects: ProjectCard[]; + projects: ProjectPreview[]; translation?: Messages; }; diff --git a/src/pages/recherche/index.tsx b/src/pages/recherche/index.tsx index 2a18aa3..293df0e 100644 --- a/src/pages/recherche/index.tsx +++ b/src/pages/recherche/index.tsx @@ -20,25 +20,26 @@ import { PageBody, } from '../../components'; import { - getArticles, - getThematicsPreview, - getTopicsPreview, - getTotalArticles, - getTotalThematics, - getTotalTopics, + convertTaxonomyToPageLink, + fetchPostsCount, + fetchPostsList, + fetchThematicsCount, + fetchThematicsList, + fetchTopicsCount, + fetchTopicsList, } from '../../services/graphql'; import styles from '../../styles/pages/blog.module.scss'; import type { NextPageWithLayout, - RawThematicPreview, - RawTopicPreview, + WPThematicPreview, + WPTopicPreview, } from '../../types'; import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { getBlogSchema, getLinksItemData, - getPageLinkFromRawData, + getPostsWithUrl, getSchemaJson, getWebPageSchema, } from '../../utils/helpers'; @@ -46,8 +47,8 @@ import { loadTranslation, type Messages } from '../../utils/helpers/server'; import { useBreadcrumb, useDataFromAPI, usePostsList } from '../../utils/hooks'; type SearchPageProps = { - thematicsList: RawThematicPreview[]; - topicsList: RawTopicPreview[]; + thematicsList: WPThematicPreview[]; + topicsList: WPTopicPreview[]; translation: Messages; }; @@ -115,6 +116,7 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ const schemaJsonLd = getSchemaJson([webpageSchema, blogSchema]); const { + articles, error, firstNewResultIndex, isLoading, @@ -122,16 +124,15 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ isRefreshing, hasNextPage, loadMore, - posts, } = usePostsList({ fallback: [], - fetcher: getArticles, + fetcher: fetchPostsList, perPage: CONFIG.postsPerPage, searchQuery: query.s as string, }); const totalArticles = useDataFromAPI<number>(async () => - getTotalArticles(query.s as string) + fetchPostsCount({ search: query.s as string }) ); const thematicsListTitle = intl.formatMessage({ @@ -172,6 +173,10 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ [intl, routerPush] ); + const foundArticles = articles?.flatMap((p) => + p.edges.map((edge) => edge.node) + ); + return ( <Page breadcrumbs={breadcrumbItems} isBodyLastChild> <Head> @@ -199,14 +204,14 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ /> <PageHeader heading={title} meta={{ total: totalArticles }} /> <PageBody className={styles.body}> - {posts ? null : <Spinner>{loadingResults}</Spinner>} - {posts?.length ? ( + {foundArticles ? null : <Spinner>{loadingResults}</Spinner>} + {foundArticles?.length ? ( <PostsList className={styles.list} firstNewResult={firstNewResultIndex} isLoading={isLoading || isLoadingMore || isRefreshing} onLoadMore={hasNextPage ? loadMore : undefined} - posts={posts} + posts={getPostsWithUrl(foundArticles)} sortByYear /> ) : ( @@ -248,11 +253,7 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ {thematicsListTitle} </Heading> } - items={getLinksItemData( - thematicsList.map((thematic) => - getPageLinkFromRawData(thematic, 'thematic') - ) - )} + items={getLinksItemData(thematicsList.map(convertTaxonomyToPageLink))} /> <LinksWidget heading={ @@ -260,9 +261,7 @@ const SearchPage: NextPageWithLayout<SearchPageProps> = ({ {topicsListTitle} </Heading> } - items={getLinksItemData( - topicsList.map((topic) => getPageLinkFromRawData(topic, 'topic')) - )} + items={getLinksItemData(topicsList.map(convertTaxonomyToPageLink))} /> </PageSidebar> </Page> @@ -274,10 +273,10 @@ SearchPage.getLayout = (page) => getLayout(page); export const getStaticProps: GetStaticProps<SearchPageProps> = async ({ locale, }) => { - const totalThematics = await getTotalThematics(); - const thematics = await getThematicsPreview({ first: totalThematics }); - const totalTopics = await getTotalTopics(); - const topics = await getTopicsPreview({ first: totalTopics }); + const totalThematics = await fetchThematicsCount(); + const thematics = await fetchThematicsList({ first: totalThematics }); + const totalTopics = await fetchTopicsCount(); + const topics = await fetchTopicsList({ first: totalTopics }); const translation = await loadTranslation(locale); return { diff --git a/src/pages/sujet/[slug].tsx b/src/pages/sujet/[slug].tsx index 30adec3..aed7ea9 100644 --- a/src/pages/sujet/[slug].tsx +++ b/src/pages/sujet/[slug].tsx @@ -18,10 +18,11 @@ import { PageBody, } from '../../components'; import { - getAllTopicsSlugs, - getTopicBySlug, - getTopicsPreview, - getTotalTopics, + convertTaxonomyToPageLink, + fetchAllTopicsSlugs, + fetchTopic, + fetchTopicsCount, + fetchTopicsList, } from '../../services/graphql'; import styles from '../../styles/pages/blog.module.scss'; import type { NextPageWithLayout, PageLink, Topic } from '../../types'; @@ -29,7 +30,6 @@ import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { getLinksItemData, - getPageLinkFromRawData, getPostsWithUrl, getSchemaJson, getSinglePageSchema, @@ -208,13 +208,13 @@ export const getStaticProps: GetStaticProps<TopicPageProps> = async ({ locale, params, }) => { - const currentTopic = await getTopicBySlug((params as TopicParams).slug); - const totalTopics = await getTotalTopics(); - const allTopicsEdges = await getTopicsPreview({ + const currentTopic = await fetchTopic((params as TopicParams).slug); + const totalTopics = await fetchTopicsCount(); + const allTopicsEdges = await fetchTopicsList({ first: totalTopics, }); const allTopics = allTopicsEdges.edges.map((edge) => - getPageLinkFromRawData(edge.node, 'topic') + convertTaxonomyToPageLink(edge.node) ); const topicsLinks = allTopics.filter( (topic) => topic.url !== `${ROUTES.TOPICS}/${(params as TopicParams).slug}` @@ -231,7 +231,7 @@ export const getStaticProps: GetStaticProps<TopicPageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await getAllTopicsSlugs(); + const slugs = await fetchAllTopicsSlugs(); const paths = slugs.map((slug) => { return { params: { slug } }; }); diff --git a/src/pages/thematique/[slug].tsx b/src/pages/thematique/[slug].tsx index b8518c5..a44c98b 100644 --- a/src/pages/thematique/[slug].tsx +++ b/src/pages/thematique/[slug].tsx @@ -17,10 +17,11 @@ import { PageBody, } from '../../components'; import { - getAllThematicsSlugs, - getThematicBySlug, - getThematicsPreview, - getTotalThematics, + convertTaxonomyToPageLink, + fetchAllThematicsSlugs, + fetchThematic, + fetchThematicsCount, + fetchThematicsList, } from '../../services/graphql'; import styles from '../../styles/pages/blog.module.scss'; import type { NextPageWithLayout, PageLink, Thematic } from '../../types'; @@ -28,7 +29,6 @@ import { CONFIG } from '../../utils/config'; import { ROUTES } from '../../utils/constants'; import { getLinksItemData, - getPageLinkFromRawData, getPostsWithUrl, getSchemaJson, getSinglePageSchema, @@ -191,15 +191,13 @@ export const getStaticProps: GetStaticProps<ThematicPageProps> = async ({ locale, params, }) => { - const currentThematic = await getThematicBySlug( - (params as ThematicParams).slug - ); - const totalThematics = await getTotalThematics(); - const allThematicsEdges = await getThematicsPreview({ + const currentThematic = await fetchThematic((params as ThematicParams).slug); + const totalThematics = await fetchThematicsCount(); + const allThematicsEdges = await fetchThematicsList({ first: totalThematics, }); const allThematics = allThematicsEdges.edges.map((edge) => - getPageLinkFromRawData(edge.node, 'thematic') + convertTaxonomyToPageLink(edge.node) ); const allThematicsLinks = allThematics.filter( (thematic) => @@ -218,7 +216,7 @@ export const getStaticProps: GetStaticProps<ThematicPageProps> = async ({ }; export const getStaticPaths: GetStaticPaths = async () => { - const slugs = await getAllThematicsSlugs(); + const slugs = await fetchAllThematicsSlugs(); const paths = slugs.map((slug) => { return { params: { slug } }; }); |
