2018-12-18 19:45:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-01-10 18:16:37 +00:00
|
|
|
import { InspectorControls } from '@wordpress/editor';
|
2018-12-18 19:45:49 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-12-19 22:18:13 +00:00
|
|
|
import { debounce } from 'lodash';
|
2018-12-18 19:45:49 +00:00
|
|
|
import {
|
|
|
|
PanelBody,
|
|
|
|
Placeholder,
|
|
|
|
RangeControl,
|
|
|
|
Spinner,
|
|
|
|
} from '@wordpress/components';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-01-10 18:16:37 +00:00
|
|
|
import getQuery from '../../utils/get-query';
|
|
|
|
import { IconNewReleases } from '../../components/icons';
|
|
|
|
import ProductCategoryControl from '../../components/product-category-control';
|
|
|
|
import ProductPreview from '../../components/product-preview';
|
2018-12-18 19:45:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "Newest Products".
|
|
|
|
*/
|
|
|
|
class ProductNewestBlock 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-18 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if ( this.props.attributes.categories ) {
|
|
|
|
this.getProducts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps ) {
|
2019-01-15 18:52:34 +00:00
|
|
|
const hasChange = [ 'rows', 'columns', 'categories', 'catOperator' ].reduce( ( acc, key ) => {
|
2018-12-18 19:45:49 +00:00
|
|
|
return acc || prevProps.attributes[ key ] !== this.props.attributes[ key ];
|
|
|
|
}, false );
|
|
|
|
if ( hasChange ) {
|
2018-12-19 22:18:13 +00:00
|
|
|
this.debouncedGetProducts();
|
2018-12-18 19:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getProducts() {
|
|
|
|
apiFetch( {
|
2018-12-19 22:18:13 +00:00
|
|
|
path: addQueryArgs(
|
|
|
|
'/wc-pb/v3/products',
|
|
|
|
getQuery( this.props.attributes, this.props.name )
|
|
|
|
),
|
2018-12-18 19:45:49 +00:00
|
|
|
} )
|
|
|
|
.then( ( products ) => {
|
|
|
|
this.setState( { products, loaded: true } );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
this.setState( { products: [], loaded: true } );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getInspectorControls() {
|
|
|
|
const { attributes, setAttributes } = this.props;
|
2019-01-15 18:52:34 +00:00
|
|
|
const { categories, catOperator, columns, rows } = attributes;
|
2018-12-18 19:45:49 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Layout', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen
|
|
|
|
>
|
|
|
|
<RangeControl
|
|
|
|
label={ __( 'Columns', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ columns }
|
|
|
|
onChange={ ( value ) => setAttributes( { columns: value } ) }
|
|
|
|
min={ wc_product_block_data.min_columns }
|
|
|
|
max={ wc_product_block_data.max_columns }
|
|
|
|
/>
|
|
|
|
<RangeControl
|
|
|
|
label={ __( 'Rows', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ rows }
|
|
|
|
onChange={ ( value ) => setAttributes( { rows: value } ) }
|
|
|
|
min={ wc_product_block_data.min_rows }
|
|
|
|
max={ wc_product_block_data.max_rows }
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Filter by Product Category',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
initialOpen={ false }
|
|
|
|
>
|
|
|
|
<ProductCategoryControl
|
2019-01-15 18:52:34 +00:00
|
|
|
selected={ categories }
|
2018-12-18 19:45:49 +00:00
|
|
|
onChange={ ( value = [] ) => {
|
|
|
|
const ids = value.map( ( { id } ) => id );
|
|
|
|
setAttributes( { categories: ids } );
|
|
|
|
} }
|
2019-01-15 18:52:34 +00:00
|
|
|
operator={ catOperator }
|
|
|
|
onOperatorChange={ ( value = 'any' ) =>
|
|
|
|
setAttributes( { catOperator: value } )
|
|
|
|
}
|
2018-12-18 19:45:49 +00:00
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-01-10 18:16:37 +00:00
|
|
|
const { columns } = this.props.attributes;
|
2018-12-18 19:45:49 +00:00
|
|
|
const { loaded, products } = this.state;
|
|
|
|
const classes = [ 'wc-block-products-grid', 'wc-block-newest-products' ];
|
|
|
|
if ( columns ) {
|
|
|
|
classes.push( `cols-${ columns }` );
|
|
|
|
}
|
|
|
|
if ( products && ! products.length ) {
|
|
|
|
if ( ! loaded ) {
|
|
|
|
classes.push( 'is-loading' );
|
|
|
|
} else {
|
|
|
|
classes.push( 'is-not-found' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ this.getInspectorControls() }
|
|
|
|
<div className={ classes.join( ' ' ) }>
|
|
|
|
{ products.length ? (
|
|
|
|
products.map( ( product ) => (
|
|
|
|
<ProductPreview product={ product } key={ product.id } />
|
|
|
|
) )
|
|
|
|
) : (
|
|
|
|
<Placeholder
|
2018-12-22 00:10:17 +00:00
|
|
|
icon={ <IconNewReleases /> }
|
2018-12-19 22:18:13 +00:00
|
|
|
label={ __( 'Newest Products', 'woo-gutenberg-products-block' ) }
|
2018-12-18 19:45:49 +00:00
|
|
|
>
|
|
|
|
{ ! loaded ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
__( 'No products found.', 'woo-gutenberg-products-block' )
|
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProductNewestBlock.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 ProductNewestBlock;
|