diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-07 22:25:20 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-07 22:25:20 +0300 |
commit | 2d6ba7459fff67823b55cfc39342896873962714 (patch) | |
tree | abdbe2503cc9a08226175cac1fad0e09eed698be | |
parent | 056ef91e36f86a3278dfbb2f369939534bffe7e2 (diff) | |
download | which-ui-2d6ba7459fff67823b55cfc39342896873962714.tar.gz |
refactor: simplify Route component
-rw-r--r-- | src/pages/Page.tsx | 15 | ||||
-rw-r--r-- | src/pages/PrivateRoute.tsx | 29 | ||||
-rw-r--r-- | src/pages/Route.tsx | 11 |
3 files changed, 19 insertions, 36 deletions
diff --git a/src/pages/Page.tsx b/src/pages/Page.tsx index 85b351c..29b9564 100644 --- a/src/pages/Page.tsx +++ b/src/pages/Page.tsx @@ -2,14 +2,14 @@ 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 { BrowserRouter, Switch } from 'react-router-dom'; import ProfilePage from './ProfilePage/ProfilePage'; import FeedPage from './FeedPage/FeedPage'; import AuthPage from './AuthPage/AuthPage'; import HomePage from './HomePage/HomePage'; import NotificationsPage from './NotificationsPage/NotificationsPage'; -import PrivateRoute from './PrivateRoute'; +import Route from './Route'; import urls from './urls'; @@ -41,11 +41,12 @@ const Page: React.FC = () => { > <div className={classes.root}> <Switch> - <PrivateRoute exact path={urls.home} component={HomePage} /> - <PrivateRoute exact path={urls.login} component={AuthPage} /> - <PrivateRoute exact path={urls.registration} component={AuthPage} /> - <PrivateRoute exact path={urls.feed} component={FeedPage} /> - <PrivateRoute exact path={urls.notifications} component={NotificationsPage} /> + <Route exact path={urls.home} component={HomePage} /> + <Route exact path={urls.login} component={AuthPage} /> + <Route exact path={urls.registration} component={AuthPage} /> + <Route exact path={urls.feed} component={FeedPage} /> + <Route exact path={urls.notifications} component={NotificationsPage} /> + <Route path={urls.profile} component={ProfilePage} /> </Switch> </div> </SnackbarProvider> diff --git a/src/pages/PrivateRoute.tsx b/src/pages/PrivateRoute.tsx deleted file mode 100644 index 685e53d..0000000 --- a/src/pages/PrivateRoute.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import React from 'react'; -import { Redirect, Route } from 'react-router-dom'; -import { useAuth } from '../hooks/useAuth'; -import urls from './urls'; - - -const PrivateRoute: React.FC<any> = ({ component: ProtectedComponent, ...rest }) => { - const { isAuthenticated } = useAuth(); - - const getComponent: React.FC<any> = (props) => { - if (props.match.path === urls.login || props.match.path === urls.registration) { - return isAuthenticated() ? ( - <Redirect to={urls.profile} /> - ) : ( - <ProtectedComponent {...props} /> - ); - } - - return isAuthenticated() ? ( - <ProtectedComponent {...props} /> - ) : ( - <Redirect to={{ pathname: urls.login, state: { from: props.location } }} /> - ); - } - - return <Route {...rest} render={getComponent} />; -}; - -export default PrivateRoute; diff --git a/src/pages/Route.tsx b/src/pages/Route.tsx new file mode 100644 index 0000000..fdd6f96 --- /dev/null +++ b/src/pages/Route.tsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { Route as BaseRoute } from 'react-router-dom'; + + +const Route: React.FC<any> = ({ component: Component, ...rest }) => { + const render: React.FC<any> = (props) => <Component {...props} />; + + return <BaseRoute render={render} {...rest} />; +}; + +export default Route; |