2019-09-04 16:07:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
2019-11-08 16:30:11 +00:00
|
|
|
import { getCategories } from '@woocommerce/block-components/utils';
|
2019-09-04 16:07:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { formatError } from '../base/utils/errors.js';
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
const withCategories = createHigherOrderComponent( ( OriginalComponent ) => {
|
|
|
|
return class WrappedComponent extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
|
|
|
loading: false,
|
|
|
|
categories: null,
|
|
|
|
};
|
|
|
|
this.loadCategories = this.loadCategories.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadCategories();
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCategories() {
|
|
|
|
this.setState( { loading: true } );
|
|
|
|
|
|
|
|
getCategories()
|
|
|
|
.then( ( categories ) => {
|
|
|
|
this.setState( {
|
|
|
|
categories,
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
2019-09-04 16:07:00 +00:00
|
|
|
const error = await formatError( e );
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
this.setState( {
|
|
|
|
categories: null,
|
|
|
|
loading: false,
|
|
|
|
error,
|
|
|
|
} );
|
2019-09-04 16:07:00 +00:00
|
|
|
} );
|
2019-09-05 15:09:31 +00:00
|
|
|
}
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
render() {
|
|
|
|
const { error, loading, categories } = this.state;
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
return (
|
|
|
|
<OriginalComponent
|
2019-09-04 16:07:00 +00:00
|
|
|
{ ...this.props }
|
|
|
|
error={ error }
|
|
|
|
isLoading={ loading }
|
|
|
|
categories={ categories }
|
2019-09-05 15:09:31 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, 'withCategories' );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
|
|
|
export default withCategories;
|