diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-20 15:37:08 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-20 15:37:08 +0200 |
| commit | f4c7ab4e306d2f04324853e67032d370abd65d0c (patch) | |
| tree | 2c7d1ad467d6c52bc134202f0d33f7524f9056fa /src/utils | |
| parent | bbd63400f94b43fde04449e0c71d14763d893e6a (diff) | |
chore: handle blog pagination when JS is disabled
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/hooks/use-breadcrumb.tsx | 3 | ||||
| -rw-r--r-- | src/utils/hooks/use-redirection.tsx | 33 |
2 files changed, 35 insertions, 1 deletions
diff --git a/src/utils/hooks/use-breadcrumb.tsx b/src/utils/hooks/use-breadcrumb.tsx index 087d400..130ebf1 100644 --- a/src/utils/hooks/use-breadcrumb.tsx +++ b/src/utils/hooks/use-breadcrumb.tsx @@ -40,6 +40,7 @@ const useBreadcrumb = ({ const { website } = useSettings(); const isArticle = url.startsWith('/article/'); const isHome = url === '/'; + const isPageNumber = url.includes('/page/'); const isProject = url.startsWith('/projets/'); const isSearch = url.startsWith('/recherche'); const isThematic = url.startsWith('/thematique/'); @@ -62,7 +63,7 @@ const useBreadcrumb = ({ if (isHome) return { items, schema }; - if (isArticle || isSearch || isThematic || isTopic) { + if (isArticle || isPageNumber || isSearch || isThematic || isTopic) { const blogLabel = intl.formatMessage({ defaultMessage: 'Blog', description: 'Breadcrumb: blog label', diff --git a/src/utils/hooks/use-redirection.tsx b/src/utils/hooks/use-redirection.tsx new file mode 100644 index 0000000..9eb26c2 --- /dev/null +++ b/src/utils/hooks/use-redirection.tsx @@ -0,0 +1,33 @@ +import { useRouter } from 'next/router'; +import { useEffect } from 'react'; + +export type RouterQuery = { + param: string; + value: string; +}; + +export type UseRedirectionProps = { + /** + * The router query. + */ + query: RouterQuery; + /** + * The redirection url. + */ + redirectTo: string; +}; + +/** + * Redirect to another url when router query match the given parameters. + * + * @param {UseRedirectionProps} props - The redirection parameters. + */ +const useRedirection = ({ query, redirectTo }: UseRedirectionProps) => { + const router = useRouter(); + + useEffect(() => { + if (router.query[query.param] === query.value) router.push(redirectTo); + }, [query, redirectTo, router]); +}; + +export default useRedirection; |
