woocommerce/plugins/woocommerce-blocks/assets/js/blocks/product-top-rated/block.js

182 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-12-14 19:45:19 +00:00
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
import classnames from 'classnames';
2018-12-14 19:45:19 +00:00
import { Component, Fragment } from '@wordpress/element';
import { debounce } from 'lodash';
2018-12-14 19:45:19 +00:00
import Gridicon from 'gridicons';
import { InspectorControls } from '@wordpress/editor';
import { PanelBody, Placeholder, Spinner } from '@wordpress/components';
2018-12-14 19:45:19 +00:00
import PropTypes from 'prop-types';
/**
* Internal dependencies
*/
import getQuery from '../../utils/get-query';
import GridContentControl from '../../components/grid-content-control';
import GridLayoutControl from '../../components/grid-layout-control';
import ProductCategoryControl from '../../components/product-category-control';
import ProductPreview from '../../components/product-preview';
2018-12-14 19:45:19 +00:00
/**
* Component to handle edit mode of "Top Rated Products".
*/
class ProductTopRatedBlock extends Component {
constructor() {
super( ...arguments );
this.state = {
products: [],
loaded: false,
};
this.debouncedGetProducts = debounce( this.getProducts.bind( this ), 200 );
2018-12-14 19:45:19 +00:00
}
componentDidMount() {
if ( this.props.attributes.categories ) {
this.getProducts();
}
}
componentDidUpdate( prevProps ) {
const hasChange = [ 'rows', 'columns', 'categories', 'catOperator' ].reduce(
( acc, key ) => {
return acc || prevProps.attributes[ key ] !== this.props.attributes[ key ];
},
false
);
2018-12-14 19:45:19 +00:00
if ( hasChange ) {
this.debouncedGetProducts();
2018-12-14 19:45:19 +00:00
}
}
getProducts() {
apiFetch( {
path: addQueryArgs(
'/wc-blocks/v1/products',
getQuery( this.props.attributes, this.props.name )
),
2018-12-14 19:45:19 +00:00
} )
.then( ( products ) => {
this.setState( { products, loaded: true } );
} )
.catch( () => {
this.setState( { products: [], loaded: true } );
} );
}
getInspectorControls() {
const { attributes, setAttributes } = this.props;
const {
categories,
catOperator,
columns,
contentVisibility,
rows,
} = attributes;
2018-12-14 19:45:19 +00:00
return (
<InspectorControls key="inspector">
<PanelBody
title={ __( 'Layout', 'woo-gutenberg-products-block' ) }
initialOpen
>
<GridLayoutControl
columns={ columns }
rows={ rows }
setAttributes={ setAttributes }
2018-12-14 19:45:19 +00:00
/>
</PanelBody>
<PanelBody
title={ __( 'Content', 'woo-gutenberg-products-block' ) }
initialOpen
>
<GridContentControl
settings={ contentVisibility }
onChange={ ( value ) => setAttributes( { contentVisibility: value } ) }
/>
</PanelBody>
2018-12-14 19:45:19 +00:00
<PanelBody
title={ __(
'Filter by Product Category',
'woo-gutenberg-products-block'
) }
initialOpen={ false }
>
<ProductCategoryControl
selected={ categories }
2018-12-14 19:45:19 +00:00
onChange={ ( value = [] ) => {
const ids = value.map( ( { id } ) => id );
setAttributes( { categories: ids } );
} }
operator={ catOperator }
onOperatorChange={ ( value = 'any' ) =>
setAttributes( { catOperator: value } )
}
2018-12-14 19:45:19 +00:00
/>
</PanelBody>
</InspectorControls>
);
}
render() {
const { columns, contentVisibility } = this.props.attributes;
const { loaded, products = [] } = this.state;
const classes = classnames( {
'wc-block-products-grid': true,
'wc-block-top-rated-products': true,
[ `cols-${ columns }` ]: columns,
'is-loading': ! loaded,
'is-not-found': loaded && ! products.length,
'is-hidden-title': ! contentVisibility.title,
'is-hidden-price': ! contentVisibility.price,
'is-hidden-rating': ! contentVisibility.rating,
'is-hidden-button': ! contentVisibility.button,
} );
2018-12-14 19:45:19 +00:00
return (
<Fragment>
{ this.getInspectorControls() }
<div className={ classes }>
2018-12-14 19:45:19 +00:00
{ products.length ? (
products.map( ( product ) => (
<ProductPreview product={ product } key={ product.id } />
) )
) : (
<Placeholder
icon={ <Gridicon icon="trophy" /> }
label={ __( 'Top Rated Products', 'woo-gutenberg-products-block' ) }
2018-12-14 19:45:19 +00:00
>
{ ! loaded ? (
<Spinner />
) : (
__( 'No products found.', 'woo-gutenberg-products-block' )
) }
</Placeholder>
) }
</div>
</Fragment>
);
}
}
ProductTopRatedBlock.propTypes = {
/**
* The attributes for this block
*/
attributes: PropTypes.object.isRequired,
/**
* The register block name.
*/
name: PropTypes.string.isRequired,
/**
* A callback to update attributes
*/
setAttributes: PropTypes.func.isRequired,
};
export default ProductTopRatedBlock;