2019-09-04 16:07:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getAttributes, getTerms } from '../components/utils';
|
|
|
|
import { formatError } from '../base/utils/errors.js';
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
const withAttributes = createHigherOrderComponent( ( OriginalComponent ) => {
|
|
|
|
class WrappedComponent extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
attributes: [],
|
|
|
|
error: null,
|
|
|
|
expandedAttribute: null,
|
|
|
|
loading: false,
|
|
|
|
termsList: {},
|
|
|
|
termsLoading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.loadAttributes = this.loadAttributes.bind( this );
|
|
|
|
this.onExpandAttribute = this.onExpandAttribute.bind( this );
|
2019-09-09 10:52:48 +00:00
|
|
|
this.debouncedLoadTerms = debounce(
|
|
|
|
this.loadTerms.bind( this ),
|
|
|
|
200
|
|
|
|
);
|
2019-09-05 15:09:31 +00:00
|
|
|
}
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.loadAttributes();
|
|
|
|
}
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.debouncedLoadTerms.cancel();
|
|
|
|
}
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
componentDidUpdate( prevProps, prevState ) {
|
2019-09-09 10:52:48 +00:00
|
|
|
if (
|
|
|
|
prevState.expandedAttribute !== this.state.expandedAttribute
|
|
|
|
) {
|
2019-09-05 15:09:31 +00:00
|
|
|
this.debouncedLoadTerms();
|
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
|
|
|
loadAttributes() {
|
|
|
|
const { selected } = this.props;
|
|
|
|
const { expandedAttribute } = this.state;
|
|
|
|
this.setState( { loading: true } );
|
|
|
|
|
|
|
|
getAttributes()
|
|
|
|
.then( ( attributes ) => {
|
|
|
|
attributes = attributes.map( ( item ) => ( {
|
|
|
|
...item,
|
|
|
|
parent: 0,
|
|
|
|
} ) );
|
2019-09-04 16:07:00 +00:00
|
|
|
let newExpandedAttribute = expandedAttribute;
|
|
|
|
if ( ! expandedAttribute && selected.length > 0 ) {
|
2019-09-05 15:09:31 +00:00
|
|
|
const attr = attributes.find(
|
|
|
|
( item ) => item.slug === selected[ 0 ].attr_slug
|
|
|
|
);
|
2019-09-04 16:07:00 +00:00
|
|
|
if ( attr ) {
|
|
|
|
newExpandedAttribute = attr.id;
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 15:09:31 +00:00
|
|
|
this.setState( {
|
|
|
|
attributes,
|
|
|
|
expandedAttribute: newExpandedAttribute,
|
|
|
|
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( {
|
|
|
|
attributes: [],
|
|
|
|
expandedAttribute: 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
|
|
|
loadTerms() {
|
|
|
|
const { expandedAttribute, termsList } = this.state;
|
|
|
|
if ( ! expandedAttribute ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( ! termsList[ expandedAttribute ] ) {
|
|
|
|
this.setState( { termsLoading: true } );
|
2019-09-04 16:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
getTerms( expandedAttribute )
|
|
|
|
.then( ( terms ) => {
|
|
|
|
terms = terms.map( ( term ) => ( {
|
|
|
|
...term,
|
|
|
|
parent: expandedAttribute,
|
|
|
|
attr_slug: term.attribute.slug,
|
|
|
|
} ) );
|
|
|
|
this.setState( ( prevState ) => ( {
|
|
|
|
termsList: {
|
|
|
|
...prevState.termsList,
|
|
|
|
[ expandedAttribute ]: terms,
|
|
|
|
},
|
|
|
|
termsLoading: false,
|
|
|
|
} ) );
|
|
|
|
} )
|
|
|
|
.catch( async ( e ) => {
|
|
|
|
const error = await formatError( e );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
this.setState( {
|
|
|
|
termsList: {},
|
|
|
|
termsLoading: 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
|
|
|
onExpandAttribute( attributeId ) {
|
|
|
|
const { expandedAttribute } = this.state;
|
2019-09-04 16:07:00 +00:00
|
|
|
|
2019-09-05 15:09:31 +00:00
|
|
|
this.setState( {
|
|
|
|
expandedAttribute:
|
|
|
|
attributeId === expandedAttribute ? null : attributeId,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
error,
|
|
|
|
expandedAttribute,
|
|
|
|
loading,
|
|
|
|
attributes,
|
|
|
|
termsList,
|
|
|
|
termsLoading,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OriginalComponent
|
2019-09-04 16:07:00 +00:00
|
|
|
{ ...this.props }
|
|
|
|
attributes={ attributes }
|
|
|
|
error={ error }
|
|
|
|
expandedAttribute={ expandedAttribute }
|
|
|
|
onExpandAttribute={ this.onExpandAttribute }
|
|
|
|
isLoading={ loading }
|
|
|
|
termsAreLoading={ termsLoading }
|
|
|
|
termsList={ termsList }
|
2019-09-05 15:09:31 +00:00
|
|
|
/>
|
|
|
|
);
|
2019-09-04 16:07:00 +00:00
|
|
|
}
|
2019-09-05 15:09:31 +00:00
|
|
|
}
|
|
|
|
WrappedComponent.propTypes = {
|
|
|
|
selected: PropTypes.array,
|
|
|
|
};
|
|
|
|
WrappedComponent.defaultProps = {
|
|
|
|
selected: [],
|
|
|
|
};
|
|
|
|
return WrappedComponent;
|
|
|
|
}, 'withAttributes' );
|
2019-09-04 16:07:00 +00:00
|
|
|
|
|
|
|
export default withAttributes;
|