2019-09-04 16:07:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-04-24 13:36:47 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2020-09-02 08:21:46 +00:00
|
|
|
import { getAttributes, getTerms } from '@woocommerce/editor-components/utils';
|
2019-09-04 16:07:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-08-30 06:56:28 +00:00
|
|
|
import { formatError } from '../base/utils/errors';
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-12-10 17:17:46 +00:00
|
|
|
/**
|
2020-04-24 13:36:47 +00:00
|
|
|
* Get attribute data (name, taxonomy etc) from server data.
|
2019-12-10 17:17:46 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {number} attributeId Attribute ID to look for.
|
2020-10-27 14:37:18 +00:00
|
|
|
* @param {Array|null} attributeList List of attributes.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} matchField Field to match on. e.g. id or slug.
|
2019-12-10 17:17:46 +00:00
|
|
|
*/
|
2020-04-24 13:36:47 +00:00
|
|
|
const getAttributeData = ( attributeId, attributeList, matchField = 'id' ) => {
|
2020-10-27 14:37:18 +00:00
|
|
|
return Array.isArray( attributeList )
|
|
|
|
? attributeList.find( ( attr ) => attr[ matchField ] === attributeId )
|
2020-04-24 13:36:47 +00:00
|
|
|
: null;
|
|
|
|
};
|
2019-09-05 15:09:31 +00:00
|
|
|
|
2020-04-24 13:36:47 +00:00
|
|
|
/**
|
|
|
|
* HOC that calls the useAttributes hook.
|
|
|
|
*
|
|
|
|
* @param {Function} OriginalComponent Component being wrapped.
|
|
|
|
*/
|
|
|
|
const withAttributes = ( OriginalComponent ) => {
|
|
|
|
return ( props ) => {
|
2020-10-27 14:37:18 +00:00
|
|
|
const { selected = [] } = props;
|
|
|
|
const selectedSlug = selected.length ? selected[ 0 ].attr_slug : null;
|
|
|
|
const [ attributes, setAttributes ] = useState( null );
|
2020-04-24 13:36:47 +00:00
|
|
|
const [ expandedAttribute, setExpandedAttribute ] = useState( 0 );
|
|
|
|
const [ termsList, setTermsList ] = useState( {} );
|
|
|
|
const [ loading, setLoading ] = useState( true );
|
|
|
|
const [ termsLoading, setTermsLoading ] = useState( false );
|
|
|
|
const [ error, setError ] = useState( null );
|
|
|
|
|
|
|
|
useEffect( () => {
|
2020-10-27 14:37:18 +00:00
|
|
|
if ( attributes === null ) {
|
|
|
|
getAttributes()
|
|
|
|
.then( ( newAttributes ) => {
|
|
|
|
newAttributes = newAttributes.map( ( attribute ) => ( {
|
|
|
|
...attribute,
|
|
|
|
parent: 0,
|
|
|
|
} ) );
|
2020-04-24 13:36:47 +00:00
|
|
|
|
2020-10-27 14:37:18 +00:00
|
|
|
setAttributes( newAttributes );
|
2020-04-24 13:36:47 +00:00
|
|
|
|
2020-10-27 14:37:18 +00:00
|
|
|
if ( selectedSlug ) {
|
|
|
|
const selectedAttributeFromTerm = getAttributeData(
|
|
|
|
selectedSlug,
|
|
|
|
newAttributes,
|
|
|
|
'taxonomy'
|
2020-04-24 13:36:47 +00:00
|
|
|
);
|
2020-10-27 14:37:18 +00:00
|
|
|
|
|
|
|
if ( selectedAttributeFromTerm ) {
|
|
|
|
setExpandedAttribute(
|
|
|
|
selectedAttributeFromTerm.id
|
|
|
|
);
|
|
|
|
}
|
2019-09-04 16:07:00 +00:00
|
|
|
}
|
2020-10-27 14:37:18 +00:00
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
|
|
|
setError( await formatError( e ) );
|
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
setLoading( false );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}, [ attributes, selectedSlug ] );
|
2020-04-24 13:36:47 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
2020-10-27 14:37:18 +00:00
|
|
|
const attributeData = getAttributeData(
|
|
|
|
expandedAttribute,
|
|
|
|
attributes
|
|
|
|
);
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2020-04-24 13:36:47 +00:00
|
|
|
if ( ! attributeData ) {
|
2019-09-05 15:09:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-24 13:36:47 +00:00
|
|
|
|
|
|
|
setTermsLoading( true );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
getTerms( expandedAttribute )
|
2020-04-24 13:36:47 +00:00
|
|
|
.then( ( newTerms ) => {
|
|
|
|
newTerms = newTerms.map( ( term ) => ( {
|
2019-09-05 15:09:31 +00:00
|
|
|
...term,
|
|
|
|
parent: expandedAttribute,
|
2020-06-19 14:51:09 +00:00
|
|
|
attr_slug: attributeData.taxonomy,
|
2019-09-05 15:09:31 +00:00
|
|
|
} ) );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2020-10-27 14:37:18 +00:00
|
|
|
setTermsList( ( previousTermsList ) => ( {
|
|
|
|
...previousTermsList,
|
2020-04-24 13:36:47 +00:00
|
|
|
[ expandedAttribute ]: newTerms,
|
2020-10-27 14:37:18 +00:00
|
|
|
} ) );
|
2020-04-24 13:36:47 +00:00
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
|
|
|
setError( await formatError( e ) );
|
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
setTermsLoading( false );
|
2019-09-04 16:07:00 +00:00
|
|
|
} );
|
2020-04-24 13:36:47 +00:00
|
|
|
}, [ expandedAttribute, attributes ] );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OriginalComponent
|
|
|
|
{ ...props }
|
2020-10-27 14:37:18 +00:00
|
|
|
attributes={ attributes || [] }
|
2020-04-24 13:36:47 +00:00
|
|
|
error={ error }
|
|
|
|
expandedAttribute={ expandedAttribute }
|
|
|
|
onExpandAttribute={ setExpandedAttribute }
|
|
|
|
isLoading={ loading }
|
|
|
|
termsAreLoading={ termsLoading }
|
|
|
|
termsList={ termsList }
|
|
|
|
/>
|
|
|
|
);
|
2019-09-05 15:09:31 +00:00
|
|
|
};
|
2020-04-24 13:36:47 +00:00
|
|
|
};
|
2019-09-04 16:07:00 +00:00
|
|
|
|
|
|
|
export default withAttributes;
|