[Experimental] Product filters > Attributes loading state in editor (#51151)

* Fix no products component prematurely being rendered

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Use useMemo to memoirize component element on re-renders

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Roy Ho 2024-09-06 05:33:02 -07:00 committed by GitHub
parent a61a2bd29e
commit 63243c57a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 17 deletions

View File

@ -13,7 +13,7 @@ import {
} from '@woocommerce/types'; } from '@woocommerce/types';
import { useBlockProps } from '@wordpress/block-editor'; import { useBlockProps } from '@wordpress/block-editor';
import { Disabled, Notice, withSpokenMessages } from '@wordpress/components'; import { Disabled, Notice, withSpokenMessages } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element'; import { useEffect, useState, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
/** /**
@ -27,6 +27,7 @@ import { attributeOptionsPreview } from './constants';
import './style.scss'; import './style.scss';
import { EditProps, isAttributeCounts } from './types'; import { EditProps, isAttributeCounts } from './types';
import { getAttributeFromId } from './utils'; import { getAttributeFromId } from './utils';
import './editor.scss';
const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] ); const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] );
@ -49,22 +50,26 @@ const Edit = ( props: EditProps ) => {
AttributeTerm[] AttributeTerm[]
>( [] ); >( [] );
const { results: attributeTerms } = useCollection< AttributeTerm >( { const { results: attributeTerms, isLoading: isTermsLoading } =
namespace: '/wc/store/v1', useCollection< AttributeTerm >( {
resourceName: 'products/attributes/terms', namespace: '/wc/store/v1',
resourceValues: [ attributeObject?.id || 0 ], resourceName: 'products/attributes/terms',
shouldSelect: !! attributeObject?.id, resourceValues: [ attributeObject?.id || 0 ],
query: { orderby: 'menu_order', hide_empty: hideEmpty }, shouldSelect: !! attributeObject?.id,
} ); query: { orderby: 'menu_order', hide_empty: hideEmpty },
} );
const { results: filteredCounts } = useCollectionData( { const { results: filteredCounts, isLoading: isCountsLoading } =
queryAttribute: { useCollectionData( {
taxonomy: attributeObject?.taxonomy || '', queryAttribute: {
queryType, taxonomy: attributeObject?.taxonomy || '',
}, queryType,
queryState: {}, },
isEditor: true, queryState: {},
} ); isEditor: true,
} );
const isLoading = isTermsLoading || isCountsLoading;
useEffect( () => { useEffect( () => {
const termIdHasProducts = const termIdHasProducts =
@ -106,6 +111,20 @@ const Edit = ( props: EditProps ) => {
</div> </div>
); );
const loadingState = useMemo( () => {
return [ ...Array( 5 ) ].map( ( x, i ) => (
<li
key={ i }
style={ {
/* stylelint-disable */
width: Math.floor( Math.random() * ( 100 - 25 ) ) + '%',
} }
>
&nbsp;
</li>
) );
}, [] );
if ( isPreview ) { if ( isPreview ) {
return ( return (
<Wrapper> <Wrapper>
@ -144,7 +163,16 @@ const Edit = ( props: EditProps ) => {
</Wrapper> </Wrapper>
); );
if ( attributeOptions.length === 0 ) if ( isLoading )
return (
<Wrapper>
<ul className="is-loading wp-block-woocommerce-product-filter-attribute__loading">
{ loadingState }
</ul>
</Wrapper>
);
if ( attributeTerms.length === 0 )
return ( return (
<Wrapper> <Wrapper>
<Notice status="warning" isDismissible={ false }> <Notice status="warning" isDismissible={ false }>

View File

@ -0,0 +1,8 @@
.wp-block-woocommerce-product-filter-attribute__loading {
padding: 0;
li {
@include placeholder();
margin: 5px 0;
}
}

View File

@ -0,0 +1,4 @@
Significance: patch
Type: tweak
Comment: Product filters: add loading state for attributes filter