Skip to content

Commit

Permalink
Move logic outside reducer (#63941)
Browse files Browse the repository at this point in the history
Move bindings logic to merge usesContext outside reducer

Co-authored-by: SantosGuillamot <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2024
1 parent c460ed3 commit 2fcba1b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,24 +371,32 @@ export function collections( state = {}, action ) {
return state;
}

export function blockBindingsSources( state = {}, action ) {
// Merge usesContext with existing values, potentially defined in the server registration.
const existingUsesContext = state[ action.name ]?.usesContext || [];
const newUsesContext = action.usesContext || [];
/**
* Merges usesContext with existing values, potentially defined in the server registration.
*
* @param {string[]} existingUsesContext Existing `usesContext`.
* @param {string[]} newUsesContext Newly added `usesContext`.
* @return {string[]|undefined} Merged `usesContext`.
*/
function getMergedUsesContext( existingUsesContext = [], newUsesContext = [] ) {
const mergedArrays = Array.from(
new Set( existingUsesContext.concat( newUsesContext ) )
);
const mergedUsesContext =
mergedArrays.length > 0 ? mergedArrays : undefined;
return mergedArrays.length > 0 ? mergedArrays : undefined;
}

export function blockBindingsSources( state = {}, action ) {
switch ( action.type ) {
case 'ADD_BLOCK_BINDINGS_SOURCE':
return {
...state,
[ action.name ]: {
// Don't override the label if it's already set.
label: state[ action.name ]?.label || action.label,
usesContext: mergedUsesContext,
usesContext: getMergedUsesContext(
state[ action.name ]?.usesContext,
action.usesContext
),
getValues: action.getValues,
setValues: action.setValues,
getPlaceholder: action.getPlaceholder,
Expand All @@ -405,7 +413,10 @@ export function blockBindingsSources( state = {}, action ) {
*/
...state[ action.name ],
label: action.label,
usesContext: mergedUsesContext,
usesContext: getMergedUsesContext(
state[ action.name ]?.usesContext,
action.usesContext
),
},
};
case 'REMOVE_BLOCK_BINDINGS_SOURCE':
Expand Down

0 comments on commit 2fcba1b

Please sign in to comment.