/* eslint-disable @wordpress/no-unsafe-wp-apis */ /** * External dependencies */ import { __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor'; import { Icon, Placeholder, Spinner } from '@wordpress/components'; import classnames from 'classnames'; import { isEmpty } from 'lodash'; import { useCallback, useState } from 'react'; /** * Internal dependencies */ import { CallToAction } from './call-to-action'; import { ConstrainedResizable } from './constrained-resizable'; import { useBackgroundImage } from './use-background-image'; import { dimRatioToClass, getBackgroundImageStyles, getClassPrefixFromName, } from './utils'; export const withFeaturedItem = ( { emptyMessage, icon, label } ) => ( Component ) => ( props ) => { const [ isEditingImage ] = props.useEditingImage; const { attributes, category, isLoading, isSelected, name, product, setAttributes, } = props; const { mediaId, mediaSrc } = attributes; const item = category || product; const [ backgroundImageSize, setBackgroundImageSize ] = useState( {} ); const { backgroundImageSrc } = useBackgroundImage( { item, mediaId, mediaSrc, blockName: name, } ); const className = getClassPrefixFromName( name ); const onResize = useCallback( ( _event, _direction, elt ) => { setAttributes( { minHeight: parseInt( elt.style.height, 10 ) } ); }, [ setAttributes ] ); const renderButton = () => { const { categoryId, linkText, productId } = attributes; return ( ); }; const renderNoItem = () => ( } label={ label } > { isLoading ? : emptyMessage } ); const renderItem = () => { const { contentAlign, dimRatio, focalPoint, hasParallax, isRepeated, imageFit, minHeight, overlayColor, overlayGradient, showDesc, showPrice, style, } = attributes; const classes = classnames( className, { 'is-selected': isSelected && attributes.categoryId !== 'preview' && attributes.productId !== 'preview', 'is-loading': ! item && isLoading, 'is-not-found': ! item && ! isLoading, 'has-background-dim': dimRatio !== 0, 'is-repeated': isRepeated, }, dimRatioToClass( dimRatio ), contentAlign !== 'center' && `has-${ contentAlign }-content` ); const containerStyle = { borderRadius: style?.border?.radius, }; const wrapperStyle = { ...getSpacingClassesAndStyles( attributes ).style, minHeight, }; const isImgElement = ! isRepeated && ! hasParallax; const backgroundImageStyle = getBackgroundImageStyles( { focalPoint, imageFit, isImgElement, isRepeated, url: backgroundImageSrc, } ); const overlayStyle = { background: overlayGradient, backgroundColor: overlayColor, }; return ( <>
{ backgroundImageSrc && ( isImgElement ? ( { { setBackgroundImageSize( { height: e.target?.naturalHeight, width: e.target?.naturalWidth, } ); } } /> ) : (
) ) }

{ ! isEmpty( product?.variation ) && (

) } { showDesc && (
) } { showPrice && (
) }
{ renderButton() }
); }; if ( isEditingImage ) { return ( ); } return ( <> { item ? renderItem() : renderNoItem() } ); };