Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/404 page #73

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import useRedirectWithTimeout from 'src/shared/hooks/useRedirectWithTimeout';
import Head from 'next/head';

export default function FourOhFour() {
const { secondsRemaining } = useRedirectWithTimeout('/', 5);
return (
<>
<Head>
<title>Page not found</title>
<meta
name="description"
content="Employee Pulse (404) - This page cannot be found."
/>
</Head>
<h1 className="text-2xl font-bold text-center">
This page cannot be found.
</h1>
<h2 className="mt-4 text-xl text-center">
Redirecting to Homepage in {secondsRemaining}
{secondsRemaining > 1 ? ' seconds' : ' second'}.
</h2>
</>
);
}
25 changes: 25 additions & 0 deletions src/shared/hooks/useRedirectWithTimeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';

export default function useRedirectWithTimeout(
redirectTo: string,
seconds: number
) {
const [secondsRemaining, setSecondsRemaining] = useState(seconds);
const router = useRouter();

useEffect(() => {
if (secondsRemaining === 0) router.push('/');

const timer = setTimeout(() => {
setSecondsRemaining((prevSecondsRemaining) => prevSecondsRemaining - 1);
if (secondsRemaining === 1) router.push(redirectTo);
}, 1000);

return () => {
clearInterval(timer);
};
}, [router, secondsRemaining, redirectTo]);

return { secondsRemaining };
}