2018-12-14 23:47:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-02-21 19:00:47 +00:00
|
|
|
import classnames from 'classnames';
|
2018-12-14 23:47:16 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-12-19 22:18:13 +00:00
|
|
|
import { debounce } from 'lodash';
|
2018-12-14 23:47:16 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2019-02-21 19:00:47 +00:00
|
|
|
import { InspectorControls } from '@wordpress/editor';
|
2019-01-31 22:55:54 +00:00
|
|
|
import { PanelBody, Placeholder, Spinner } from '@wordpress/components';
|
2018-12-14 23:47:16 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-01-10 18:16:37 +00:00
|
|
|
import getQuery from '../../utils/get-query';
|
2019-02-21 19:00:47 +00:00
|
|
|
import GridContentControl from '../../components/grid-content-control';
|
2019-01-31 22:55:54 +00:00
|
|
|
import GridLayoutControl from '../../components/grid-layout-control';
|
2019-01-10 18:16:37 +00:00
|
|
|
import ProductCategoryControl from '../../components/product-category-control';
|
|
|
|
import ProductOrderbyControl from '../../components/product-orderby-control';
|
|
|
|
import ProductPreview from '../../components/product-preview';
|
2018-12-14 23:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "On Sale Products".
|
|
|
|
*/
|
|
|
|
class ProductOnSaleBlock extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
products: [],
|
|
|
|
loaded: false,
|
|
|
|
};
|
2018-12-19 22:18:13 +00:00
|
|
|
|
|
|
|
this.debouncedGetProducts = debounce( this.getProducts.bind( this ), 200 );
|
2018-12-14 23:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getProducts();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps ) {
|
2018-12-17 20:16:01 +00:00
|
|
|
const hasChange = [
|
|
|
|
'categories',
|
|
|
|
'catOperator',
|
|
|
|
'columns',
|
|
|
|
'orderby',
|
|
|
|
'rows',
|
|
|
|
].reduce( ( acc, key ) => {
|
|
|
|
return acc || prevProps.attributes[ key ] !== this.props.attributes[ key ];
|
|
|
|
}, false );
|
2018-12-14 23:47:16 +00:00
|
|
|
if ( hasChange ) {
|
2018-12-19 22:18:13 +00:00
|
|
|
this.debouncedGetProducts();
|
2018-12-14 23:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getProducts() {
|
|
|
|
apiFetch( {
|
|
|
|
path: addQueryArgs(
|
2019-02-25 20:23:29 +00:00
|
|
|
'/wc-blocks/v1/products',
|
2018-12-14 23:47:16 +00:00
|
|
|
getQuery( this.props.attributes, this.props.name )
|
|
|
|
),
|
|
|
|
} )
|
|
|
|
.then( ( products ) => {
|
|
|
|
this.setState( { products, loaded: true } );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
this.setState( { products: [], loaded: true } );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getInspectorControls() {
|
|
|
|
const { attributes, setAttributes } = this.props;
|
2019-02-21 19:00:47 +00:00
|
|
|
const {
|
|
|
|
categories,
|
|
|
|
catOperator,
|
|
|
|
columns,
|
|
|
|
contentVisibility,
|
|
|
|
rows,
|
|
|
|
orderby,
|
|
|
|
} = attributes;
|
2018-12-14 23:47:16 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Layout', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen
|
|
|
|
>
|
2019-01-31 22:55:54 +00:00
|
|
|
<GridLayoutControl
|
|
|
|
columns={ columns }
|
|
|
|
rows={ rows }
|
|
|
|
setAttributes={ setAttributes }
|
2018-12-14 23:47:16 +00:00
|
|
|
/>
|
|
|
|
</PanelBody>
|
2019-02-21 19:00:47 +00:00
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Content', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen
|
|
|
|
>
|
|
|
|
<GridContentControl
|
|
|
|
settings={ contentVisibility }
|
|
|
|
onChange={ ( value ) => setAttributes( { contentVisibility: value } ) }
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2018-12-14 23:47:16 +00:00
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Order By', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen={ false }
|
|
|
|
>
|
2018-12-17 20:16:01 +00:00
|
|
|
<ProductOrderbyControl
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
value={ orderby }
|
|
|
|
/>
|
2018-12-14 23:47:16 +00:00
|
|
|
</PanelBody>
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Filter by Product Category',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
initialOpen={ false }
|
|
|
|
>
|
|
|
|
<ProductCategoryControl
|
2018-12-17 20:16:01 +00:00
|
|
|
selected={ categories }
|
2018-12-14 23:47:16 +00:00
|
|
|
onChange={ ( value = [] ) => {
|
|
|
|
const ids = value.map( ( { id } ) => id );
|
|
|
|
setAttributes( { categories: ids } );
|
|
|
|
} }
|
2018-12-17 20:16:01 +00:00
|
|
|
operator={ catOperator }
|
|
|
|
onOperatorChange={ ( value = 'any' ) =>
|
|
|
|
setAttributes( { catOperator: value } )
|
|
|
|
}
|
2018-12-14 23:47:16 +00:00
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-02-21 19:00:47 +00:00
|
|
|
const { columns, contentVisibility } = this.props.attributes;
|
|
|
|
const { loaded, products = [] } = this.state;
|
|
|
|
const classes = classnames( {
|
|
|
|
'wc-block-products-grid': true,
|
|
|
|
'wc-block-on-sale-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-button': ! contentVisibility.button,
|
|
|
|
} );
|
2018-12-14 23:47:16 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ this.getInspectorControls() }
|
2019-02-21 19:00:47 +00:00
|
|
|
<div className={ classes }>
|
2018-12-14 23:47:16 +00:00
|
|
|
{ products.length ? (
|
|
|
|
products.map( ( product ) => (
|
|
|
|
<ProductPreview product={ product } key={ product.id } />
|
|
|
|
) )
|
|
|
|
) : (
|
|
|
|
<Placeholder
|
|
|
|
icon={ <Gridicon icon="tag" /> }
|
2018-12-17 20:16:01 +00:00
|
|
|
label={ __( 'On Sale Products', 'woo-gutenberg-products-block' ) }
|
2018-12-14 23:47:16 +00:00
|
|
|
>
|
|
|
|
{ ! loaded ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
__( 'No products found.', 'woo-gutenberg-products-block' )
|
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductOnSaleBlock.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 ProductOnSaleBlock;
|