2018-12-14 23:47:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2020-01-10 14:40:15 +00:00
|
|
|
import { Disabled, PanelBody, Placeholder } from '@wordpress/components';
|
2020-05-12 20:31:49 +00:00
|
|
|
import { InspectorControls } from '@wordpress/block-editor';
|
|
|
|
import { ServerSideRender } from '@wordpress/editor';
|
2018-12-14 23:47:16 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-09-02 08:21:46 +00:00
|
|
|
import GridContentControl from '@woocommerce/editor-components/grid-content-control';
|
|
|
|
import GridLayoutControl from '@woocommerce/editor-components/grid-layout-control';
|
|
|
|
import ProductCategoryControl from '@woocommerce/editor-components/product-category-control';
|
|
|
|
import ProductOrderbyControl from '@woocommerce/editor-components/product-orderby-control';
|
2019-11-15 15:16:18 +00:00
|
|
|
import { gridBlockPreview } from '@woocommerce/resource-previews';
|
2020-01-31 18:20:33 +00:00
|
|
|
import { Icon, tag } from '@woocommerce/icons';
|
2020-01-10 14:40:15 +00:00
|
|
|
|
|
|
|
const EmptyPlaceholder = () => (
|
|
|
|
<Placeholder
|
2020-01-31 18:20:33 +00:00
|
|
|
icon={ <Icon srcElement={ tag } /> }
|
2020-01-10 14:40:15 +00:00
|
|
|
label={ __( 'On Sale Products', 'woo-gutenberg-products-block' ) }
|
|
|
|
className="wc-block-product-on-sale"
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'This block shows on-sale products. There are currently no discounted products in your store.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
);
|
2018-12-14 23:47:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "On Sale Products".
|
|
|
|
*/
|
|
|
|
class ProductOnSaleBlock extends Component {
|
|
|
|
getInspectorControls() {
|
|
|
|
const { attributes, setAttributes } = this.props;
|
2019-02-21 19:00:47 +00:00
|
|
|
const {
|
|
|
|
categories,
|
|
|
|
catOperator,
|
|
|
|
columns,
|
|
|
|
contentVisibility,
|
|
|
|
rows,
|
|
|
|
orderby,
|
2019-06-27 11:13:02 +00:00
|
|
|
alignButtons,
|
2019-02-21 19:00:47 +00:00
|
|
|
} = 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 }
|
2019-06-27 11:13:02 +00:00
|
|
|
alignButtons={ alignButtons }
|
2019-01-31 22:55:54 +00:00
|
|
|
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 }
|
2019-09-05 15:09:31 +00:00
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( { contentVisibility: value } )
|
|
|
|
}
|
2019-02-21 19:00:47 +00:00
|
|
|
/>
|
|
|
|
</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-06-11 13:50:42 +00:00
|
|
|
const { attributes, name } = this.props;
|
2018-12-14 23:47:16 +00:00
|
|
|
|
2019-11-15 15:16:18 +00:00
|
|
|
if ( attributes.isPreview ) {
|
|
|
|
return gridBlockPreview;
|
|
|
|
}
|
|
|
|
|
2018-12-14 23:47:16 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ this.getInspectorControls() }
|
2019-05-21 20:34:48 +00:00
|
|
|
<Disabled>
|
2019-09-09 10:52:48 +00:00
|
|
|
<ServerSideRender
|
|
|
|
block={ name }
|
|
|
|
attributes={ attributes }
|
2020-01-10 14:40:15 +00:00
|
|
|
EmptyResponsePlaceholder={ EmptyPlaceholder }
|
2019-09-09 10:52:48 +00:00
|
|
|
/>
|
2019-05-21 20:34:48 +00:00
|
|
|
</Disabled>
|
2018-12-14 23:47:16 +00:00
|
|
|
</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;
|