2019-08-15 09:36:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
2019-11-08 16:30:11 +00:00
|
|
|
import { getCategory } from '@woocommerce/block-components/utils';
|
2019-08-15 09:36:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-08-30 09:36:06 +00:00
|
|
|
import { formatError } from '../base/utils/errors.js';
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
const withCategory = createHigherOrderComponent( ( OriginalComponent ) => {
|
|
|
|
return class WrappedComponent extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
|
|
|
loading: false,
|
2019-11-05 10:33:07 +00:00
|
|
|
category:
|
|
|
|
this.props.attributes.categoryId === 'preview'
|
|
|
|
? this.props.attributes.previewCategory
|
|
|
|
: null,
|
2019-09-05 15:09:31 +00:00
|
|
|
};
|
|
|
|
this.loadCategory = this.loadCategory.bind( this );
|
|
|
|
}
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.loadCategory();
|
|
|
|
}
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
componentDidUpdate( prevProps ) {
|
|
|
|
if (
|
2019-09-09 10:52:48 +00:00
|
|
|
prevProps.attributes.categoryId !==
|
|
|
|
this.props.attributes.categoryId
|
2019-09-05 15:09:31 +00:00
|
|
|
) {
|
|
|
|
this.loadCategory();
|
2019-08-15 09:36:24 +00:00
|
|
|
}
|
2019-09-05 15:09:31 +00:00
|
|
|
}
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
loadCategory() {
|
|
|
|
const { categoryId } = this.props.attributes;
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-11-05 10:33:07 +00:00
|
|
|
if ( categoryId === 'preview' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
if ( ! categoryId ) {
|
|
|
|
this.setState( {
|
|
|
|
category: null,
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
|
|
|
} );
|
|
|
|
return;
|
|
|
|
}
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
this.setState( { loading: true } );
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
getCategory( categoryId )
|
|
|
|
.then( ( category ) => {
|
2019-08-15 09:36:24 +00:00
|
|
|
this.setState( { category, loading: false, error: null } );
|
2019-09-05 15:09:31 +00:00
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
2019-09-04 16:07:00 +00:00
|
|
|
const error = await formatError( e );
|
2019-08-15 09:36:24 +00:00
|
|
|
|
|
|
|
this.setState( { category: null, loading: false, error } );
|
|
|
|
} );
|
2019-09-05 15:09:31 +00:00
|
|
|
}
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
render() {
|
|
|
|
const { error, loading, category } = this.state;
|
2019-08-15 09:36:24 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
return (
|
|
|
|
<OriginalComponent
|
2019-08-15 09:36:24 +00:00
|
|
|
{ ...this.props }
|
|
|
|
error={ error }
|
|
|
|
getCategory={ this.loadCategory }
|
|
|
|
isLoading={ loading }
|
|
|
|
category={ category }
|
2019-09-05 15:09:31 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, 'withCategory' );
|
2019-08-15 09:36:24 +00:00
|
|
|
|
|
|
|
export default withCategory;
|