From f17cccfdfd8cc2ea3d1ac8c5c3c8dce4653c5db8 Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Tue, 14 Feb 2017 14:28:44 +0200 Subject: [PATCH] Refactor Routes to work with the new react-router API --- src/Routes.js | 58 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/src/Routes.js b/src/Routes.js index f84b2b2e..fe3aa73c 100644 --- a/src/Routes.js +++ b/src/Routes.js @@ -1,25 +1,53 @@ import React, { PropTypes } from 'react'; -// import { Miss } from 'react-router-dom'; -import { RouterProvider, RoutesProvider } from './components'; -// import { NotFoundPage, MatchWithSubRoutes } from './containers'; -import { flattenRoutes } from './routesConfiguration'; +import { connect } from 'react-redux'; +import { Switch, Route } from 'react-router-dom'; +import { NotFoundPage } from './containers'; +import { NamedRedirect } from './components'; const Routes = props => { - const flattenedRoutes = flattenRoutes(props.routes); - // const matches = flattenedRoutes.map(route => ); + const { isAuthenticated, routes } = props; + + const renderComponent = (route, match) => { + const { auth, component: Component } = route; + const canShowComponent = !auth || isAuthenticated; + return canShowComponent + ? + : ; + }; + + const toRouteComponent = route => ( + renderComponent(route, matchProps.match)} + /> + ); return ( - -
- {/*matches*/} - {/**/} -
-
+ + {routes.map(toRouteComponent)} + + ); }; -const { any, array } = PropTypes; +const { bool, arrayOf, shape, string, func } = PropTypes; -Routes.propTypes = { routes: array.isRequired }; +Routes.propTypes = { + isAuthenticated: bool.isRequired, + routes: arrayOf( + shape({ + name: string.isRequired, + pattern: string.isRequired, + exactly: bool, + strict: bool, + component: func.isRequired, + loadData: func, + }), + ).isRequired, +}; -export default Routes; +const mapStateToProps = state => ({ isAuthenticated: state.Auth.isAuthenticated }); + +export default connect(mapStateToProps)(Routes);