Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Move logic to merge usesContext outside the reducer #63941

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading