2019-01-10 19:01:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2019-01-13 17:22:15 +00:00
|
|
|
import {
|
|
|
|
AlignmentToolbar,
|
|
|
|
BlockControls,
|
|
|
|
InspectorControls,
|
2019-01-14 22:22:00 +00:00
|
|
|
MediaUpload,
|
|
|
|
MediaUploadCheck,
|
2019-01-13 17:22:15 +00:00
|
|
|
PanelColorSettings,
|
|
|
|
RichText,
|
|
|
|
withColors,
|
|
|
|
} from '@wordpress/editor';
|
2019-01-10 19:01:49 +00:00
|
|
|
import {
|
|
|
|
Button,
|
2019-01-14 22:22:00 +00:00
|
|
|
IconButton,
|
2019-01-10 19:01:49 +00:00
|
|
|
PanelBody,
|
|
|
|
Placeholder,
|
2019-01-13 17:22:15 +00:00
|
|
|
RangeControl,
|
2019-01-10 19:01:49 +00:00
|
|
|
Spinner,
|
2019-01-13 17:22:15 +00:00
|
|
|
ToggleControl,
|
2019-01-10 19:01:49 +00:00
|
|
|
Toolbar,
|
|
|
|
withSpokenMessages,
|
|
|
|
} from '@wordpress/components';
|
2019-01-13 17:22:15 +00:00
|
|
|
import classnames from 'classnames';
|
2019-01-10 19:01:49 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-01-13 17:22:15 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-01-14 22:22:00 +00:00
|
|
|
import { debounce, isObject } from 'lodash';
|
2019-01-10 19:01:49 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ProductControl from '../../components/product-control';
|
2019-01-14 22:22:00 +00:00
|
|
|
import {
|
|
|
|
getImageSrcFromProduct,
|
|
|
|
getImageIdFromProduct,
|
|
|
|
} from '../../utils/products';
|
2019-01-13 17:22:15 +00:00
|
|
|
|
2019-01-14 22:22:00 +00:00
|
|
|
/**
|
|
|
|
* Generate a style object given either a product object or URL to an image.
|
|
|
|
*
|
|
|
|
* @param {object|string} url A product object as returned from the API, or an image URL.
|
|
|
|
* @return {object} A style object with a backgroundImage set (if a valid image is provided).
|
|
|
|
*/
|
|
|
|
function backgroundImageStyles( url ) {
|
|
|
|
// If `url` is an object, it's actually a product.
|
|
|
|
if ( isObject( url ) ) {
|
|
|
|
url = getImageSrcFromProduct( url );
|
|
|
|
}
|
|
|
|
if ( url ) {
|
2019-01-13 17:22:15 +00:00
|
|
|
return { backgroundImage: `url(${ url })` };
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-14 22:22:00 +00:00
|
|
|
/**
|
|
|
|
* Convert the selected ratio to the correct background class.
|
|
|
|
*
|
|
|
|
* @param {number} ratio Selected opacity from 0 to 100.
|
|
|
|
* @return {string} The class name, if applicable (not used for ratio 0 or 50).
|
|
|
|
*/
|
2019-01-13 17:22:15 +00:00
|
|
|
function dimRatioToClass( ratio ) {
|
|
|
|
return ratio === 0 || ratio === 50 ?
|
|
|
|
null :
|
|
|
|
`has-background-dim-${ 10 * Math.round( ratio / 10 ) }`;
|
|
|
|
}
|
2019-01-10 19:01:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "Featured Product".
|
|
|
|
*/
|
|
|
|
class FeaturedProduct extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
this.state = {
|
|
|
|
product: false,
|
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.debouncedGetProduct = debounce( this.getProduct.bind( this ), 200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getProduct();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps ) {
|
|
|
|
if ( prevProps.attributes.productId !== this.props.attributes.productId ) {
|
|
|
|
this.debouncedGetProduct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getProduct() {
|
|
|
|
const { productId } = this.props.attributes;
|
|
|
|
if ( ! productId ) {
|
|
|
|
// We've removed the selected product, or no product is selected yet.
|
|
|
|
this.setState( { product: false, loaded: true } );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
apiFetch( {
|
|
|
|
path: `/wc-pb/v3/products/${ productId }`,
|
|
|
|
} )
|
|
|
|
.then( ( product ) => {
|
|
|
|
this.setState( { product, loaded: true } );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
this.setState( { product: false, loaded: true } );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getInspectorControls() {
|
2019-01-13 17:22:15 +00:00
|
|
|
const {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
overlayColor,
|
|
|
|
setOverlayColor,
|
|
|
|
} = this.props;
|
2019-01-10 19:01:49 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<InspectorControls key="inspector">
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Product', 'woo-gutenberg-products-block' ) }
|
|
|
|
initialOpen={ false }
|
|
|
|
>
|
|
|
|
<ProductControl
|
|
|
|
selected={ attributes.productId || 0 }
|
|
|
|
onChange={ ( value = [] ) => {
|
|
|
|
const id = value[ 0 ] ? value[ 0 ].id : 0;
|
2019-01-14 22:22:00 +00:00
|
|
|
setAttributes( { productId: id, mediaId: 0, mediaSrc: '' } );
|
2019-01-10 19:01:49 +00:00
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2019-01-13 17:22:15 +00:00
|
|
|
<PanelBody title={ __( 'Content', 'woo-gutenberg-products-block' ) }>
|
|
|
|
<ToggleControl
|
|
|
|
label="Show description"
|
|
|
|
checked={ attributes.showDesc }
|
|
|
|
onChange={ () => setAttributes( { showDesc: ! attributes.showDesc } ) }
|
|
|
|
/>
|
|
|
|
<ToggleControl
|
|
|
|
label="Show price"
|
|
|
|
checked={ attributes.showPrice }
|
|
|
|
onChange={ () => setAttributes( { showPrice: ! attributes.showPrice } ) }
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelColorSettings
|
|
|
|
title={ __( 'Overlay', 'woo-gutenberg-products-block' ) }
|
|
|
|
colorSettings={ [
|
|
|
|
{
|
|
|
|
value: overlayColor.color,
|
|
|
|
onChange: setOverlayColor,
|
|
|
|
label: __( 'Overlay Color', 'woo-gutenberg-products-block' ),
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
>
|
|
|
|
<RangeControl
|
|
|
|
label={ __( 'Background Opacity', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ attributes.dimRatio }
|
|
|
|
onChange={ ( ratio ) => setAttributes( { dimRatio: ratio } ) }
|
|
|
|
min={ 0 }
|
|
|
|
max={ 100 }
|
|
|
|
step={ 10 }
|
|
|
|
/>
|
|
|
|
</PanelColorSettings>
|
2019-01-10 19:01:49 +00:00
|
|
|
</InspectorControls>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderEditMode() {
|
|
|
|
const { attributes, debouncedSpeak, setAttributes } = this.props;
|
|
|
|
const onDone = () => {
|
|
|
|
setAttributes( { editMode: false } );
|
|
|
|
debouncedSpeak(
|
|
|
|
__(
|
|
|
|
'Showing Featured Product block preview.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Placeholder
|
2019-01-14 19:37:35 +00:00
|
|
|
icon="star-filled"
|
2019-01-10 19:01:49 +00:00
|
|
|
label={ __( 'Featured Product', 'woo-gutenberg-products-block' ) }
|
|
|
|
className="wc-block-featured-product"
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Visually highlight a product and encourage prompt action',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
<div className="wc-block-handpicked-products__selection">
|
|
|
|
<ProductControl
|
|
|
|
selected={ attributes.productId || 0 }
|
|
|
|
onChange={ ( value = [] ) => {
|
|
|
|
const id = value[ 0 ] ? value[ 0 ].id : 0;
|
2019-01-14 22:22:00 +00:00
|
|
|
setAttributes( { productId: id, mediaId: 0, mediaSrc: '' } );
|
2019-01-10 19:01:49 +00:00
|
|
|
} }
|
|
|
|
/>
|
|
|
|
<Button isDefault onClick={ onDone }>
|
|
|
|
{ __( 'Done', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Placeholder>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-01-13 17:22:15 +00:00
|
|
|
const { attributes, setAttributes, overlayColor } = this.props;
|
|
|
|
const {
|
|
|
|
contentAlign,
|
|
|
|
dimRatio,
|
|
|
|
editMode,
|
|
|
|
linkText,
|
|
|
|
showDesc,
|
|
|
|
showPrice,
|
|
|
|
} = attributes;
|
2019-01-10 19:01:49 +00:00
|
|
|
const { loaded, product } = this.state;
|
2019-01-13 17:22:15 +00:00
|
|
|
const classes = classnames(
|
|
|
|
'wc-block-featured-product',
|
|
|
|
{
|
|
|
|
'is-loading': ! product && ! loaded,
|
|
|
|
'is-not-found': ! product && loaded,
|
|
|
|
'has-background-dim': dimRatio !== 0,
|
|
|
|
},
|
|
|
|
dimRatioToClass( dimRatio ),
|
|
|
|
contentAlign !== 'center' && `has-${ contentAlign }-content`
|
|
|
|
);
|
2019-01-14 22:22:00 +00:00
|
|
|
const mediaId = attributes.mediaId || getImageIdFromProduct( product );
|
2019-01-13 17:22:15 +00:00
|
|
|
|
2019-01-14 22:22:00 +00:00
|
|
|
const style = !! product ?
|
|
|
|
backgroundImageStyles( attributes.mediaSrc || product ) :
|
|
|
|
{};
|
2019-01-13 17:22:15 +00:00
|
|
|
if ( overlayColor.color ) {
|
|
|
|
style.backgroundColor = overlayColor.color;
|
2019-01-10 19:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<BlockControls>
|
2019-01-13 17:22:15 +00:00
|
|
|
<AlignmentToolbar
|
|
|
|
value={ contentAlign }
|
|
|
|
onChange={ ( nextAlign ) => {
|
|
|
|
setAttributes( { contentAlign: nextAlign } );
|
|
|
|
} }
|
|
|
|
/>
|
2019-01-10 19:01:49 +00:00
|
|
|
<Toolbar
|
|
|
|
controls={ [
|
|
|
|
{
|
|
|
|
icon: 'edit',
|
|
|
|
title: __( 'Edit' ),
|
|
|
|
onClick: () => setAttributes( { editMode: ! editMode } ),
|
|
|
|
isActive: editMode,
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
2019-01-14 22:22:00 +00:00
|
|
|
<MediaUploadCheck>
|
|
|
|
<Toolbar>
|
|
|
|
<MediaUpload
|
|
|
|
onSelect={ ( media ) => {
|
|
|
|
setAttributes( { mediaId: media.id, mediaSrc: media.url } );
|
|
|
|
} }
|
|
|
|
allowedTypes={ [ 'image' ] }
|
|
|
|
value={ mediaId }
|
|
|
|
render={ ( { open } ) => (
|
|
|
|
<IconButton
|
|
|
|
className="components-toolbar__control"
|
|
|
|
label={ __( 'Edit media' ) }
|
|
|
|
icon="format-image"
|
|
|
|
onClick={ open }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</Toolbar>
|
|
|
|
</MediaUploadCheck>
|
2019-01-10 19:01:49 +00:00
|
|
|
</BlockControls>
|
2019-01-13 17:22:15 +00:00
|
|
|
{ ! attributes.editMode && this.getInspectorControls() }
|
2019-01-10 19:01:49 +00:00
|
|
|
{ editMode ? (
|
|
|
|
this.renderEditMode()
|
|
|
|
) : (
|
2019-01-13 17:22:15 +00:00
|
|
|
<Fragment>
|
2019-01-10 19:01:49 +00:00
|
|
|
{ !! product ? (
|
2019-01-13 17:22:15 +00:00
|
|
|
<div className={ classes } style={ style }>
|
|
|
|
<h2 className="wc-block-featured-product__title">
|
|
|
|
{ product.name }
|
|
|
|
</h2>
|
|
|
|
{ showDesc && (
|
|
|
|
<div
|
|
|
|
className="wc-block-featured-product__description"
|
|
|
|
dangerouslySetInnerHTML={ {
|
2019-01-14 19:53:30 +00:00
|
|
|
__html: product.short_description,
|
2019-01-13 17:22:15 +00:00
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ showPrice && (
|
|
|
|
<div
|
|
|
|
className="wc-block-featured-product__price"
|
|
|
|
dangerouslySetInnerHTML={ { __html: product.price_html } }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<div className="wc-block-featured-product__link wp-block-button">
|
|
|
|
<RichText
|
|
|
|
value={ linkText }
|
|
|
|
onChange={ ( value ) => setAttributes( { linkText: value } ) }
|
|
|
|
formattingControls={ [ 'bold', 'italic', 'strikethrough' ] }
|
|
|
|
className="wp-block-button__link"
|
|
|
|
keepPlaceholderOnFocus
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-10 19:01:49 +00:00
|
|
|
) : (
|
|
|
|
<Placeholder
|
2019-01-13 17:22:15 +00:00
|
|
|
className="wc-block-featured-product"
|
2019-01-14 19:37:35 +00:00
|
|
|
icon="star-filled"
|
2019-01-10 19:01:49 +00:00
|
|
|
label={ __( 'Featured Product', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
{ ! loaded ? (
|
|
|
|
<Spinner />
|
|
|
|
) : (
|
|
|
|
__( 'No product is selected.', 'woo-gutenberg-products-block' )
|
|
|
|
) }
|
|
|
|
</Placeholder>
|
|
|
|
) }
|
2019-01-13 17:22:15 +00:00
|
|
|
</Fragment>
|
2019-01-10 19:01:49 +00:00
|
|
|
) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FeaturedProduct.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,
|
2019-01-13 17:22:15 +00:00
|
|
|
// from withColors
|
|
|
|
overlayColor: PropTypes.object,
|
|
|
|
setOverlayColor: PropTypes.func.isRequired,
|
2019-01-10 19:01:49 +00:00
|
|
|
// from withSpokenMessages
|
|
|
|
debouncedSpeak: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2019-01-13 17:22:15 +00:00
|
|
|
export default compose( [
|
|
|
|
withColors( { overlayColor: 'background-color' } ),
|
|
|
|
withSpokenMessages,
|
|
|
|
] )( FeaturedProduct );
|