Skip to content

Commit

Permalink
Create extractInvariantErrors function to avoid code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl committed Apr 30, 2024
1 parent 5cca767 commit 63a7238
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/volto/src/components/manage/Add/Add.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { BodyClass, Helmet } from '@plone/volto/helpers';
import {
BodyClass,
Helmet,
extractInvariantErrors,
} from '@plone/volto/helpers';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { keys, isEmpty } from 'lodash';
Expand Down Expand Up @@ -182,9 +186,7 @@ class Add extends Component {
const errorsList = tryParseJSON(error);
let erroMessage;
if (Array.isArray(errorsList)) {
const invariantErrors = errorsList
.filter((errorItem) => !('field' in errorItem))
.map((errorItem) => errorItem['message']);
const invariantErrors = extractInvariantErrors(errorsList);
if (invariantErrors.length > 0) {
// Plone invariant validation message.
erroMessage = invariantErrors.join(' - ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { withRouter } from 'react-router-dom';
import { Helmet, tryParseJSON } from '@plone/volto/helpers';
import {
Helmet,
tryParseJSON,
extractInvariantErrors,
} from '@plone/volto/helpers';
import { createPortal } from 'react-dom';
import { Button, Container } from 'semantic-ui-react';
import { defineMessages, injectIntl } from 'react-intl';
Expand Down Expand Up @@ -131,9 +135,7 @@ class Controlpanel extends Component {
const errorsList = tryParseJSON(error);
let invariantErrors = [];
if (Array.isArray(errorsList)) {
invariantErrors = errorsList
.filter((errorItem) => !('field' in errorItem))
.map((errorItem) => errorItem['message']);
invariantErrors = extractInvariantErrors(errorsList);
}

this.setState({ error: error });
Expand Down
6 changes: 2 additions & 4 deletions packages/volto/src/components/manage/Edit/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Helmet } from '@plone/volto/helpers';
import { Helmet, extractInvariantErrors } from '@plone/volto/helpers';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { asyncConnect, hasApiExpander } from '@plone/volto/helpers';
Expand Down Expand Up @@ -208,9 +208,7 @@ class Edit extends Component {
const errorsList = tryParseJSON(error);
let erroMessage;
if (Array.isArray(errorsList)) {
const invariantErrors = errorsList
.filter((errorItem) => !('field' in errorItem))
.map((errorItem) => errorItem['message']);
const invariantErrors = extractInvariantErrors(errorsList);
if (invariantErrors.length > 0) {
// Plone invariant validation message.
erroMessage = invariantErrors.join(' - ');
Expand Down
10 changes: 10 additions & 0 deletions packages/volto/src/helpers/FormValidation/FormValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,13 @@ export const validateFileUploadSize = (file, intlFunc) => {
}
return isValid;
};

/**
* Extract invariant errors given an array of errors.
* @param {Array} erros
*/
export const extractInvariantErrors = (erros) => {
return erros
.filter((errorItem) => !('field' in errorItem))
.map((errorItem) => errorItem['message']);
};
1 change: 1 addition & 0 deletions packages/volto/src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export { default as Helmet } from './Helmet/Helmet';
export { default as FormValidation } from './FormValidation/FormValidation';
export { validateFileUploadSize } from './FormValidation/FormValidation';
export { tryParseJSON } from './FormValidation/FormValidation';
export { extractInvariantErrors } from './FormValidation/FormValidation';
export {
difference,
getColor,
Expand Down

0 comments on commit 63a7238

Please sign in to comment.