Skip to content

Commit

Permalink
Fix: JavaScript error when pattern category is unregistered (#67063)
Browse files Browse the repository at this point in the history
* Fix: JavaScript error when pattern category is unregistered

Because the wp_pattern_category taxonomy can be unregistered, it needs to be used conditionally in the JavaScript code, or there may be JavaScript errors.

* Add the conditional chaining to two more places
  • Loading branch information
carolinan authored Nov 19, 2024
1 parent 32fe48c commit 327238a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function mapUserPattern(
id: userPattern.id,
type: INSERTER_PATTERN_TYPES.user,
title: userPattern.title.raw,
categories: userPattern.wp_pattern_category.map( ( catId ) => {
categories: userPattern.wp_pattern_category?.map( ( catId ) => {
const category = __experimentalUserPatternCategories.find(
( { id } ) => id === catId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const selectPatterns = createSelector(
categoryId,
hasCategory: ( item, currentCategory ) => {
if ( item.type === PATTERN_TYPES.user ) {
return item.wp_pattern_category.some(
return item.wp_pattern_category?.some(
( catId ) =>
userPatternCategories.find(
( cat ) => cat.id === catId
Expand All @@ -173,7 +173,7 @@ const selectPatterns = createSelector(
return (
userPatternCategories?.length &&
( ! item.wp_pattern_category?.length ||
! item.wp_pattern_category.some( ( catId ) =>
! item.wp_pattern_category?.some( ( catId ) =>
userPatternCategories.find(
( cat ) => cat.id === catId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function usePatternCategories() {
// If the pattern has no categories, add it to uncategorized.
if (
! pattern.wp_pattern_category?.length ||
! pattern.wp_pattern_category.some( ( catId ) =>
! pattern.wp_pattern_category?.some( ( catId ) =>
userPatternCategories.find( ( cat ) => cat.id === catId )
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getTermLabels( pattern, categories ) {

return categories.user
?.filter( ( category ) =>
pattern.wp_pattern_category.includes( category.id )
pattern.wp_pattern_category?.includes( category.id )
)
.map( ( category ) => category.label );
}
Expand Down

1 comment on commit 327238a

@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 327238a.
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/11909682907
📝 Reported issues:

Please sign in to comment.