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

Commit 50c6c84

Browse files
committed
Making sure submitFailed, submitted propagates to fields. Fixes #222
1 parent 51535da commit 50c6c84

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

src/actions/model-actions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const push = (model, item = null) => (dispatch, getState) => {
5757
});
5858
};
5959

60-
const toggle = model => (dispatch, getState) => {
60+
const toggle = (model) => (dispatch, getState) => {
6161
const value = !_get(getState(), model);
6262

6363
dispatch({
@@ -78,7 +78,7 @@ const filter = (model, iteratee = identity) => (dispatch, getState) => {
7878
});
7979
};
8080

81-
const reset = model => ({
81+
const reset = (model) => ({
8282
type: actionTypes.RESET,
8383
model,
8484
});

src/reducers/form-reducer.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,43 @@ function _createFormReducer(model, initialState) {
400400
untouched: true, // will be deprecated
401401
});
402402

403-
case actionTypes.SET_SUBMITTED:
404-
return setField(state, localPath, {
403+
case actionTypes.SET_SUBMITTED: {
404+
const submittedState = {
405405
pending: false,
406406
submitted: !!action.submitted,
407407
submitFailed: false,
408408
touched: true,
409409
untouched: false, // will be deprecated
410-
});
410+
};
411411

412-
case actionTypes.SET_SUBMIT_FAILED:
413-
return setField(state, localPath, {
412+
if (!localPath.length) {
413+
return icepick.merge(state, {
414+
...submittedState,
415+
fields: mapValues(state.fields, (field) => icepick.merge(field, submittedState)),
416+
});
417+
}
418+
419+
return setField(state, localPath, submittedState);
420+
}
421+
422+
case actionTypes.SET_SUBMIT_FAILED: {
423+
const submitFailedState = {
414424
pending: false,
415425
submitted: false,
416426
submitFailed: true,
417427
touched: true,
418-
untouched: false, // will be deprecated
419-
});
428+
untouched: false,
429+
};
430+
431+
if (!localPath.length) {
432+
return icepick.merge(state, {
433+
...submitFailedState,
434+
fields: mapValues(state.fields, (field) => icepick.merge(field, submitFailedState)),
435+
});
436+
}
437+
438+
return setField(state, localPath, submitFailedState);
439+
}
420440

421441
case actionTypes.SET_INITIAL:
422442
case actionTypes.RESET:

test/form-reducer-spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,54 @@ describe('formReducer()', () => {
259259
actions.remove('test.items', 0)(dispatch, getState);
260260
});
261261
});
262+
263+
describe('SET_SUBMIT_FAILED action', () => {
264+
it('should set all fields to submitFailed = true when form submitFailed = true', () => {
265+
const reducer = formReducer('test', { foo: '', bar: '' });
266+
267+
const actual = reducer(undefined, actions.setSubmitFailed('test'));
268+
269+
assert.containSubset(actual, {
270+
submitFailed: true,
271+
submitted: false,
272+
fields: {
273+
foo: {
274+
submitFailed: true,
275+
touched: true,
276+
submitted: false,
277+
},
278+
bar: {
279+
submitFailed: true,
280+
touched: true,
281+
submitted: false,
282+
},
283+
},
284+
});
285+
});
286+
287+
it('should set all fields to submitFailed = false when form submitFailed = false', () => {
288+
const reducer = formReducer('test', { foo: '', bar: '' });
289+
290+
const submitFailed = reducer(undefined, actions.setSubmitFailed('test', false));
291+
292+
const actual = reducer(submitFailed, actions.setSubmitted('test'));
293+
294+
assert.containSubset(actual, {
295+
submitFailed: false,
296+
submitted: true,
297+
fields: {
298+
foo: {
299+
submitFailed: false,
300+
touched: true,
301+
submitted: true,
302+
},
303+
bar: {
304+
submitFailed: false,
305+
touched: true,
306+
submitted: true,
307+
},
308+
},
309+
});
310+
});
311+
});
262312
});

0 commit comments

Comments
 (0)