From d9c74d25ab1f4e13cb980074e188ef6125d2d290 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 7 Aug 2020 21:53:48 +0300 Subject: feat: setup initial routing --- src/pages/Page.tsx | 46 +++++++++++++++++++++++++++++++--------------- src/pages/PrivateRoute.tsx | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 src/pages/PrivateRoute.tsx (limited to 'src') diff --git a/src/pages/Page.tsx b/src/pages/Page.tsx index 56d7372..62b6b61 100644 --- a/src/pages/Page.tsx +++ b/src/pages/Page.tsx @@ -2,6 +2,7 @@ import React from 'react'; import { makeStyles, useTheme } from '@material-ui/core/styles'; import { useMediaQuery } from '@material-ui/core'; import { SnackbarProvider } from 'notistack'; +import { BrowserRouter, Switch, Route } from 'react-router-dom'; import ProfilePage from './ProfilePage/ProfilePage'; import FeedPage from './FeedPage/FeedPage'; @@ -9,6 +10,7 @@ import AuthPage from './AuthPage/AuthPage'; import HomePage from './HomePage/HomePage'; import NotificationsPage from './NotificationsPage/NotificationsPage'; import { useNavigate } from '../hooks/useNavigate'; +import PrivateRoute from './PrivateRoute'; const useStyles = makeStyles(theme => ({ @@ -22,6 +24,16 @@ const useStyles = makeStyles(theme => ({ } })); + +const urls = { + home: '/', + login: '/login', + registration: '/registration', + profile: '/profile', + feed: '/feed', + notifications: '/notifications' +}; + const Page: React.FC = () => { const { page } = useNavigate(); const classes = useStyles(); @@ -29,21 +41,25 @@ const Page: React.FC = () => { const isMobile = useMediaQuery(theme.breakpoints.down('sm')); return ( - -
- { page.prefix === 'home' && } - { page.prefix === 'profile' && } - { page.prefix === 'feed' && } - { page.prefix === 'auth' && } - { page.prefix === 'notifications' && } -
-
+ + +
+ + + + + + + +
+
+
); }; diff --git a/src/pages/PrivateRoute.tsx b/src/pages/PrivateRoute.tsx new file mode 100644 index 0000000..cdee67b --- /dev/null +++ b/src/pages/PrivateRoute.tsx @@ -0,0 +1,36 @@ +import React from 'react'; +import { Redirect, Route } from 'react-router-dom'; +import { useAuth } from '../hooks/useAuth'; + +const urls = { + home: '/', + login: '/login', + registration: '/registration', + profile: '/profile', + feed: '/feed', + notifications: '/notifications' +}; + +const PrivateRoute: React.FC = ({ component: ProtectedComponent, ...rest }) => { + const { isAuthenticated } = useAuth(); + + const getComponent: React.FC = (props) => { + if (props.match.path === urls.login || props.match.path === urls.registration) { + return isAuthenticated() ? ( + + ) : ( + + ); + } + + return isAuthenticated() ? ( + + ) : ( + + ); + } + + return ; +}; + +export default PrivateRoute; -- cgit v1.2.3