|
@@ -1,63 +1,51 @@
|
|
|
import React, { Suspense, lazy } from "react";
|
|
|
-import { Switch, Route } from "react-router-dom";
|
|
|
+import { Switch, Route, withRouter } from "react-router-dom";
|
|
|
import { connect } from 'react-redux';
|
|
|
+import { tokenAuth } from './actions/auth/tokenAuth'
|
|
|
|
|
|
-import * as routes from './constants/routes';
|
|
|
-
|
|
|
-import Header from "./containers/header/index";
|
|
|
-import landingPage from './components/public-components/landingPage';
|
|
|
+import Header from "./containers/header";
|
|
|
import Footer from './components/public-components/footer';
|
|
|
-import PrivateRouter from './components/common/privateRouter';
|
|
|
-import PrivateAdminRouter from './components/common/privateAdminRouter';
|
|
|
|
|
|
import Spinner from "./components/common/spinner";
|
|
|
|
|
|
-const notFound = lazy(() => import("./components/public-components/notFound"));
|
|
|
-// const permissionDenied = lazy(() => import("./components/public-components/permissionDenied"));
|
|
|
-
|
|
|
-const homePage = lazy(() => import("./components/user-components/homePage"));
|
|
|
-const signInPage = lazy(() => import("./containers/auth/SignInPage"));
|
|
|
-const signUpPage = lazy(() => import("./containers/auth/SignUpPage"));
|
|
|
-const createTestForm = lazy(() => import("./components/user-components/admin-components/createTestForm"));
|
|
|
-const profilePage = lazy(() => import('./components/user-components/profilePage'));
|
|
|
-const categoriesPage = lazy(() => import('./components/user-components/categoriesPage'));
|
|
|
+import ProtectedRoute from './components/common/protectedRoute';
|
|
|
+import config from './components/common/protectedRoute/config';
|
|
|
|
|
|
-class Router extends React.Component {
|
|
|
+import fakeToken from './utils/token'
|
|
|
+import { bindActionCreators } from "redux";
|
|
|
|
|
|
- checkUserInLocalStorage = () => {
|
|
|
- // localStorage.getItem(token)
|
|
|
- }
|
|
|
+const notFound = lazy(() => import('./components/public-components/notFound'));
|
|
|
|
|
|
- //chunk.js warnings are disabled !
|
|
|
+class Router extends React.Component {
|
|
|
|
|
|
componentDidMount() {
|
|
|
+ const storagedUser = localStorage.getItem(fakeToken);
|
|
|
+ const { tokenAuth } = this.props;
|
|
|
+
|
|
|
+ storagedUser && tokenAuth(storagedUser);
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
const { user } = this.props;
|
|
|
|
|
|
+ //map located here because of switch-bag
|
|
|
+
|
|
|
+ const protectedRoutes = config.map(route =>
|
|
|
+ <ProtectedRoute
|
|
|
+ path={route.path}
|
|
|
+ component={route.component}
|
|
|
+ access={route.access}
|
|
|
+ user={user}
|
|
|
+ exact
|
|
|
+ />
|
|
|
+ );
|
|
|
+
|
|
|
return (
|
|
|
<div className="app">
|
|
|
<Header />
|
|
|
<Suspense fallback={<Spinner />}>
|
|
|
<Switch>
|
|
|
- <Route path={routes.LANDING} exact component={landingPage} />
|
|
|
- <Route path={routes.SIGN_UP} exact component={signUpPage} />
|
|
|
- <Route path={routes.SIGN_IN} exact component={signInPage} />
|
|
|
- <Route path={routes.HOME} exact component={homePage} />
|
|
|
-
|
|
|
- {/* should be protected */}
|
|
|
- <Route path={routes.CREATE_TEST} exact component={createTestForm} />
|
|
|
- <Route path={routes.PROFILE} user={user} exact component={profilePage} />
|
|
|
- <Route path={routes.CATEGORIES} user={user} exact component={categoriesPage} />
|
|
|
-
|
|
|
- <PrivateRouter path={routes.HOME} user={user} exact component={homePage} />
|
|
|
- <PrivateRouter path={routes.TESTS} user={user} exact component={null} />
|
|
|
- <PrivateRouter path={routes.CATEGORIES} user={user} exact component={null} />
|
|
|
-
|
|
|
- <PrivateAdminRouter path={routes.CREATE_CATEGORY} user={user} exact component={null} />
|
|
|
- <PrivateAdminRouter path={routes.DELETE_USER} user={user} exact component={null} />
|
|
|
-
|
|
|
+ {protectedRoutes}
|
|
|
<Route component={notFound} />
|
|
|
</Switch>
|
|
|
</Suspense>
|
|
@@ -67,11 +55,9 @@ class Router extends React.Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// const
|
|
|
-// mapStateToProps = state => ({
|
|
|
-// user: state.user
|
|
|
-// })
|
|
|
-
|
|
|
-// export default connect()(Router)
|
|
|
+const mapStateToProps = state => ({
|
|
|
+ user: state.user
|
|
|
+})
|
|
|
+const mapDispatchToProps = dispatch => bindActionCreators({ tokenAuth }, dispatch);
|
|
|
|
|
|
-export default Router;
|
|
|
+export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Router));
|