Skip to content

Commit

Permalink
pathes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasia-front committed May 14, 2023
1 parent ce96b13 commit aa21e56
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const HomePage = lazy(() => import('../pages/Home'));
const RegisterPage = lazy(() => import('../pages/Register'));
const LoginPage = lazy(() => import('../pages/Login'));
const ContactsPage = lazy(() => import('../pages/Contacts'));
const NotFoundPage = lazy(() =>
import('../components/NotFoundPage/NotFoundPage')
);

export const App = () => {
const dispatch = useDispatch();
Expand All @@ -37,32 +40,33 @@ export const App = () => {
) : (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route path="/goit-react-hw-08-phonebook" element={<Layout />}>
<Route index element={<HomePage />} />
<Route
path="/register"
path="register"
element={
<RestrictedRoute
redirectTo="/contacts"
redirectTo="contacts"
component={<RegisterPage />}
/>
}
/>
<Route
path="/login"
path="login"
element={
<RestrictedRoute
redirectTo="/contacts"
redirectTo="contacts"
component={<LoginPage />}
/>
}
/>
<Route
path="/contacts"
path="contacts"
element={
<PrivateRoute redirectTo="/login" component={<ContactsPage />} />
<PrivateRoute redirectTo="login" component={<ContactsPage />} />
}
/>
<Route path="*" element={<NotFoundPage />} />
</Route>
</Routes>
<Toaster
Expand Down
44 changes: 44 additions & 0 deletions src/components/NotFoundPage/NotFoundPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Center, Title, Description } from './NotFoundPage.styled';
import React, { useState, useEffect } from 'react';

function RedirectingPage() {
const [secondsLeft, setSecondsLeft] = useState(5);

useEffect(() => {
const interval = setInterval(() => {
setSecondsLeft(seconds => seconds - 1);
}, 1000);

const timeout = setTimeout(() => {
window.location.replace('/goit-react-hw-08-phonebook');
}, 5000);

return () => {
clearInterval(interval);
clearTimeout(timeout);
};
}, []);

return (
<div>
<Title>Redirecting...</Title>
<Description>
You will be redirected to the home page in {secondsLeft} seconds.
</Description>
</div>
);
}

function NotFoundPage() {
return (
<Center>
<Title>404 Not Found</Title>
<Description>
Sorry, the page you are looking for could not be found.
</Description>
<RedirectingPage />
</Center>
);
}

export default NotFoundPage;
18 changes: 18 additions & 0 deletions src/components/NotFoundPage/NotFoundPage.styled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import styled from 'styled-components';

export const Center = styled.div`
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
`;

export const Title = styled.h1`
font-size: 30px;
font-weight: 700;
`;
export const Description = styled.p`
font-size: 20px;
font-weight: 500;
`;
5 changes: 4 additions & 1 deletion src/components/PrivateRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { useAuth } from 'hooks';
* - Otherwise render <Navigate> to redirectTo
*/

export const PrivateRoute = ({ component: Component, redirectTo = '/' }) => {
export const PrivateRoute = ({
component: Component,
redirectTo = '/goit-react-hw-08-phonebook',
}) => {
const { isLoggedIn, isRefreshing } = useAuth();
const shouldRedirect = !isLoggedIn && !isRefreshing;

Expand Down
5 changes: 4 additions & 1 deletion src/components/RestrictedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Navigate } from 'react-router-dom';
* - Otherwise render the component
*/

export const RestrictedRoute = ({ component: Component, redirectTo = '/' }) => {
export const RestrictedRoute = ({
component: Component,
redirectTo = '/goit-react-hw-08-phonebook',
}) => {
const { isLoggedIn } = useAuth();

return isLoggedIn ? <Navigate to={redirectTo} /> : Component;
Expand Down

0 comments on commit aa21e56

Please sign in to comment.