/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { AlignmentToolbar, BlockControls, InnerBlocks, InspectorControls, MediaUpload, MediaUploadCheck, PanelColorSettings, withColors, RichText, } from '@wordpress/block-editor'; import { withSelect } from '@wordpress/data'; import { Button, FocalPointPicker, IconButton, PanelBody, Placeholder, RangeControl, ResizableBox, Spinner, ToggleControl, Toolbar, withSpokenMessages, } from '@wordpress/components'; import classnames from 'classnames'; import { Fragment, Component } from '@wordpress/element'; import { compose, createHigherOrderComponent } from '@wordpress/compose'; import { isEmpty } from 'lodash'; import PropTypes from 'prop-types'; import { MIN_HEIGHT } from '@woocommerce/block-settings'; import ProductControl from '@woocommerce/editor-components/product-control'; import ErrorPlaceholder from '@woocommerce/editor-components/error-placeholder'; import { withProduct } from '@woocommerce/block-hocs'; import { Icon, star } from '@woocommerce/icons'; /** * Internal dependencies */ import { dimRatioToClass, getBackgroundImageStyles } from './utils'; import { getImageSrcFromProduct, getImageIdFromProduct, } from '../../utils/products'; /** * Component to handle edit mode of "Featured Product". */ const FeaturedProduct = ( { attributes, debouncedSpeak, error, getProduct, isLoading, isSelected, overlayColor, product, setAttributes, setOverlayColor, triggerUrlUpdate = () => void null, } ) => { const renderApiError = () => ( ); const renderEditMode = () => { const onDone = () => { setAttributes( { editMode: false } ); debouncedSpeak( __( 'Showing Featured Product block preview.', 'woo-gutenberg-products-block' ) ); }; return ( { getBlockControls() } } label={ __( 'Featured Product', 'woo-gutenberg-products-block' ) } className="wc-block-featured-product" > { __( 'Visually highlight a product or variation and encourage prompt action', 'woo-gutenberg-products-block' ) }
{ const id = value[ 0 ] ? value[ 0 ].id : 0; setAttributes( { productId: id, mediaId: 0, mediaSrc: '', } ); triggerUrlUpdate(); } } />
); }; const getBlockControls = () => { const { contentAlign, editMode } = attributes; const mediaId = attributes.mediaId || getImageIdFromProduct( product ); return ( { setAttributes( { contentAlign: nextAlign } ); } } /> { setAttributes( { mediaId: media.id, mediaSrc: media.url, } ); } } allowedTypes={ [ 'image' ] } value={ mediaId } render={ ( { open } ) => ( ) } /> setAttributes( { editMode: ! editMode } ), isActive: editMode, }, ] } /> ); }; const getInspectorControls = () => { const url = attributes.mediaSrc || getImageSrcFromProduct( product ); const { focalPoint = { x: 0.5, y: 0.5 } } = attributes; // FocalPointPicker was introduced in Gutenberg 5.0 (WordPress 5.2), // so we need to check if it exists before using it. const focalPointPickerExists = typeof FocalPointPicker === 'function'; return ( setAttributes( { showDesc: ! attributes.showDesc } ) } /> setAttributes( { showPrice: ! attributes.showPrice } ) } /> { !! url && ( setAttributes( { dimRatio: ratio } ) } min={ 0 } max={ 100 } step={ 10 } /> { focalPointPickerExists && ( setAttributes( { focalPoint: value } ) } /> ) } ) } ); }; const renderProduct = () => { const { className, contentAlign, dimRatio, focalPoint, height, showDesc, showPrice, } = attributes; const classes = classnames( 'wc-block-featured-product', { 'is-selected': isSelected && attributes.productId !== 'preview', 'is-loading': ! product && isLoading, 'is-not-found': ! product && ! isLoading, 'has-background-dim': dimRatio !== 0, }, dimRatioToClass( dimRatio ), contentAlign !== 'center' && `has-${ contentAlign }-content`, className ); const style = getBackgroundImageStyles( attributes.mediaSrc || product ); if ( overlayColor.color ) { style.backgroundColor = overlayColor.color; } if ( focalPoint ) { const bgPosX = focalPoint.x * 100; const bgPosY = focalPoint.y * 100; style.backgroundPosition = `${ bgPosX }% ${ bgPosY }%`; } const onResizeStop = ( event, direction, elt ) => { setAttributes( { height: parseInt( elt.style.height, 10 ) } ); }; return (

{ ! isEmpty( product.variation ) && (

) } { showDesc && (
) } { showPrice && (
) }
{ renderButton() }
); }; const renderButton = () => { const buttonClasses = classnames( 'wp-block-button__link', 'is-style-fill' ); const buttonStyle = { backgroundColor: 'vivid-green-cyan', borderRadius: '5px', }; const wrapperStyle = { width: '100%', }; return attributes.productId === 'preview' ? (
) : ( ); }; const renderNoProduct = () => ( } label={ __( 'Featured Product', 'woo-gutenberg-products-block' ) } > { isLoading ? ( ) : ( __( 'No product is selected.', 'woo-gutenberg-products-block' ) ) } ); const { editMode } = attributes; if ( error ) { return renderApiError(); } if ( editMode ) { return renderEditMode(); } return ( { getBlockControls() } { getInspectorControls() } { product ? renderProduct() : renderNoProduct() } ); }; FeaturedProduct.propTypes = { /** * The attributes for this block. */ attributes: PropTypes.object.isRequired, /** * Whether this block is currently active. */ isSelected: PropTypes.bool.isRequired, /** * The register block name. */ name: PropTypes.string.isRequired, /** * A callback to update attributes. */ setAttributes: PropTypes.func.isRequired, // from withProduct error: PropTypes.object, getProduct: PropTypes.func, isLoading: PropTypes.bool, product: PropTypes.shape( { name: PropTypes.node, variation: PropTypes.node, description: PropTypes.node, price_html: PropTypes.node, permalink: PropTypes.string, } ), // from withColors overlayColor: PropTypes.object, setOverlayColor: PropTypes.func.isRequired, // from withSpokenMessages debouncedSpeak: PropTypes.func.isRequired, triggerUrlUpdate: PropTypes.func, }; export default compose( [ withProduct, withColors( { overlayColor: 'background-color' } ), withSpokenMessages, withSelect( ( select, { clientId }, { dispatch } ) => { const Block = select( 'core/block-editor' ).getBlock( clientId ); const buttonBlockId = Block?.innerBlocks[ 0 ]?.clientId || ''; const currentButtonAttributes = Block?.innerBlocks[ 0 ]?.attributes || {}; const updateBlockAttributes = ( attributes ) => { if ( buttonBlockId ) { dispatch( 'core/block-editor' ).updateBlockAttributes( buttonBlockId, attributes ); } }; return { updateBlockAttributes, currentButtonAttributes }; } ), createHigherOrderComponent( ( ProductComponent ) => { class WrappedComponent extends Component { state = { doUrlUpdate: false, }; componentDidUpdate() { const { attributes, updateBlockAttributes, currentButtonAttributes, product, } = this.props; if ( this.state.doUrlUpdate && ! attributes.editMode && product?.permalink && currentButtonAttributes?.url && product.permalink !== currentButtonAttributes.url ) { updateBlockAttributes( { ...currentButtonAttributes, url: product.permalink, } ); this.setState( { doUrlUpdate: false } ); } } triggerUrlUpdate = () => { this.setState( { doUrlUpdate: true } ); }; render() { return ( ); } } return WrappedComponent; }, 'withUpdateButtonAttributes' ), ] )( FeaturedProduct );