From de9a9eac060974a7878f2bb5577f2b135596a555 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 22 Jul 2022 19:00:50 +0200 Subject: refactor(article): wrap useSWR with a custom hook to revalidate article --- src/pages/article/[slug].tsx | 8 +++----- src/utils/hooks/use-article.tsx | 24 ++++++++++++++++++++++++ src/utils/hooks/use-comments.tsx | 8 ++++---- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/utils/hooks/use-article.tsx diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx index f720fa6..572a5fc 100644 --- a/src/pages/article/[slug].tsx +++ b/src/pages/article/[slug].tsx @@ -25,6 +25,7 @@ import { getSinglePageSchema, getWebPageSchema, } from '@utils/helpers/schema-org'; +import useArticle from '@utils/hooks/use-article'; import useBreadcrumb from '@utils/hooks/use-breadcrumb'; import useComments from '@utils/hooks/use-comments'; import usePrism, { type OptionalPrismPlugin } from '@utils/hooks/use-prism'; @@ -37,7 +38,6 @@ import Script from 'next/script'; import { ParsedUrlQuery } from 'querystring'; import { HTMLAttributes } from 'react'; import { useIntl } from 'react-intl'; -import useSWR from 'swr'; type ArticlePageProps = { comments: Comment[]; @@ -56,9 +56,7 @@ const ArticlePage: NextPageWithLayout = ({ }) => { const { isFallback } = useRouter(); const intl = useIntl(); - const { data: article } = useSWR(() => slug, getArticleBySlug, { - fallbackData: post, - }); + const article = useArticle(slug, post); const commentsData = useComments(post.id, comments); const { items: breadcrumbItems, schema: breadcrumbSchema } = useBreadcrumb({ title: article?.title || '', @@ -71,7 +69,7 @@ const ArticlePage: NextPageWithLayout = ({ if (isFallback) return ; - const { content, id, intro, meta, title } = article!; + const { content, id, intro, meta, title } = article; const { author, commentsCount, cover, dates, seo, thematics, topics } = meta; const headerMeta: PageLayoutProps['headerMeta'] = { diff --git a/src/utils/hooks/use-article.tsx b/src/utils/hooks/use-article.tsx new file mode 100644 index 0000000..3a173dd --- /dev/null +++ b/src/utils/hooks/use-article.tsx @@ -0,0 +1,24 @@ +import { fetchAPI, getAPIUrl } from '@services/graphql/api'; +import { getArticleFromRawData } from '@services/graphql/articles'; +import { articleBySlugQuery } from '@services/graphql/articles.query'; +import { Article } from '@ts/types/app'; +import { RawArticle } from '@ts/types/raw-data'; +import useSWR from 'swr'; + +/** + * Retrieve an article by slug. + * + * @param {string} slug - The article slug. + * @param {Article} fallback - A fallback article. + * @returns {Article} The matching article. + */ +const useArticle = (slug: string, fallback: Article): Article => { + const { data } = useSWR( + { api: getAPIUrl(), query: articleBySlugQuery, variables: { slug } }, + fetchAPI + ); + + return data ? getArticleFromRawData(data.post) : fallback; +}; + +export default useArticle; diff --git a/src/utils/hooks/use-comments.tsx b/src/utils/hooks/use-comments.tsx index cc9e560..f430f19 100644 --- a/src/utils/hooks/use-comments.tsx +++ b/src/utils/hooks/use-comments.tsx @@ -10,7 +10,8 @@ import useSWR from 'swr'; /** * Retrieve the comments of a page/article. - * @param contentId - A page/article id. + * + * @param {string | number} contentId - A page/article id. * @returns {Comment[]|undefined} */ const useComments = ( @@ -19,15 +20,14 @@ const useComments = ( ): Comment[] | undefined => { const { data } = useSWR( { api: getAPIUrl(), query: commentsQuery, variables: { contentId } }, - fetchAPI, - { fallback } + fetchAPI ); const comments = data?.comments.nodes.map((comment) => getCommentFromRawData(comment) ); - return comments ? buildCommentsTree(comments) : undefined; + return comments ? buildCommentsTree(comments) : fallback; }; export default useComments; -- cgit v1.2.3