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

Commit f443686

Browse files
committed
Condensing clearIntents into field/model actions handled by forms action reducer to reduce action dispatching noise
1 parent 7a69d74 commit f443686

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

src/actions/field-actions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ function createFieldActions(s = defaultStrategies) {
6464
model,
6565
});
6666

67-
const setPending = (model, pending = true) => ({
67+
const setPending = (model, pending = true, options) => ({
6868
type: actionTypes.SET_PENDING,
6969
model,
7070
pending,
71+
...options,
7172
});
7273

7374
const setValidating = (model, validating = true) => ({
@@ -153,10 +154,11 @@ function createFieldActions(s = defaultStrategies) {
153154
submitted,
154155
});
155156

156-
const setSubmitFailed = (model, submitFailed = true) => ({
157+
const setSubmitFailed = (model, submitFailed = true, options) => ({
157158
type: actionTypes.SET_SUBMIT_FAILED,
158159
model,
159160
submitFailed,
161+
...options,
160162
});
161163

162164
const submit = (model, promise, options = {}) => {

src/actions/model-actions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ export function createModelActions(s = defaultStrategies) {
147147
return newValue;
148148
}, {}, 2, (_, props) => ({ removeKeys: props }));
149149

150-
const load = (model, values) => change(model, values, {
150+
const load = (model, values, options) => change(model, values, {
151151
silent: true,
152152
load: true,
153+
...options,
153154
});
154155

155156
return mapValues({

src/components/control-component.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ function getToggleValue(props) {
4848
}
4949
}
5050

51-
function mergeOrSetErrors(model, errors) {
51+
function mergeOrSetErrors(model, errors, options) {
5252
return actions.setErrors(model, errors, {
5353
merge: isPlainObject(errors),
54+
...options,
5455
});
5556
}
5657

@@ -399,15 +400,13 @@ function createControlClass(s = defaultStrategy) {
399400
}
400401
case 'validate':
401402
if (containsEvent(validateOn, 'change')) {
402-
dispatch(actions.clearIntents(model, intent));
403-
this.validate();
403+
this.validate({ clearIntents: intent });
404404
}
405405
return;
406406

407407
case 'load':
408408
if (!shallowEqual(modelValue, fieldValue.value)) {
409-
dispatch(actions.clearIntents(model, intent));
410-
dispatch(actions.load(model, fieldValue.value));
409+
dispatch(actions.load(model, fieldValue.value, { clearIntents: intent }));
411410
}
412411
return;
413412

@@ -563,7 +562,7 @@ function createControlClass(s = defaultStrategy) {
563562
if (node) this.node = node;
564563
}
565564

566-
validate() {
565+
validate(options) {
567566
const {
568567
model,
569568
modelValue,
@@ -584,7 +583,9 @@ function createControlClass(s = defaultStrategy) {
584583
: fieldErrors;
585584

586585
if (!shallowEqual(errors, fieldValue.errors)) {
587-
dispatch(mergeOrSetErrors(model, errors));
586+
dispatch(mergeOrSetErrors(model, errors, options));
587+
} else if (options.clearIntents) {
588+
dispatch(actions.clearIntents(options.clearIntents));
588589
}
589590

590591
return modelValue;

src/components/form-component.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,27 +268,27 @@ function createFormClass(s = defaultStrategy) {
268268
}
269269
}
270270

271-
handleValidSubmit() {
271+
handleValidSubmit(options) {
272272
const {
273273
dispatch,
274274
model,
275275
modelValue,
276276
onSubmit,
277277
} = this.props;
278278

279-
dispatch(s.actions.setPending(model));
279+
dispatch(s.actions.setPending(model, true, options));
280280

281281
if (onSubmit) onSubmit(modelValue);
282282
}
283283

284-
handleInvalidSubmit() {
284+
handleInvalidSubmit(options) {
285285
const { onSubmitFailed, formValue, dispatch } = this.props;
286286

287287
if (onSubmitFailed) {
288288
onSubmitFailed(formValue);
289289
}
290290

291-
dispatch(s.actions.setSubmitFailed(this.props.model));
291+
dispatch(s.actions.setSubmitFailed(this.props.model, true, options));
292292
}
293293

294294
handleReset(e) {
@@ -307,12 +307,10 @@ function createFormClass(s = defaultStrategy) {
307307
formValue.$form.intents.forEach((intent) => {
308308
switch (intent.type) {
309309
case 'submit': {
310-
dispatch(s.actions.clearIntents(model, intent));
311-
312310
if (isValid(formValue, { async: false })) {
313-
this.handleValidSubmit();
311+
this.handleValidSubmit({ clearIntents: intent });
314312
} else {
315-
this.handleInvalidSubmit();
313+
this.handleInvalidSubmit({ clearIntents: intent });
316314
}
317315

318316
return;

src/reducers/form-actions-reducer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ export function createFormActionsReducer(options) {
381381
return state;
382382
}
383383

384+
if (action.clearIntents) {
385+
fieldUpdates.intents = clearIntents(intents, action.clearIntents);
386+
}
387+
384388
const updatedField = updateField(state, localPath, fieldUpdates);
385389
const updatedSubFields = Object.keys(subFieldUpdates).length
386390
? updateSubFields(updatedField, localPath, subFieldUpdates)

0 commit comments

Comments
 (0)