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

Commit

Permalink
If product is changed for featured product block, update the link in …
Browse files Browse the repository at this point in the history
…the button. (#1894)

* If product is changed for featured product block, update the link in the button

* add handling to _only_ update the product url when the product is changed.
  • Loading branch information
nerrad committed May 12, 2020
1 parent 72188b0 commit 5d85b0e
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions assets/js/blocks/featured-product/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
withColors,
RichText,
} from '@wordpress/editor';
import { withSelect } from '@wordpress/data';
import {
Button,
FocalPointPicker,
Expand All @@ -27,8 +28,8 @@ import {
withSpokenMessages,
} from '@wordpress/components';
import classnames from 'classnames';
import { Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
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';
Expand Down Expand Up @@ -60,6 +61,7 @@ const FeaturedProduct = ( {
product,
setAttributes,
setOverlayColor,
triggerUrlUpdate = () => void null,
} ) => {
const renderApiError = () => (
<ErrorPlaceholder
Expand Down Expand Up @@ -107,6 +109,7 @@ const FeaturedProduct = ( {
mediaId: 0,
mediaSrc: '',
} );
triggerUrlUpdate();
} }
/>
<Button isDefault onClick={ onDone }>
Expand Down Expand Up @@ -446,10 +449,66 @@ FeaturedProduct.propTypes = {
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 (
<ProductComponent
triggerUrlUpdate={ this.triggerUrlUpdate }
{ ...this.props }
/>
);
}
}
return WrappedComponent;
}, 'withUpdateButtonAttributes' ),
] )( FeaturedProduct );

0 comments on commit 5d85b0e

Please sign in to comment.