Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 388f325

Browse files
committed
Slightly refactoring model reducer and form reducer
1 parent d058940 commit 388f325

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

lib/reducers/form-reducer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,20 @@ function createInitialFormState(model) {
9999
}
100100

101101
function createFormReducer(model) {
102+
var modelPath = (0, _toPath2.default)(model);
103+
102104
return function () {
103105
var state = arguments.length <= 0 || arguments[0] === undefined ? createInitialFormState(model) : arguments[0];
104106
var action = arguments[1];
105107

108+
if (!action.model) return state;
109+
106110
var path = (0, _toPath2.default)(action.model);
107-
var modelPath = (0, _toPath2.default)(model);
111+
108112
if (!(0, _isEqual2.default)(path.slice(0, modelPath.length), modelPath)) {
109113
return state;
110114
}
115+
111116
var localPath = path.slice(modelPath.length);
112117

113118
var form = (0, _cloneDeep2.default)(state);

lib/reducers/model-reducer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ var _toPath = require('lodash/toPath');
2525

2626
var _toPath2 = _interopRequireDefault(_toPath);
2727

28+
var _isEqual = require('lodash/isEqual');
29+
30+
var _isEqual2 = _interopRequireDefault(_isEqual);
31+
2832
var _actionTypes = require('../action-types');
2933

3034
var actionTypes = _interopRequireWildcard(_actionTypes);
@@ -46,7 +50,7 @@ function createModelReducer(model) {
4650

4751
var path = (0, _toPath2.default)(action.model);
4852

49-
if (path[0] !== modelPath[0]) {
53+
if (!(0, _isEqual2.default)(path.slice(0, modelPath.length), modelPath)) {
5054
return state;
5155
}
5256

src/reducers/form-reducer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ function createInitialFormState(model) {
6464
}
6565

6666
function createFormReducer(model) {
67+
const modelPath = toPath(model);
68+
6769
return (state = createInitialFormState(model), action) => {
70+
if (!action.model) return state;
71+
6872
let path = toPath(action.model);
69-
let modelPath = toPath(model);
73+
7074
if (!isEqual(path.slice(0, modelPath.length), modelPath)) {
7175
return state;
7276
}
77+
7378
let localPath = path.slice(modelPath.length);
7479

7580
let form = cloneDeep(state);

src/reducers/model-reducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import set from 'lodash/set';
33
import startsWith from 'lodash/startsWith';
44
import cloneDeep from 'lodash/cloneDeep';
55
import toPath from 'lodash/toPath';
6+
import isEqual from 'lodash/isEqual';
67

78
import * as actionTypes from '../action-types';
89

@@ -14,7 +15,7 @@ function createModelReducer(model, initialState = {}) {
1415

1516
let path = toPath(action.model);
1617

17-
if (path[0] !== modelPath[0]) {
18+
if (!isEqual(path.slice(0, modelPath.length), modelPath)) {
1819
return state;
1920
}
2021

test/model-reducer-spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,15 @@ describe('createModelReducer()', () => {
8282

8383
it('should handle model at deep state path', () => {
8484
const reducer = createModelReducer('forms.test');
85+
8586
assert.deepEqual(
8687
reducer(undefined, actions.change('forms.test.foo', 'new')),
8788
{ foo: 'new' }
8889
);
8990

90-
})
91-
91+
assert.deepEqual(
92+
reducer(undefined, actions.change('forms.different.foo', 'new')),
93+
{},
94+
'should only change when base path is equal');
95+
});
9296
});

0 commit comments

Comments
 (0)