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

Commit 1fc15a4

Browse files
committed
Performance enhancement - batching actions in submit() action creator
1 parent 4ab65b0 commit 1fc15a4

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/actions/field-actions.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _get from 'lodash/get';
22
import mapValues from 'lodash/mapValues';
33

44
import actionTypes from '../action-types';
5+
import batchActions from './batch-actions';
56
import { getValidity, getForm, isValid, isInvalid } from '../utils';
67

78
const focus = model => ({
@@ -114,18 +115,34 @@ const setViewValue = (model, value) => ({
114115
value,
115116
});
116117

117-
const submit = (model, promise) => dispatch => {
118+
const submit = (model, promise, options = {}) => dispatch => {
118119
dispatch(setPending(model, true));
119120

121+
const errorsAction = options.fields
122+
? setFieldsErrors
123+
: setErrors;
124+
120125
promise.then(response => {
121-
dispatch(setSubmitted(model, true));
122-
dispatch(setValidity(model, response));
126+
dispatch(batchActions.batch(model, [
127+
setSubmitted(model, true),
128+
setValidity(model, response),
129+
]));
123130
}).catch(error => {
124-
dispatch(setSubmitFailed(model));
125-
dispatch(setValidity(model, error, { errors: true }));
131+
dispatch(batchActions.batch(model, [
132+
setSubmitFailed(model),
133+
errorsAction(model, error, { errors: true }),
134+
]));
126135
});
136+
137+
return promise;
127138
};
128139

140+
const submitFields = (model, promise, options = {}) =>
141+
submit(model, promise, {
142+
...options,
143+
fields: true,
144+
});
145+
129146
const validate = (model, validators) => (dispatch, getState) => {
130147
const value = _get(getState(), model);
131148
const validity = getValidity(validators, value);
@@ -188,6 +205,7 @@ export default {
188205
blur,
189206
focus,
190207
submit,
208+
submitFields,
191209
setDirty,
192210
setErrors,
193211
setInitial,

test/field-actions-spec.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,14 @@ describe('field actions', () => {
11531153
it('should be able to resolve a form as valid', done => {
11541154
const expectedActions = [
11551155
{ type: actionTypes.SET_PENDING, pending: true, model: 'test' },
1156-
{ type: actionTypes.SET_SUBMITTED, submitted: true, model: 'test' },
1157-
{ type: actionTypes.SET_VALIDITY, validity: true, model: 'test' },
1156+
{
1157+
type: actionTypes.BATCH,
1158+
model: 'test',
1159+
actions: [
1160+
{ model: 'test', submitted: true, type: actionTypes.SET_SUBMITTED },
1161+
{ model: 'test', type: actionTypes.SET_VALIDITY, validity: true },
1162+
],
1163+
},
11581164
];
11591165

11601166
const store = mockStore(() => ({}), expectedActions, done);
@@ -1170,8 +1176,14 @@ describe('field actions', () => {
11701176

11711177
const expectedActions = [
11721178
{ type: actionTypes.SET_PENDING, pending: true, model: 'test' },
1173-
{ type: actionTypes.SET_SUBMIT_FAILED, model: 'test' },
1174-
{ type: actionTypes.SET_ERRORS, errors, model: 'test' },
1179+
{
1180+
type: actionTypes.BATCH,
1181+
model: 'test',
1182+
actions: [
1183+
{ type: actionTypes.SET_SUBMIT_FAILED, model: 'test' },
1184+
{ type: actionTypes.SET_ERRORS, errors, model: 'test' },
1185+
],
1186+
},
11751187
];
11761188

11771189
const store = mockStore(() => ({}), expectedActions, done);

0 commit comments

Comments
 (0)