Guest User

Untitled

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { object } from 'prop-types';
  3. import { connect } from 'react-redux';
  4.  
  5. import { getFormSubmitErrors, getFormSyncErrors, getFormMeta } from 'redux-form';
  6.  
  7. export default formName => WrappedComponent =>
  8. connect(state => ({
  9. submitErrors: getFormSubmitErrors(formName)(state),
  10. syncErrors: getFormSyncErrors(formName)(state),
  11. meta: getFormMeta(formName)(state),
  12. }))(
  13. class WithValidationErrors extends Component {
  14. static propTypes = {
  15. meta: object,
  16. submitErrors: object,
  17. syncErrors: object,
  18. };
  19.  
  20. static defaultProps = {
  21. meta: {},
  22. submitErrors: {},
  23. syncErrors: {},
  24. };
  25.  
  26. combineErrors({ submitErrors, meta, syncErrors }) {
  27.  
  28. const errorsToShow = {};
  29. const errorKeys = [...Object.keys(submitErrors), ...Object.keys(syncErrors)];
  30.  
  31. errorKeys
  32. .filter((value, index, arr) => arr.indexOf(value) === index)
  33. .forEach(key => {
  34. if (!(meta[key] && meta[key].touched && !meta[key].active)) return;
  35.  
  36. if (key in syncErrors) errorsToShow[key] = syncErrors[key];
  37. if (key in submitErrors) {
  38. if (key in errorsToShow) {
  39. errorsToShow[key] += `; ${submitErrors[key]}`;
  40. } else {
  41. errorsToShow[key] = submitErrors[key];
  42. }
  43. }
  44. });
  45.  
  46. return errorsToShow;
  47. }
  48.  
  49. render() {
  50. const errorsToShow = this.combineErrors(...this.props);
  51.  
  52. return <WrappedComponent {...this.props} errorsToShow={errorsToShow} />;
  53. }
  54. }
  55. );
Add Comment
Please, Sign In to add comment