Skip to content

Commit

Permalink
Lodash: Remove from blocks store reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed May 9, 2023
1 parent e4adc1e commit 472c532
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get, isEmpty } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -123,13 +118,11 @@ export function blockStyles( state = {}, action ) {
).map( ( [ name, blockType ] ) => [
name,
getUniqueItemsByName( [
...get( blockType, [ 'styles' ], [] ).map(
( style ) => ( {
...style,
source: 'block',
} )
),
...get( state, [ blockType.name ], [] ).filter(
...( blockType.styles ?? [] ).map( ( style ) => ( {
...style,
source: 'block',
} ) ),
...( state[ blockType.name ] ?? [] ).filter(
( { source } ) => 'block' !== source
),
] ),
Expand All @@ -140,17 +133,15 @@ export function blockStyles( state = {}, action ) {
return {
...state,
[ action.blockName ]: getUniqueItemsByName( [
...get( state, [ action.blockName ], [] ),
...( state[ action.blockName ] ?? [] ),
...action.styles,
] ),
};
case 'REMOVE_BLOCK_STYLES':
return {
...state,
[ action.blockName ]: get(
state,
[ action.blockName ],
[]
[ action.blockName ]: (
state[ action.blockName ] ?? []
).filter(
( style ) => action.styleNames.indexOf( style.name ) === -1
),
Expand Down Expand Up @@ -180,13 +171,13 @@ export function blockVariations( state = {}, action ) {
return [
name,
getUniqueItemsByName( [
...get( blockType, [ 'variations' ], [] ).map(
...( blockType.variations ?? [] ).map(
( variation ) => ( {
...variation,
source: 'block',
} )
),
...get( state, [ blockType.name ], [] ).filter(
...( state[ blockType.name ] ?? [] ).filter(
( { source } ) => 'block' !== source
),
] ),
Expand All @@ -198,17 +189,15 @@ export function blockVariations( state = {}, action ) {
return {
...state,
[ action.blockName ]: getUniqueItemsByName( [
...get( state, [ action.blockName ], [] ),
...( state[ action.blockName ] ?? [] ),
...action.variations,
] ),
};
case 'REMOVE_BLOCK_VARIATIONS':
return {
...state,
[ action.blockName ]: get(
state,
[ action.blockName ],
[]
[ action.blockName ]: (
state[ action.blockName ] ?? []
).filter(
( variation ) =>
action.variationNames.indexOf( variation.name ) === -1
Expand Down Expand Up @@ -269,7 +258,10 @@ export function categories( state = DEFAULT_CATEGORIES, action ) {
case 'SET_CATEGORIES':
return action.categories || [];
case 'UPDATE_CATEGORY': {
if ( ! action.category || isEmpty( action.category ) ) {
if (
! action.category ||
! Object.keys( action.category ).length
) {
return state;
}
const categoryToChange = state.find(
Expand Down

0 comments on commit 472c532

Please sign in to comment.