Skip to content

Commit

Permalink
Only add the selected pattern category in metadata during insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed May 10, 2024
1 parent f4cf64a commit 40bbc46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function PatternList( {
} );
const [ patterns, , onClickPattern ] = usePatternsState(
onInsertBlocks,
destinationRootClientId
destinationRootClientId,
selectedCategory
);

const registeredPatternCategories = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export function PatternCategoryPreviews( {
} ) {
const [ allPatterns, , onClickPattern ] = usePatternsState(
onInsert,
rootClientId
rootClientId,
category?.name
);
const [ patternSyncFilter, setPatternSyncFilter ] = useState( 'all' );
const [ patternSourceFilter, setPatternSourceFilter ] = useState( 'all' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import { INSERTER_PATTERN_TYPES } from '../block-patterns-tab/utils';
/**
* Retrieves the block patterns inserter state.
*
* @param {Function} onInsert function called when inserter a list of blocks.
* @param {string=} rootClientId Insertion's root client ID.
* @param {Function} onInsert function called when inserter a list of blocks.
* @param {string=} rootClientId Insertion's root client ID.
*
* @param {string} selectedCategory The selected pattern category.
* @return {Array} Returns the patterns state. (patterns, categories, onSelect handler)
*/
const usePatternsState = ( onInsert, rootClientId ) => {
const usePatternsState = ( onInsert, rootClientId, selectedCategory ) => {
const { patternCategories, patterns, userPatternCategories } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getSettings } =
Expand Down Expand Up @@ -63,7 +64,18 @@ const usePatternsState = ( onInsert, rootClientId ) => {
? [ createBlock( 'core/block', { ref: pattern.id } ) ]
: blocks;
onInsert(
( patternBlocks ?? [] ).map( ( block ) => cloneBlock( block ) ),
( patternBlocks ?? [] ).map( ( block ) => {
const clonedBlock = cloneBlock( block );
if (
selectedCategory &&
clonedBlock.attributes.metadata?.categories
) {
clonedBlock.attributes.metadata.categories = [
selectedCategory,
];
}
return clonedBlock;
} ),
pattern.name
);
createSuccessNotice(
Expand All @@ -78,7 +90,7 @@ const usePatternsState = ( onInsert, rootClientId ) => {
}
);
},
[ createSuccessNotice, onInsert ]
[ createSuccessNotice, onInsert, selectedCategory ]
);

return [ patterns, allCategories, onClickPattern ];
Expand Down

0 comments on commit 40bbc46

Please sign in to comment.