Merge pull request #1666 from Discordius/devel

Added failure form callbacks and success form callbacks to forms
This commit is contained in:
Sacha Greif 2017-07-07 08:28:58 +09:00 committed by GitHub
commit a0feda1e0f

View file

@ -61,6 +61,8 @@ class Form extends Component {
this.addToAutofilledValues = this.addToAutofilledValues.bind(this);
this.addToDeletedValues = this.addToDeletedValues.bind(this);
this.addToSubmitForm = this.addToSubmitForm.bind(this);
this.addToSuccessForm = this.addToSuccessForm.bind(this);
this.addToFailureForm = this.addToFailureForm.bind(this);
this.throwError = this.throwError.bind(this);
this.clearForm = this.clearForm.bind(this);
this.updateCurrentValues = this.updateCurrentValues.bind(this);
@ -80,6 +82,8 @@ class Form extends Component {
};
this.submitFormCallbacks = [];
this.successFormCallbacks = [];
this.failureFormCallbacks = [];
}
// --------------------------------------------------------------------- //
@ -372,6 +376,16 @@ class Form extends Component {
this.submitFormCallbacks.push(callback);
}
// add a callback to form submission success
addToSuccessForm(callback) {
this.successFormCallbacks.push(callback);
}
// add a callback to form submission failure
addToFailureForm(callback) {
this.failureFormCallbacks.push(callback);
}
setFormState(fn) {
this.setState(fn);
}
@ -388,6 +402,8 @@ class Form extends Component {
getDocument: this.getDocument,
setFormState: this.setFormState,
addToSubmitForm: this.addToSubmitForm,
addToSuccessForm: this.addToSuccessForm,
addToFailureForm: this.addToFailureForm,
};
}
@ -421,6 +437,9 @@ class Form extends Component {
this.clearForm({clearErrors: true, clearCurrentValues});
}
// run document through mutation success callbacks
result = runCallbacks(this.successFormCallbacks, result);
// run success callback if it exists
if (this.props.successCallback) this.props.successCallback(document);
@ -434,6 +453,9 @@ class Form extends Component {
console.log("// graphQL Error"); // eslint-disable-line no-console
console.log(error); // eslint-disable-line no-console
// run mutation failure callbacks on error, we do not allow the callbacks to change the error
runCallbacks(this.failureFormCallbacks, error);
if (!_.isEmpty(error)) {
// add error to state
this.throwError(error.message);
@ -612,6 +634,8 @@ Form.childContextTypes = {
addToAutofilledValues: PropTypes.func,
addToDeletedValues: PropTypes.func,
addToSubmitForm: PropTypes.func,
addToFailureForm: PropTypes.func,
addToSuccessForm: PropTypes.func,
updateCurrentValues: PropTypes.func,
setFormState: PropTypes.func,
throwError: PropTypes.func,