Skip to content

Commit

Permalink
Support function plugins: reduxMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Oct 16, 2017
1 parent 65ff192 commit e3f9481
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Griddle extends Component {
settingsComponentObjects,
selectors,
styleConfig: defaultStyleConfig,
reduxMiddleware: [],

pageProperties: {
currentPage: 1,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/__tests__/initilizerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ test('init returns flattened/compacted reduxMiddleware given plugins', (assert)
props: {
plugins: [
{},
{ reduxMiddleware: [mw[0]] },
{},
() => ({ reduxMiddleware: [mw[0]] }),
() => ({ reduxMiddleware: null }),
{ reduxMiddleware: [null, mw[1], undefined, mw[2], null] },
{},
() => null,
],
reduxMiddleware: [null, mw[3], undefined],
},
Expand Down
23 changes: 18 additions & 5 deletions src/utils/initializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { buildGriddleReducer, buildGriddleComponents } from './compositionUtils'
import { getColumnProperties } from './columnUtils';
import { getRowProperties } from './rowUtils';

function pluginReducer(acc, plugin) {
const realPlugin = typeof plugin === 'function' ? plugin(acc) : plugin;
if (!realPlugin) return acc;

const {
reduxMiddleware = [],
} = realPlugin;

return {
...acc,
reduxMiddleware: _.flatten([acc.reduxMiddleware, reduxMiddleware]),
};
}

module.exports = function initializer(defaults) {
if (!this) throw new Error('this missing!');

Expand All @@ -24,13 +38,15 @@ module.exports = function initializer(defaults) {
components: userComponents,
renderProperties: userRenderProperties = {},
settingsComponentObjects: userSettingsComponentObjects,
reduxMiddleware = [],
...userInitialState
} = this.props;

const rowProperties = getRowProperties(rowPropertiesComponent);
const columnProperties = getColumnProperties(rowPropertiesComponent);

const withPlugins = plugins.reduce(pluginReducer, { ...defaults });
const withPluginsAndProps = pluginReducer(withPlugins, this.props);

// Combine / compose the reducers to make a single, unified reducer
const reducers = buildGriddleReducer([dataReducers, ...plugins.map(p => p.reducer)]);

Expand Down Expand Up @@ -79,9 +95,6 @@ module.exports = function initializer(defaults) {
return {
initialState,
reducers,
reduxMiddleware: _.compact([
..._.flatten(plugins.map(p => p.reduxMiddleware)),
...reduxMiddleware
]),
reduxMiddleware: _.compact(withPluginsAndProps.reduxMiddleware),
};
};

0 comments on commit e3f9481

Please sign in to comment.