Doodle3D-Core/src/utils/reactUtils.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-11-02 16:59:56 +01:00
import { SubmissionError, startAsyncValidation, stopAsyncValidation } from 'redux-form';
2017-10-29 09:04:21 +01:00
// redux form submit promise wrapper
// - turns all errors into SubmissionError
// - fills _error field, enables showing main error message in form
// - includes superlogin's validationErrors
export function formPromiseWrapper(promise) {
return new Promise((resolve, reject) => {
promise
.then(resolve)
.catch(error => {
// axius (used by superlogin http utils) puts response data in response.data
if (error.response && error.response.data) {
error = error.response.data;
}
// promise middleware puts actual error in reason
if (error.action && error.reason) {
error = error.reason;
}
const submissionErrors = {
_error: error.message || error.error
};
// add superlogin validation errors
// joined because superlogin creates arrays per field
for (const prop in error.validationErrors) {
submissionErrors[prop] = error.validationErrors[prop].join(', ');
}
const submissionError = new SubmissionError(submissionErrors);
reject(submissionError);
});
});
}
export function classNames(...names) {
return names
.filter(name => typeof name === 'string')
.join(' ');
}
2017-11-02 16:59:56 +01:00
export function asyncValidateForm(dispatch, form, asyncValidate, formData) {
dispatch(startAsyncValidation(form));
asyncValidate(formData).then(() => {
dispatch(stopAsyncValidation(form));
}).catch(error => {
dispatch(stopAsyncValidation(form, error));
})
}