Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Enable re-select category for Featured Category block #4559

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 74 additions & 1 deletion assets/js/blocks/featured-category/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import {
ResizableBox,
Spinner,
ToggleControl,
ToolbarGroup,
withSpokenMessages,
} from '@wordpress/components';
import classnames from 'classnames';
import { compose } from '@wordpress/compose';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { compose, createHigherOrderComponent } from '@wordpress/compose';
import PropTypes from 'prop-types';
import { getSetting } from '@woocommerce/settings';
import { Icon, folderStarred } from '@woocommerce/icons';
Expand Down Expand Up @@ -56,6 +59,7 @@ import { withCategory } from '../../hocs';
* @param {Object} props.overlayColor Overlay color object for content.
* @param {function(any):any} props.setOverlayColor Setter for overlay color.
* @param {function(any):any} props.debouncedSpeak Function for delayed speak.
* @param {function(any):any} props.triggerUrlUpdate Function to update Shop now button Url.
thisissandip marked this conversation as resolved.
Show resolved Hide resolved
*/
const FeaturedCategory = ( {
attributes,
Expand All @@ -68,6 +72,7 @@ const FeaturedCategory = ( {
overlayColor,
setOverlayColor,
debouncedSpeak,
triggerUrlUpdate = () => void null,
} ) => {
const renderApiError = () => (
<ErrorPlaceholder
Expand Down Expand Up @@ -102,6 +107,17 @@ const FeaturedCategory = ( {
} }
allowedTypes={ [ 'image' ] }
/>
<ToolbarGroup
controls={ [
{
icon: 'edit',
title: __( 'Edit' ),
onClick: () =>
setAttributes( { editMode: ! editMode } ),
isActive: editMode,
},
] }
/>
</BlockControls>
);
};
Expand Down Expand Up @@ -208,6 +224,7 @@ const FeaturedCategory = ( {
mediaId: 0,
mediaSrc: '',
} );
triggerUrlUpdate();
} }
isSingle
/>
Expand Down Expand Up @@ -398,10 +415,66 @@ FeaturedCategory.propTypes = {
setOverlayColor: PropTypes.func.isRequired,
// from withSpokenMessages
debouncedSpeak: PropTypes.func.isRequired,
triggerUrlUpdate: PropTypes.func,
};

export default compose( [
withCategory,
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,
category,
} = this.props;
if (
this.state.doUrlUpdate &&
! attributes.editMode &&
category?.permalink &&
currentButtonAttributes?.url &&
category.permalink !== currentButtonAttributes.url
) {
updateBlockAttributes( {
...currentButtonAttributes,
url: category.permalink,
} );
this.setState( { doUrlUpdate: false } );
}
}
triggerUrlUpdate = () => {
this.setState( { doUrlUpdate: true } );
};
render() {
return (
<ProductComponent
triggerUrlUpdate={ this.triggerUrlUpdate }
{ ...this.props }
/>
);
}
}
return WrappedComponent;
}, 'withUpdateButtonAttributes' ),
] )( FeaturedCategory );