mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-27 19:42:11 +10:00
Refactor Routes to work with the new react-router API
This commit is contained in:
parent
9a91380896
commit
f17cccfdfd
1 changed files with 43 additions and 15 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue