From 8f533f36ab49dc4dff97a391a3816d051c4460e7 Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Thu, 2 Feb 2017 13:26:59 +0200 Subject: [PATCH] Extract MatchWithSubRoutes component --- src/Routes.js | 65 ++----------------- .../MatchWithSubRoutes/MatchWithSubRoutes.js | 45 +++++++++++++ src/containers/index.js | 2 + 3 files changed, 52 insertions(+), 60 deletions(-) create mode 100644 src/containers/MatchWithSubRoutes/MatchWithSubRoutes.js diff --git a/src/Routes.js b/src/Routes.js index bbf28319..bc65cf32 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -1,65 +1,8 @@ import React, { PropTypes } from 'react'; -import { Match, Miss, Redirect } from 'react-router'; +import { Miss } from 'react-router'; import { RouterProvider, RoutesProvider } from './components'; -import { NotFoundPage } from './containers'; -import routesConfiguration, { flattenRoutes, pathByRouteName } from './routesConfiguration'; - -// Fake authentication module -// An example from react-router v4 repository -// This will be replaced once we have redux store and sdk -export const fakeAuth = { - isAuthenticated: false, - authenticate(cb) { - // eslint-disable-next-line no-console - console.log('fakeAuth: authenticate'); - this.isAuthenticated = true; - window.setTimeout(cb, 100); // fake async - }, - signout(cb) { - // eslint-disable-next-line no-console - console.log('fakeAuth: signout'); - this.isAuthenticated = false; - cb(); - window.setTimeout(cb, 100); // weird bug if async? - }, -}; - -// wrap `Match` and use this everywhere instead, then when -// sub routes are added to any route it'll work -// This will also check if route needs authentication. -/* eslint-disable arrow-body-style */ -const MatchWithSubRoutes = props => { - const { auth, component: Component, ...rest } = props; - const isAuthenticated = auth && fakeAuth.isAuthenticated; - const canShowComponent = !auth || isAuthenticated; - return ( - { - return canShowComponent - ? - : ; - }} - /> - ); -}; -/* eslint-enable arrow-body-style */ -MatchWithSubRoutes.defaultProps = { auth: false, exactly: false }; - -const { any, array, bool, func, node, oneOfType, string } = PropTypes; - -MatchWithSubRoutes.propTypes = { - pattern: string.isRequired, - auth: bool, - exactly: bool, - name: string.isRequired, - component: oneOfType([func, node]).isRequired, -}; +import { NotFoundPage, MatchWithSubRoutes } from './containers'; +import { flattenRoutes } from './routesConfiguration'; const Routes = props => { const flattenedRoutes = flattenRoutes(props.routes); @@ -77,6 +20,8 @@ const Routes = props => { ); }; +const { any, array } = PropTypes; + Routes.propTypes = { router: any.isRequired, routes: array.isRequired }; export default Routes; diff --git a/src/containers/MatchWithSubRoutes/MatchWithSubRoutes.js b/src/containers/MatchWithSubRoutes/MatchWithSubRoutes.js new file mode 100644 index 00000000..1d971115 --- /dev/null +++ b/src/containers/MatchWithSubRoutes/MatchWithSubRoutes.js @@ -0,0 +1,45 @@ +import React, { PropTypes } from 'react'; +import { connect } from 'react-redux'; +import { Match, Redirect } from 'react-router'; +import routesConfiguration, { pathByRouteName } from '../../routesConfiguration'; + +// wrap `Match` and use this everywhere instead, then when +// sub routes are added to any route it'll work +// This will also check if route needs authentication. +/* eslint-disable arrow-body-style */ +const MatchWithSubRoutes = props => { + const { auth, component: Component, isAuthenticated, ...rest } = props; + const canShowComponent = !auth || isAuthenticated; + return ( + { + return canShowComponent + ? + : ; + }} + /> + ); +}; +/* eslint-enable arrow-body-style */ +MatchWithSubRoutes.defaultProps = { auth: false, exactly: false }; + +const { bool, func, node, oneOfType, string } = PropTypes; + +MatchWithSubRoutes.propTypes = { + pattern: string.isRequired, + auth: bool, + exactly: bool, + name: string.isRequired, + component: oneOfType([func, node]).isRequired, + isAuthenticated: bool.isRequired, +}; + +const mapStateToProps = state => ({ isAuthenticated: state.Auth.isAuthenticated }); + +export default connect(mapStateToProps)(MatchWithSubRoutes); diff --git a/src/containers/index.js b/src/containers/index.js index 0942c704..937cfc39 100644 --- a/src/containers/index.js +++ b/src/containers/index.js @@ -10,6 +10,7 @@ import LandingPage from './LandingPage/LandingPage'; import ListingPage from './ListingPage/ListingPage'; import LoginForm from './LoginForm/LoginForm'; import ManageListingsPage from './ManageListingsPage/ManageListingsPage'; +import MatchWithSubRoutes from './MatchWithSubRoutes/MatchWithSubRoutes'; import NotFoundPage from './NotFoundPage/NotFoundPage'; import OrderPage from './OrderPage/OrderPage'; import PasswordChangePage from './PasswordChangePage/PasswordChangePage'; @@ -37,6 +38,7 @@ export { ListingPage, LoginForm, ManageListingsPage, + MatchWithSubRoutes, NotFoundPage, OrderPage, PasswordChangePage,