Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix Attribute terms sometimes being assigned children #8720

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions assets/js/editor-components/search-list-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ export type SearchListItem = {
breadcrumbs: string[];
children?: SearchListItem[];
count: number;
id: string | number;
id?: string | number;
name: string;
parent: number;
termId?: string | number;
value: string;
};
// This is to avoid a problem with overlapping
// ids with terms and their parents.
// For more context: https://github.com/woocommerce/woocommerce-blocks/pull/8720
} & (
| { id: string | number; termId?: never }
| { id?: never; termId: string | number }
);

export interface SearchListItemsContainerProps
extends SearchListControlProps,
Expand Down
9 changes: 7 additions & 2 deletions assets/js/editor-components/search-list-control/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ export const buildTermsTree = (

const fillWithChildren = ( terms: SearchListItem[] ): SearchListItem[] => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
builtParents.push( '' + term.id );
const id = ( term.id ?? term.termId ) as string | number;
// If the object has `termId` property, it is an `AttributeTerm`.
// Those can't have children, but sometimes they have the same `id`
// as an `AttributeObject`, causing possible overlaps.
// For more context: https://github.com/woocommerce/woocommerce-blocks/pull/8720
const children = term.termId ? null : termsByParent[ id ];
builtParents.push( '' + id );
return {
...term,
breadcrumbs: getParentsName( listById[ term.parent ] ),
Expand Down
10 changes: 8 additions & 2 deletions assets/js/utils/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,20 @@ export const convertAttributeObjectToSearchItem = (
): SearchListItem => {
const { count, id, name, parent } = attribute;

const base = isAttributeTerm( attribute )
? {
termId: id,
value: attribute.attr_slug,
}
: { id, value: '' };

return {
...base,
count,
id,
name,
parent,
breadcrumbs: [],
children: [],
value: isAttributeTerm( attribute ) ? attribute.attr_slug : '',
};
};

Expand Down