Skip to content

Commit

Permalink
Patterns: move mapping of values from core-data selector into consume…
Browse files Browse the repository at this point in the history
…rs (#54576)

Co-authored-by: Kai Hao <[email protected]>
  • Loading branch information
glendaviesnz and kevin940726 authored Sep 19, 2023
1 parent 6999011 commit 9fa8d8d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ _Parameters_

_Returns_

- `UserPatternCategories`: User patterns category array and map keyed by id.
- `Array< UserPatternCategory >`: User patterns category array.

### getUserQueryResults

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const usePatternsState = ( onInsert, rootClientId ) => {
} = getSettings();
return {
patterns: __experimentalGetAllowedPatterns( rootClientId ),
userPatternCategories:
__experimentalUserPatternCategories?.patternCategories,
userPatternCategories: __experimentalUserPatternCategories,
patternCategories: __experimentalBlockPatternCategories,
};
},
Expand Down
8 changes: 6 additions & 2 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2294,8 +2294,12 @@ const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
function getUserPatterns( state ) {
const userPatterns =
state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY;
const { patternCategoriesMap: categories } =
state?.settings?.__experimentalUserPatternCategories ?? {};
const userPatternCategories =
state?.settings?.__experimentalUserPatternCategories ?? [];
const categories = new Map();
userPatternCategories.forEach( ( userCategory ) =>
categories.set( userCategory.id, userCategory )
);
return userPatterns.map( ( userPattern ) => {
return {
name: `core/block/${ userPattern.id }`,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ _Parameters_

_Returns_

- `UserPatternCategories`: User patterns category array and map keyed by id.
- `Array< UserPatternCategory >`: User patterns category array.

### getUserQueryResults

Expand Down
21 changes: 4 additions & 17 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,14 @@ interface UserState {
byId: Record< EntityRecordKey, ET.User< 'edit' > >;
}

interface UserPatternCategory {
export interface UserPatternCategory {
id: number;
name: string;
label: string;
slug: string;
description: string;
}

export interface UserPatternCategories {
patternCategories: Array< UserPatternCategory >;
patternCategoriesMap: Map< number, UserPatternCategory >;
}

type Optional< T > = T | undefined;

/**
Expand Down Expand Up @@ -1241,21 +1236,13 @@ export function getBlockPatternCategories( state: State ): Array< any > {
*
* @param state Data state.
*
* @return User patterns category array and map keyed by id.
* @return User patterns category array.
*/

export function getUserPatternCategories(
state: State
): UserPatternCategories {
const patternCategoriesMap = new Map< number, UserPatternCategory >();
state.userPatternCategories?.forEach(
( userCategory: UserPatternCategory ) =>
patternCategoriesMap.set( userCategory.id, userCategory )
);
return {
patternCategories: state.userPatternCategories,
patternCategoriesMap,
};
): Array< UserPatternCategory > {
return state.userPatternCategories;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => {

const query = { per_page: -1 };
const records = getEntityRecords( 'postType', PATTERN_TYPES.user, query );
const categories = getUserPatternCategories();

const userPatternCategories = getUserPatternCategories();
const categories = new Map();
userPatternCategories.forEach( ( userCategory ) =>
categories.set( userCategory.id, userCategory )
);
let patterns = records
? records.map( ( record ) =>
patternBlockToPattern( record, categories.patternCategoriesMap )
patternBlockToPattern( record, categories )
)
: EMPTY_PATTERN_LIST;

Expand All @@ -197,7 +200,7 @@ const selectUserPatterns = ( select, { search = '', syncStatus } = {} ) => {
hasCategory: () => true,
} );

return { patterns, isResolving, categories: categories.patternCategories };
return { patterns, isResolving, categories: userPatternCategories };
};

export const usePatterns = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ export default function usePatternDetails( postType, postId ) {
const { currentTheme, patternCategories } = useSelect( ( select ) => {
const { getCurrentTheme, getUserPatternCategories } =
select( coreStore );
const userPatternCategories = getUserPatternCategories();
const categories = new Map();
userPatternCategories.forEach( ( userCategory ) =>
categories.set( userCategory.id, userCategory )
);

return {
currentTheme: getCurrentTheme(),
patternCategories: getUserPatternCategories().patternCategoriesMap,
patternCategories: categories,
};
}, [] );

Expand Down

1 comment on commit 9fa8d8d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 9fa8d8d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6230773430
📝 Reported issues:

Please sign in to comment.