diff --git a/src/redux-arg/reducers.js b/src/redux-arg/reducers.js index b49ab83..69aa14c 100644 --- a/src/redux-arg/reducers.js +++ b/src/redux-arg/reducers.js @@ -9,6 +9,10 @@ import type { ObjectReducerBehaviorsConfig, ObjectReducerBehaviors, } from './reducers/objectReducer'; +import type { + StructureType, + PrimitiveType, +} from './structure'; //============================== // Flow types @@ -62,3 +66,12 @@ export function createReducerBehaviors(behaviorsConfig: ReducerBehaviorsConfig, [`${locationString}.${name}`]: behavior.reducer, }), {}); } + +export function calculateDefaults(typeDescription: StructureType | PrimitiveType) { + const { type, structure = {}} = typeDescription; + if ([PROP_TYPES.array, PROP_TYPES.shape].find(type)) + return reduce(reducerStructure, (memo, propValue, propName) => ({ + ...memo, + [propName]: propValue().defaultValue, + }), {}); +} diff --git a/src/redux-arg/reducers/arrayReducer.js b/src/redux-arg/reducers/arrayReducer.js index db770d2..7e55b7d 100644 --- a/src/redux-arg/reducers/arrayReducer.js +++ b/src/redux-arg/reducers/arrayReducer.js @@ -47,7 +47,7 @@ function checkIndex(state: Object, payload: any, behaviorName: string): boolean return true; } -const DEFAULT_OBJECT_BEHAVIORS: ArrayReducerBehaviorsConfig = { +const DEFAULT_ARRAY_BEHAVIORS: ArrayReducerBehaviorsConfig = { updateAtIndex: { action(value) { return value }, reducer(state, payload, initialState, index) { @@ -95,29 +95,24 @@ export function createArrayReducer(reducerShape: StructureType, { locationString }: ArrayReducerOptions = {}) { return { - reducers: createReducer(reducerShape, createReducerBehaviors(DEFAULT_OBJECT_BEHAVIORS, locationString)), - actionsObject: createActions(DEFAULT_OBJECT_BEHAVIORS, locationString, {}), + reducers: createReducer(reducerShape, createReducerBehaviors(DEFAULT_ARRAY_BEHAVIORS, locationString)), + actionsObject: createActions(DEFAULT_ARRAY_BEHAVIORS, locationString, {}), }; } -function calculateDefaults(reducerStructure) { - return reduce(reducerStructure, (memo, propValue, propName) => ({ - ...memo, - [propName]: propValue().defaultValue, - }), {}); -} -function createReducer(arrayStructure: StructureType, behaviors: ArrayReducerBehaviors): ArrayReducer { - const initialState = calculateDefaults(arrayStructure().structure); + +function createReducer(arrayTypeDescription: StructureType, behaviors: ArrayReducerBehaviors): ArrayReducer { + const initialState = calculateDefaults(arrayTypeDescription); return (state = initialState, { type, payload }: ArrayReducerAction) => { //If the action type does not match any of the specified behaviors, just return the current state. if (!behaviors[type]) return state; //Sanitize the payload using the reducer shape, then apply the sanitized //payload to the state using the behavior linked to this action type. - return behaviors[type](state, validateArray(arrayStructure, payload), initialState); + return behaviors[type](state, validateArray(arrayTypeDescription, payload), initialState); } }