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

[Product Collection] Fix: HTML entity decoding for product names in Hand-Picked Products #11927

Merged
merged 6 commits into from
Nov 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { getProducts } from '@woocommerce/editor-components/utils';
import { ProductResponseItem } from '@woocommerce/types';
import { decodeEntities } from '@wordpress/html-entities';
import { useState, useEffect, useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
Expand Down Expand Up @@ -91,16 +92,21 @@ const HandPickedProductsControl = ( {
);
}, [ productsList, selectedProductIds ] );

/**
* Transforms a token into a product name.
* - If the token is a number, it will be used to lookup the product name.
* - Otherwise, the token will be used as is.
*/
const transformTokenIntoProductName = ( token: string ) => {
const parsedToken = Number( token );

if ( Number.isNaN( parsedToken ) ) {
return token;
return decodeEntities( token ) || '';
}

const product = productsMap.get( parsedToken );

return product?.name || '';
return decodeEntities( product?.name ) || '';
};

return (
Expand All @@ -120,7 +126,7 @@ const HandPickedProductsControl = ( {
disabled={ ! productsMap.size }
displayTransform={ transformTokenIntoProductName }
label={ __(
'Pick some products',
'Hand-picked Products',
'woo-gutenberg-products-block'
) }
onChange={ onTokenChange }
Expand All @@ -135,6 +141,7 @@ const HandPickedProductsControl = ( {
: selectedProductIds || []
}
__experimentalExpandOnFocus={ true }
__experimentalShowHowTo={ false }
/>
</ToolsPanelItem>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test.describe( 'Product Collection', () => {
} ) => {
await pageObject.addFilter( 'Show Hand-picked Products' );

const filterName = 'Pick some products';
const filterName = 'Hand-picked Products';
await pageObject.setFilterComboboxValue( filterName, [ 'Album' ] );
await expect( pageObject.products ).toHaveCount( 1 );

Expand Down