Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Replace pickBy usage in validation reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Apr 21, 2023
1 parent e1055c1 commit 549826b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions assets/js/data/validation/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import type { Reducer } from 'redux';
import { pickBy } from 'lodash';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { isString, FieldValidationStatus } from '@woocommerce/types';

Expand All @@ -19,16 +18,24 @@ const reducer: Reducer< Record< string, FieldValidationStatus > > = (
const newState = { ...state };
switch ( action.type ) {
case types.SET_VALIDATION_ERRORS:
const newErrors = pickBy( action.errors, ( error, property ) => {
if ( typeof error?.message !== 'string' ) {
return false;
}
if ( state.hasOwnProperty( property ) ) {
return ! isShallowEqual( state[ property ], error );
if ( ! action.errors ) {
return state;
}
const hasNewError = Object.entries( action.errors ).some(
( [ property, error ] ) => {
if ( typeof error?.message !== 'string' ) {
return false;
}
if (
state.hasOwnProperty( property ) &&
isShallowEqual( state[ property ], error )
) {
return false;
}
return true;
}
return true;
} );
if ( Object.values( newErrors ).length === 0 ) {
);
if ( ! hasNewError ) {
return state;
}
return { ...state, ...action.errors };
Expand Down

0 comments on commit 549826b

Please sign in to comment.