42 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-05-17 15:58:19 +02:00
// These are the pages you can go to.
// They are all wrapped in the App component, which should contain the navbar etc
// See http://blog.mxstbr.com/2016/01/react-apps-with-pages for more information
// about the code splitting business
2017-08-14 14:16:15 +02:00
import { camelCase, map } from 'lodash';
2017-05-17 15:58:19 +02:00
import { getAsyncInjectors } from 'utils/asyncInjectors';
import routes from 'routes.json'; // eslint-disable-line
2017-05-17 15:58:19 +02:00
// Try to require a node module without throwing an error
const tryRequire = (path) => {
try {
return require(`containers/${path}.js`); // eslint-disable-line global-require
} catch (err) {
return null;
}
2017-05-17 15:58:19 +02:00
};
export default function createRoutes(store) {
// Create reusable async injectors using getAsyncInjectors factory
const { injectReducer, injectSagas } = getAsyncInjectors(store); // eslint-disable-line no-unused-vars
// Inject app sagas
2017-05-17 16:00:43 +02:00
const appSagas = tryRequire('App/sagas');
if (appSagas) injectSagas(appSagas.default);
2017-05-17 15:58:19 +02:00
return map(routes, (route, key) => ({
path: key === '/' ? '' : key,
name: route.name,
getComponent(nextState, cb) {
const reducer = tryRequire(`${route.container}/reducer`); // eslint-disable-line global-require
const sagas = tryRequire(`${route.container}/sagas`); // eslint-disable-line global-require
const component = tryRequire(`${route.container}/index`); // eslint-disable-line global-require
2017-05-17 15:58:19 +02:00
process.nextTick(() => {
2017-08-01 00:16:00 +02:00
if (reducer) injectReducer(camelCase(route.container), reducer.default);
if (sagas) injectSagas(sagas.default);
cb(null, component.default);
});
},
}));
2017-05-17 15:58:19 +02:00
}