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

Commit

Permalink
Fix: Spilled icecream error with Filter Products by Attribute when no…
Browse files Browse the repository at this point in the history
… attribute was selected (#4847)
  • Loading branch information
dinhtungdu authored Oct 15, 2021
1 parent 1c4e97f commit f22c8b6
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions assets/js/blocks/attribute-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Label from '@woocommerce/base-components/filter-element-label';
import FilterSubmitButton from '@woocommerce/base-components/filter-submit-button';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { decodeEntities } from '@wordpress/html-entities';
import { Notice } from '@wordpress/components';

/**
* Internal dependencies
Expand Down Expand Up @@ -61,7 +62,7 @@ const AttributeFilterBlock = ( {
} = useCollection( {
namespace: '/wc/store',
resourceName: 'products/attributes/terms',
resourceValues: [ attributeObject.id ],
resourceValues: [ attributeObject?.id || 0 ],
shouldSelect: blockAttributes.attributeId > 0,
} );

Expand All @@ -73,7 +74,7 @@ const AttributeFilterBlock = ( {
isLoading: filteredCountsLoading,
} = useCollectionData( {
queryAttribute: {
taxonomy: attributeObject.taxonomy,
taxonomy: attributeObject?.taxonomy,
queryType: blockAttributes.queryType,
},
queryState: {
Expand Down Expand Up @@ -151,7 +152,7 @@ const AttributeFilterBlock = ( {

setDisplayedOptions( newOptions );
}, [
attributeObject.taxonomy,
attributeObject?.taxonomy,
attributeTerms,
attributeTermsLoading,
blockAttributes.showCounts,
Expand All @@ -164,10 +165,10 @@ const AttributeFilterBlock = ( {
const checkedQuery = useMemo( () => {
return productAttributesQuery
.filter(
( { attribute } ) => attribute === attributeObject.taxonomy
( { attribute } ) => attribute === attributeObject?.taxonomy
)
.flatMap( ( { slug } ) => slug );
}, [ productAttributesQuery, attributeObject.taxonomy ] );
}, [ productAttributesQuery, attributeObject?.taxonomy ] );

const currentCheckedQuery = useShallowEqual( checkedQuery );
const previousCheckedQuery = usePrevious( currentCheckedQuery );
Expand Down Expand Up @@ -327,7 +328,36 @@ const AttributeFilterBlock = ( {
]
);

// Short-circuit if no attribute is selected.
if ( ! attributeObject ) {
if ( isEditor ) {
return (
<Notice status="warning" isDismissible={ false }>
<p>
{ __(
'Please select an attribute to use this filter!',
'woo-gutenberg-products-block'
) }
</p>
</Notice>
);
}
return null;
}

if ( displayedOptions.length === 0 && ! attributeTermsLoading ) {
if ( isEditor ) {
return (
<Notice status="warning" isDismissible={ false }>
<p>
{ __(
'The selected attribute does not have any term assigned to products.',
'woo-gutenberg-products-block'
) }
</p>
</Notice>
);
}
return null;
}

Expand All @@ -337,11 +367,13 @@ const AttributeFilterBlock = ( {

return (
<>
{ ! isEditor && blockAttributes.heading && (
<TagName className="wc-block-attribute-filter__title">
{ blockAttributes.heading }
</TagName>
) }
{ ! isEditor &&
blockAttributes.heading &&
displayedOptions.length > 0 && (
<TagName className="wc-block-attribute-filter__title">
{ blockAttributes.heading }
</TagName>
) }
<div
className={ `wc-block-attribute-filter style-${ blockAttributes.displayStyle }` }
>
Expand Down

0 comments on commit f22c8b6

Please sign in to comment.