fix: isLogged is now accounted into the reducer and redirection logic is moved to the AppLayout (#322)

This commit is contained in:
pal-sig 2021-10-11 16:21:15 +05:30 committed by GitHub
parent d69a637275
commit 2c1c0ceea6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 16 deletions

View File

@ -1,12 +1,10 @@
import NotFound from 'components/NotFound';
import Spinner from 'components/Spinner';
import { IS_LOGGED_IN } from 'constants/auth';
import ROUTES from 'constants/routes';
import history from 'lib/history';
import AppLayout from 'modules/AppLayout';
import { RouteProvider } from 'modules/RouteProvider';
import React, { Suspense } from 'react';
import { Redirect, Route, Router, Switch } from 'react-router-dom';
import { Route, Router, Switch } from 'react-router-dom';
import routes from './routes';
@ -21,19 +19,6 @@ const App = (): JSX.Element => (
<Route key={index} exact={exact} path={path} component={component} />
);
})}
{/* This logic should be moved to app layout */}
<Route
path="/"
exact
render={(): JSX.Element => {
return localStorage.getItem(IS_LOGGED_IN) === 'yes' ? (
<Redirect to={ROUTES.APPLICATION} />
) : (
<Redirect to={ROUTES.SIGN_UP} />
);
}}
/>
<Route path="*" exact component={NotFound} />
</Switch>
</Suspense>

View File

@ -1,7 +1,12 @@
import { Layout } from 'antd';
import SideNav from 'components/SideNav';
import ROUTES from 'constants/routes';
import history from 'lib/history';
import React, { ReactNode, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { AppState } from 'store/reducers';
import AppReducer from 'types/reducer/app';
import TopNav from './Nav/TopNav';
import { useRoute } from './RouteProvider';
@ -16,11 +21,20 @@ const BaseLayout: React.FC<BaseLayoutProps> = ({ children }) => {
const location = useLocation();
const { dispatch } = useRoute();
const currentYear = new Date().getFullYear();
const { isLoggedIn } = useSelector<AppState, AppReducer>((state) => state.app);
useEffect(() => {
dispatch({ type: 'ROUTE_IS_LOADED', payload: location.pathname });
}, [location, dispatch]);
useEffect(() => {
if (isLoggedIn) {
history.push(ROUTES.APPLICATION);
} else {
history.push(ROUTES.SIGN_UP);
}
}, [isLoggedIn]);
return (
<Layout style={{ minHeight: '100vh' }}>
<SideNav />

View File

@ -1,8 +1,10 @@
import { IS_LOGGED_IN } from 'constants/auth';
import { AppAction, SWITCH_DARK_MODE } from 'types/actions/app';
import InitialValueTypes from 'types/reducer/app';
const InitialValue: InitialValueTypes = {
isDarkMode: true,
isLoggedIn: localStorage.getItem(IS_LOGGED_IN) === 'yes',
};
const appReducer = (

View File

@ -1,3 +1,4 @@
export default interface AppReducer {
isDarkMode: boolean;
isLoggedIn: boolean;
}