Merge pull request woocommerce/woocommerce-blocks#109 from woocommerce/fix/106-attribute-select

Refactor attribute select
This commit is contained in:
Tiago Noronha 2018-09-06 16:42:23 +01:00 committed by GitHub
commit cc05f947cd
2 changed files with 815 additions and 424 deletions

File diff suppressed because it is too large Load Diff

View File

@ -159,23 +159,89 @@ const ProductAttributeFilter = ( props ) => {
/** /**
* List of attributes. * List of attributes.
*/ */
/*const ProductAttributeList = withAPIData( ( props ) => { class ProductAttributeList extends React.Component {
return {
attributes: '/wc/v2/products/attributes' /**
* Constructor
*/
constructor( props ) {
super( props );
this.state = {
attributes: [],
loaded: false,
query: '',
}; };
} )( ( { attributes, selectedAttribute, filterQuery, selectedTerms, setSelectedAttribute, addTerm, removeTerm } ) => {
if ( ! attributes.data ) { this.updatePreview = this.updatePreview.bind( this );
return __( 'Loading' ); this.getQuery = this.getQuery.bind( this );
} }
if ( 0 === attributes.data.length ) { /**
return __( 'No attributes found' ); * Get the preview when component is first loaded.
*/
componentDidMount() {
if ( this.getQuery() !== this.state.query ) {
this.updatePreview();
}
} }
/**
* Update the preview when component is updated.
*/
componentDidUpdate() {
if ( this.getQuery() !== this.state.query && this.state.loaded ) {
this.updatePreview();
}
}
/**
* Get the endpoint for the current state of the component.
*
* @return string
*/
getQuery() {
const endpoint = '/wc/v2/products/attributes';
return endpoint;
}
/**
* Update the preview with the latest settings.
*/
updatePreview() {
const self = this;
const query = this.getQuery();
self.setState( {
loaded: false
} );
apiFetch( { path: query } ).then( attributes => {
self.setState( {
attributes: attributes,
loaded: true,
query: query
} );
} );
}
/**
* Render.
*/
render() {
const { selectedAttribute, filterQuery, selectedTerms, setSelectedAttribute, addTerm, removeTerm } = this.props;
if ( ! this.state.loaded ) {
return ( <ul><li>{ __( 'Loading' ) }</li></ul> );
}
if ( 0 === this.state.attributes.length ) {
return ( <ul><li>{ __( 'No attributes found' ) }</li></ul> );
}
const filter = filterQuery.toLowerCase(); const filter = filterQuery.toLowerCase();
let attributeElements = []; let attributeElements = [];
for ( let attribute of attributes.data ) {
for ( let attribute of this.state.attributes ) {
// Filter out attributes that don't match the search query. // Filter out attributes that don't match the search query.
if ( filter.length && -1 === attribute.name.toLowerCase().indexOf( filter ) ) { if ( filter.length && -1 === attribute.name.toLowerCase().indexOf( filter ) ) {
continue; continue;
@ -199,8 +265,7 @@ const ProductAttributeFilter = ( props ) => {
</div> </div>
); );
} }
);*/ }
const ProductAttributeList = null;
/** /**
* One product attribute. * One product attribute.
@ -279,19 +344,90 @@ class ProductAttributeElement extends React.Component {
/** /**
* The list of terms in an attribute. * The list of terms in an attribute.
*/ */
/*const AttributeTerms = withAPIData( ( props ) => { class AttributeTerms extends React.Component {
return {
terms: '/wc/v2/products/attributes/' + props.attribute.id + '/terms' /**
* Constructor
*/
constructor( props ) {
super( props );
this.state = {
terms: [],
loaded: false,
query: '',
}; };
} )( ( { terms, selectedTerms, attribute, addTerm, removeTerm } ) => {
if ( ! terms.data ) { this.updatePreview = this.updatePreview.bind( this );
this.getQuery = this.getQuery.bind( this );
}
/**
* Get the preview when component is first loaded.
*/
componentDidMount() {
if ( this.getQuery() !== this.state.query ) {
this.updatePreview();
}
}
/**
* Update the preview when component is updated.
*/
componentDidUpdate() {
if ( this.getQuery() !== this.state.query && this.state.loaded ) {
this.updatePreview();
}
}
/**
* Get the endpoint for the current state of the component.
*
* @return string
*/
getQuery() {
const endpoint = '/wc/v2/products/attributes/' + this.props.attribute.id + '/terms';
return endpoint;
}
/**
* Update the preview with the latest settings.
*/
updatePreview() {
const self = this;
const query = this.getQuery();
self.setState( {
loaded: false
} );
apiFetch( { path: query } ).then( terms => {
self.setState( {
terms: terms,
loaded: true,
query: query
} );
} );
}
/**
* Render.
*/
render() {
const { selectedTerms, attribute, addTerm, removeTerm } = this.props;
if ( ! this.state.loaded ) {
return ( <ul><li>{ __( 'Loading' ) }</li></ul> ); return ( <ul><li>{ __( 'Loading' ) }</li></ul> );
} }
if ( 0 === terms.data.length ) { if ( 0 === this.state.terms.length ) {
return ( <ul><li>{ __( 'No terms found' ) }</li></ul> ); return ( <ul><li>{ __( 'No terms found' ) }</li></ul> );
} }
/**
* Add or remove selected terms.
*
* @param evt Event object
*/
function handleTermChange( evt ) { function handleTermChange( evt ) {
if ( evt.target.checked ) { if ( evt.target.checked ) {
addTerm( evt.target.value ); addTerm( evt.target.value );
@ -302,7 +438,7 @@ class ProductAttributeElement extends React.Component {
return ( return (
<ul> <ul>
{ terms.data.map( ( term ) => ( { this.state.terms.map( ( term ) => (
<li className="wc-products-list-card__item"> <li className="wc-products-list-card__item">
<label className="wc-products-list-card__content"> <label className="wc-products-list-card__content">
<input type="checkbox" <input type="checkbox"
@ -318,5 +454,4 @@ class ProductAttributeElement extends React.Component {
</ul> </ul>
); );
} }
);*/ }
const AttributeTerms = null;