From 0452b816a4e91002f5374533b934afbddb5a9bfe Mon Sep 17 00:00:00 2001 From: Kai Moseley Date: Mon, 5 Dec 2016 21:44:55 +0000 Subject: [PATCH] Amended resulting reducer to be an object for spreading --- src/index.js | 4 ++-- src/redux-arg/buildReducers.js | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index be6a4df..92b65e2 100644 --- a/src/index.js +++ b/src/index.js @@ -65,8 +65,8 @@ const test2 = buildReducers('invoices', exampleReducer2); const store = createStore( combineReducers({ - example: test.reducers, - invoices: test2.reducers, + ...test.reducers, + ...test2.reducers, }), compose(window.devToolsExtension ? window.devToolsExtension() : f => f) ); diff --git a/src/redux-arg/buildReducers.js b/src/redux-arg/buildReducers.js index cd5bc3b..7cf1c51 100644 --- a/src/redux-arg/buildReducers.js +++ b/src/redux-arg/buildReducers.js @@ -18,6 +18,25 @@ export function buildReducers(name: string, structure: any, { baseSelector: any, locationString: string, } = {}): PartialReducer { + const temp = reducerBuilder(name, structure, { + baseSelector, + locationString + }); + return { + ...temp, + reducers: { + [name]: temp.reducers, + } + } +} + +function reducerBuilder(name: string, structure: any, { + baseSelector = state => state[name], + locationString = '', +}: { + baseSelector: any, + locationString: string, +} = {}): PartialReducer { if (structure === undefined) throw new Error(`The structure must be defined for a reducer! LocationString: ${ locationString }`); //Build up the reducers, actions, and selectors for this level. Due to recursion, @@ -43,11 +62,11 @@ export function buildReducers(name: string, structure: any, { const containsReducers = !!find(propStructure, v => v().type === PROP_TYPES._reducer); //Create the child reducer. Depending on whether or not the current structure level contains - //child reducers, we will either recursively call buildReducers, or we will call the + //child reducers, we will either recursively call reducerBuilder, or we will call the //createReducer function, which will create the correct reducer for the given structure //(which can be either object, array, or primitive). let childReducer = containsReducers - ? buildReducers(propName, propStructure, { + ? reducerBuilder(propName, propStructure, { locationString: locationString ? `${locationString}.${propName}` : propName, baseSelector: (state: any) => baseSelector(state)[propName], })