|
@@ -1,43 +1,63 @@
|
|
import React, { Suspense, lazy } from "react";
|
|
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"
|
|
|
|
-import landingPage from './components/landingPage'
|
|
|
|
-import Footer from './components/footer'
|
|
|
|
-import PrivateRouter from './PrivateRouter'
|
|
|
|
|
|
+import Header from "./containers/header";
|
|
|
|
+import Footer from './components/public-components/footer';
|
|
|
|
|
|
import Spinner from "./components/common/spinner";
|
|
import Spinner from "./components/common/spinner";
|
|
|
|
|
|
-const notFound = lazy(() => import("./components/notFound"));
|
|
|
|
-const homePage = lazy(() => import("./components/homePage"));
|
|
|
|
-const signInPage = lazy(() => import("./containers/auth/SignInPage"));
|
|
|
|
-const signUpPage = lazy(() => import("./containers/auth/SignUpPage"));
|
|
|
|
-const createTestPage = lazy(() => import("./components/admin-components/createTestPage"));
|
|
|
|
-
|
|
|
|
-export default () => (
|
|
|
|
- <div className="app">
|
|
|
|
- <Header />
|
|
|
|
- <Suspense fallback={<Spinner />}>
|
|
|
|
- <Switch>
|
|
|
|
- <Route path={routes.LANDING} exact component={landingPage} />
|
|
|
|
- <Route path={routes.SIGN_IN} exact component={signInPage} />
|
|
|
|
- <Route path={routes.SIGN_UP} exact component={signUpPage} />
|
|
|
|
- <Route path={routes.HOME} exact component={homePage} />
|
|
|
|
-
|
|
|
|
- {/* <PrivateRouter path={routes.HOME} user={false} component={homePage}/> */}
|
|
|
|
- <PrivateRouter path={routes.TESTS} user={false} component={null}/>
|
|
|
|
- <PrivateRouter path={routes.CATEGORIES} user={false} component={null}/>
|
|
|
|
- <PrivateRouter path={routes.PROFILE} user={false} component={null} />
|
|
|
|
-
|
|
|
|
- <PrivateRouter path={routes.CREATE_TEST} user={true} component={createTestPage} />
|
|
|
|
- <PrivateRouter path={routes.CREATE_CATEGORY} user={false} component={createTestPage} />
|
|
|
|
- <PrivateRouter path={routes.DELETE_USER} user={false} component={createTestPage} />
|
|
|
|
-
|
|
|
|
- <Route component={notFound} />
|
|
|
|
- </Switch>
|
|
|
|
- </Suspense>
|
|
|
|
- <Footer />
|
|
|
|
- </div>
|
|
|
|
-);
|
|
|
|
|
|
+import ProtectedRoute from './components/common/protectedRoute';
|
|
|
|
+import config from './components/common/protectedRoute/config';
|
|
|
|
+
|
|
|
|
+import fakeToken from './utils/token'
|
|
|
|
+import { bindActionCreators } from "redux";
|
|
|
|
+
|
|
|
|
+const notFound = lazy(() => import('./components/public-components/notFound'));
|
|
|
|
+
|
|
|
|
+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>
|
|
|
|
+ {protectedRoutes}
|
|
|
|
+ <Route component={notFound} />
|
|
|
|
+ </Switch>
|
|
|
|
+ </Suspense>
|
|
|
|
+ <Footer />
|
|
|
|
+ </div>
|
|
|
|
+ )
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+const mapStateToProps = state => ({
|
|
|
|
+ user: state.user
|
|
|
|
+})
|
|
|
|
+const mapDispatchToProps = dispatch => bindActionCreators({ tokenAuth }, dispatch);
|
|
|
|
+
|
|
|
|
+export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Router));
|