aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-redirection.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-20 15:37:08 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-20 15:37:08 +0200
commitf4c7ab4e306d2f04324853e67032d370abd65d0c (patch)
tree2c7d1ad467d6c52bc134202f0d33f7524f9056fa /src/utils/hooks/use-redirection.tsx
parentbbd63400f94b43fde04449e0c71d14763d893e6a (diff)
chore: handle blog pagination when JS is disabled
Diffstat (limited to 'src/utils/hooks/use-redirection.tsx')
-rw-r--r--src/utils/hooks/use-redirection.tsx33
1 files changed, 33 insertions, 0 deletions
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;