Refactor Routes to work with the new react-router API

This commit is contained in:
Kimmo Puputti 2017-02-14 14:28:44 +02:00
parent 9a91380896
commit f17cccfdfd

View file

@ -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 => <MatchWithSubRoutes key={route.name} {...route} />);
const { isAuthenticated, routes } = props;
const renderComponent = (route, match) => {
const { auth, component: Component } = route;
const canShowComponent = !auth || isAuthenticated;
return canShowComponent
? <Component {...match} />
: <NamedRedirect name="LogInPage" state={{ from: match.location }} />;
};
const toRouteComponent = route => (
<Route
key={route.name}
path={route.pattern}
exact={route.exactly}
render={matchProps => renderComponent(route, matchProps.match)}
/>
);
return (
<RoutesProvider routes={props.routes}>
<div>
{/*matches*/}
{/*<Miss component={NotFoundPage} />*/}
</div>
</RoutesProvider>
<Switch>
{routes.map(toRouteComponent)}
<Route component={NotFoundPage} />
</Switch>
);
};
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);