Skip to content

Instantly share code, notes, and snippets.

@farism
Created October 9, 2016 18:56
Show Gist options
  • Save farism/3bb62482f72b617b13b7940706de98ed to your computer and use it in GitHub Desktop.
Save farism/3bb62482f72b617b13b7940706de98ed to your computer and use it in GitHub Desktop.
import { combineReducers, createStore } from 'redux';
import { local } from 'redux-fractal';
import R from 'ramda';
const combineModules = modules => {
return modules.reduce((acc, module) => ({
reducers: { ...acc.reducers, [module.name]: module.reducer },
actions: { ...acc.actions, ...module.actions },
}), { actions: {}, reducers: {} });
};
export default R.curry(config => {
if (!config.modules || config.modules.length === 0) {
return local(config);
}
const { actions, reducers } = combineModules(config.modules);
return local({
createStore: () => createStore(combineReducers(reducers)),
mapDispatchToProps: dispatch => {
return R.map(fn => payload => dispatch(fn(payload)), actions);
},
...config,
});
});
// usage
localWithModules({
key: (props) => "keyname"
modules: [ModuleA, ModuleB],
})(Component)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment