Skip to content

Commit

Permalink
Patterns: Refactor the findOrCreate term method (#57655)
Browse files Browse the repository at this point in the history
* Refactor the findOrCreate term method to not call API if user category already exist, and fix bug that allowed different cased labels that could then not be selected

* Add missing lower casing

* Remove type as we can just use id to identify the user patterns in this context

* Add the duplicate term error handling back in as had a duplicate term once in testing, but haven't been able to replicate, but no disadvantage in having this code just in case
  • Loading branch information
glendaviesnz authored Jan 10, 2024
1 parent 7c21100 commit 5d063e3
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions packages/patterns/src/components/create-pattern-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,27 @@ export function CreatePatternModalContents( {
const categoryMap = useMemo( () => {
// Merge the user and core pattern categories and remove any duplicates.
const uniqueCategories = new Map();
[ ...userPatternCategories, ...corePatternCategories ].forEach(
( category ) => {
if (
! uniqueCategories.has( category.label ) &&
// There are two core categories with `Post` label so explicitly remove the one with
// the `query` slug to avoid any confusion.
category.name !== 'query'
) {
// We need to store the name separately as this is used as the slug in the
// taxonomy and may vary from the label.
uniqueCategories.set( category.label, {
label: category.label,
value: category.label,
name: category.name,
} );
}
userPatternCategories.forEach( ( category ) => {
uniqueCategories.set( category.label.toLowerCase(), {
label: category.label,
name: category.name,
id: category.id,
} );
} );

corePatternCategories.forEach( ( category ) => {
if (
! uniqueCategories.has( category.label.toLowerCase() ) &&
// There are two core categories with `Post` label so explicitly remove the one with
// the `query` slug to avoid any confusion.
category.name !== 'query'
) {
uniqueCategories.set( category.label.toLowerCase(), {
label: category.label,
name: category.name,
} );
}
);
} );
return uniqueCategories;
}, [ userPatternCategories, corePatternCategories ] );

Expand Down Expand Up @@ -140,9 +143,13 @@ export function CreatePatternModalContents( {
*/
async function findOrCreateTerm( term ) {
try {
// We need to match any existing term to the correct slug to prevent duplicates, eg.
// the core `Headers` category uses the singular `header` as the slug.
const existingTerm = categoryMap.get( term );
const existingTerm = categoryMap.get( term.toLowerCase() );
if ( existingTerm && existingTerm.id ) {
return existingTerm.id;
}
// If we have an existing core category we need to match the new user category to the
// correct slug rather than autogenerating it to prevent duplicates, eg. the core `Headers`
// category uses the singular `header` as the slug.
const termData = existingTerm
? { name: existingTerm.label, slug: existingTerm.name }
: { name: term };
Expand Down

0 comments on commit 5d063e3

Please sign in to comment.