From 70efcfeaa0603415dd992cb662d8efb960e6e49a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 26 Sep 2023 15:54:28 +0200 Subject: refactor(routes): replace hardcoded routes with constants It makes it easier to change a route if needed and it avoid typo mistakes. I also refactored a bit the concerned files to be complient with the new ESlint config. However, I should rewrite the pages to reduce the number of statements. --- src/pages/recherche/index.tsx | 54 +++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'src/pages/recherche') diff --git a/src/pages/recherche/index.tsx b/src/pages/recherche/index.tsx index 26cfd91..971d04a 100644 --- a/src/pages/recherche/index.tsx +++ b/src/pages/recherche/index.tsx @@ -1,7 +1,9 @@ -import { GetStaticProps } from 'next'; +/* eslint-disable max-statements */ +import type { GetStaticProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; import Script from 'next/script'; +import { useCallback } from 'react'; import { useIntl } from 'react-intl'; import { getLayout, @@ -19,12 +21,13 @@ import { getTotalThematics, getTotalTopics, } from '../../services/graphql'; -import { - type NextPageWithLayout, - type RawArticle, - type RawThematicPreview, - type RawTopicPreview, +import type { + NextPageWithLayout, + RawArticle, + RawThematicPreview, + RawTopicPreview, } from '../../types'; +import { ROUTES } from '../../utils/constants'; import { getBlogSchema, getLinksListItems, @@ -72,11 +75,14 @@ const SearchPage: NextPageWithLayout = ({ }); const { items: breadcrumbItems, schema: breadcrumbSchema } = useBreadcrumb({ title, - url: `/recherche`, + url: ROUTES.SEARCH, }); const { blog, website } = useSettings(); - const pageTitle = `${title} - ${website.name}`; + const page = { + title: `${title} - ${website.name}`, + url: `${website.url}${asPath}`, + }; const pageDescription = query.s ? intl.formatMessage( { @@ -99,7 +105,7 @@ const SearchPage: NextPageWithLayout = ({ description: pageDescription, locale: website.locales.default, slug: asPath, - title: pageTitle, + title: page.title, }); const blogSchema = getBlogSchema({ isSinglePage: false, @@ -122,16 +128,16 @@ const SearchPage: NextPageWithLayout = ({ search: query.s as string, }); - const totalArticles = useDataFromAPI(() => + const totalArticles = useDataFromAPI(async () => getTotalArticles(query.s as string) ); /** * Load more posts handler. */ - const loadMore = () => { + const loadMore = useCallback(() => { setSize((prevSize) => prevSize + 1); - }; + }, [setSize]); const thematicsListTitle = intl.formatMessage({ defaultMessage: 'Thematics', @@ -144,20 +150,25 @@ const SearchPage: NextPageWithLayout = ({ description: 'SearchPage: topics list widget title', id: 'N804XO', }); + const postsListBaseUrl = `${ROUTES.SEARCH}/page/`; return ( <> - {pageTitle} + {page.title} + {/*eslint-disable-next-line react/jsx-no-literals -- Name allowed */} - + + {/*eslint-disable-next-line react/jsx-no-literals -- Content allowed */}