From 95961199e0e338083f01e89b1a4bfb066ff02667 Mon Sep 17 00:00:00 2001 From: Vladislav Kutsevich Date: Fri, 29 Sep 2023 17:56:07 +0300 Subject: [PATCH] update --- src/App.jsx | 17 +++++++++++++---- src/components/Routes/PrivateRoute.jsx | 10 ++++++++-- src/components/Routes/RestrictedRoute.jsx | 17 +++++++++++++++++ src/components/Routes/index.js | 1 + src/pages/Error/Error.jsx | 16 ++++++++++++++-- 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/components/Routes/RestrictedRoute.jsx diff --git a/src/App.jsx b/src/App.jsx index 51302ce9..16157dde 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -6,7 +6,11 @@ import { fetchCurrentUser } from './redux/auth/operation'; import SharedLayout from './components/SharedLayout/SharedLayout'; -import { PrivateRoute, PublicRoute } from './components/Routes'; +import { + PrivateRoute, + PublicRoute, + RestrictedRoute, +} from './components/Routes'; import { UseAuth } from './hooks/useAuth'; const Home = lazy(() => import('../src/pages/Home/Home')); @@ -27,9 +31,11 @@ const Profile = lazy(() => import('./pages/Profile/Profile')); function App() { const dispatch = useDispatch(); - const { isRefreshing, isLoggedIn } = UseAuth(); + const { isRefreshing, isLoggedIn, user } = UseAuth(); const { pathname } = useLocation(); + const emptyUserParams = Object.keys(user.bodyParameters).length === 0; + if (isLoggedIn && pathname !== '/') { localStorage.setItem('location', pathname); } @@ -53,7 +59,10 @@ function App() { } redirectTo={'/diary'} /> + } + redirectTo={emptyUserParams ? '/params' : '/diary'} + /> } /> } redirectTo="/" />} + element={} redirectTo="/" />} /> { - const { isLoggedIn, isRefreshing } = UseAuth(); + const { isLoggedIn, isRefreshing, user } = UseAuth(); const shouldRedirect = isLoggedIn && !isRefreshing; - return shouldRedirect ? component : ; + const emptyUserParams = Object.keys(user.bodyParameters).length === 0; + + return shouldRedirect ? ( + component + ) : ( + + ); }; PrivateRoute.propTypes = { diff --git a/src/components/Routes/RestrictedRoute.jsx b/src/components/Routes/RestrictedRoute.jsx new file mode 100644 index 00000000..ba69ca7d --- /dev/null +++ b/src/components/Routes/RestrictedRoute.jsx @@ -0,0 +1,17 @@ +import { Navigate } from 'react-router-dom'; +import PropTypes from 'prop-types'; +import { UseAuth } from '../../hooks/useAuth'; + +const RestrictedRoute = ({ component, redirectTo = '/' }) => { + const { isLoggedIn, isRefreshing } = UseAuth(); + const shouldRedirect = isLoggedIn && !isRefreshing; + + return shouldRedirect ? component : ; +}; + +RestrictedRoute.propTypes = { + component: PropTypes.object.isRequired, + redirectTo: PropTypes.string, +}; + +export default RestrictedRoute; diff --git a/src/components/Routes/index.js b/src/components/Routes/index.js index 5bbde539..f2c789a4 100644 --- a/src/components/Routes/index.js +++ b/src/components/Routes/index.js @@ -1,2 +1,3 @@ export { default as PrivateRoute } from './PrivateRoute'; export { default as PublicRoute } from './PublicRoute'; +export { default as RestrictedRoute } from './RestrictedRoute'; diff --git a/src/pages/Error/Error.jsx b/src/pages/Error/Error.jsx index 2da2e902..842754c7 100644 --- a/src/pages/Error/Error.jsx +++ b/src/pages/Error/Error.jsx @@ -13,7 +13,19 @@ import sprite from '../../assets/sprite.svg'; import { UseAuth } from '../../hooks/useAuth'; const Error = () => { - const { isLoggedIn } = UseAuth(); + const { isLoggedIn, user } = UseAuth(); + + const emptyUserParams = Object.keys(user.bodyParameters).length === 0; + + const redirectRoute = () => { + if (isLoggedIn && emptyUserParams) { + return '/params'; + } else if (isLoggedIn && !emptyUserParams) { + return 'diary'; + } else if (isLoggedIn === false) { + return '/'; + } + }; return (
@@ -39,7 +51,7 @@ const Error = () => { type="button" text={'Go Home'} isorange={'true'} - to={isLoggedIn ? '/diary' : '/'} + to={redirectRoute()} />