mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 14:14:10 +00:00
Simplify plugins react router
This commit is contained in:
parent
369dad92c9
commit
b4b2e5f139
41
packages/strapi-helper-plugin/lib/app/routes.js
Normal file
41
packages/strapi-helper-plugin/lib/app/routes.js
Normal file
@ -0,0 +1,41 @@
|
||||
// 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
|
||||
import { map } from 'lodash';
|
||||
import { getAsyncInjectors } from 'utils/asyncInjectors';
|
||||
import appSagas from 'containers/App/sagas';
|
||||
import routes from 'routes.json';
|
||||
|
||||
// Try to require a node module without throwing an error
|
||||
const tryRequire = (path) => {
|
||||
try {
|
||||
return require('containers/' + path + '.js');
|
||||
} catch (err) {}
|
||||
};
|
||||
|
||||
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
|
||||
injectSagas(appSagas);
|
||||
|
||||
return map(routes, (route, key) => {
|
||||
return {
|
||||
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
|
||||
|
||||
process.nextTick(() => {
|
||||
if (reducer) injectReducer(route.name, reducer.default);
|
||||
if (sagas) injectSagas(sagas.default);
|
||||
cb(null, component.default);
|
||||
});
|
||||
},
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
// 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
|
||||
import { getAsyncInjectors } from 'utils/asyncInjectors';
|
||||
import appSagas from 'containers/App/sagas';
|
||||
|
||||
const loadModule = cb => componentModule => {
|
||||
cb(null, componentModule.default);
|
||||
};
|
||||
|
||||
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
|
||||
injectSagas(appSagas);
|
||||
|
||||
return [
|
||||
{
|
||||
path: '',
|
||||
name: 'home',
|
||||
getComponent(nextState, cb) {
|
||||
const component = require('./containers/Home'); // eslint-disable-line global-require
|
||||
|
||||
const renderRoute = loadModule(cb);
|
||||
|
||||
process.nextTick(() => {
|
||||
renderRoute(component);
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/:slug',
|
||||
name: 'list',
|
||||
getComponent(nextState, cb) {
|
||||
const reducer = require('./containers/List/reducer'); // eslint-disable-line global-require
|
||||
const sagas = require('./containers/List/sagas'); // eslint-disable-line global-require
|
||||
const component = require('./containers/List'); // eslint-disable-line global-require
|
||||
|
||||
const renderRoute = loadModule(cb);
|
||||
|
||||
process.nextTick(() => {
|
||||
injectReducer('list', reducer.default);
|
||||
injectSagas(sagas.default);
|
||||
renderRoute(component);
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/:slug/:id',
|
||||
name: 'list',
|
||||
getComponent(nextState, cb) {
|
||||
const reducer = require('./containers/Edit/reducer'); // eslint-disable-line global-require
|
||||
const sagas = require('./containers/Edit/sagas'); // eslint-disable-line global-require
|
||||
const component = require('./containers/Edit'); // eslint-disable-line global-require
|
||||
|
||||
const renderRoute = loadModule(cb);
|
||||
|
||||
process.nextTick(() => {
|
||||
injectReducer('edit', reducer.default);
|
||||
injectSagas(sagas.default);
|
||||
renderRoute(component);
|
||||
});
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
{
|
||||
"/": {
|
||||
"name": "home",
|
||||
"container": "Home"
|
||||
},
|
||||
"/:slug": {
|
||||
"name": "list",
|
||||
"container": "List"
|
||||
},
|
||||
"/:slug/:id": {
|
||||
"name": "edit",
|
||||
"container": "Edit"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user