2022-05-30 14:38:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { ComponentType, useEffect, useMemo, useState } from 'react';
|
|
|
|
import { WP_REST_API_Category } from 'wp-types';
|
|
|
|
import { ProductResponseItem } from '@woocommerce/types';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { EditorBlock } from './types';
|
|
|
|
|
|
|
|
interface WithUpdateButtonRequiredAttributes {
|
|
|
|
editMode: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WithUpdateButtonAttributes< T > {
|
|
|
|
attributes: WithUpdateButtonRequiredAttributes &
|
|
|
|
EditorBlock< T >[ 'attributes' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WithUpdateButtonCategoryProps< T >
|
|
|
|
extends WithUpdateButtonAttributes< T > {
|
|
|
|
category: WP_REST_API_Category;
|
|
|
|
product: never;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WithUpdateButtonProductProps< T >
|
|
|
|
extends WithUpdateButtonAttributes< T > {
|
|
|
|
category: never;
|
|
|
|
product: ProductResponseItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
type WithUpdateButtonProps< T extends EditorBlock< T > > =
|
|
|
|
| ( T & WithUpdateButtonCategoryProps< T > )
|
|
|
|
| ( T & WithUpdateButtonProductProps< T > );
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
export const withUpdateButtonAttributes =
|
|
|
|
< T extends EditorBlock< T > >( Component: ComponentType< T > ) =>
|
|
|
|
( props: WithUpdateButtonProps< T > ) => {
|
|
|
|
const [ doUrlUpdate, setDoUrlUpdate ] = useState( false );
|
|
|
|
const { attributes, category, clientId, product } = props;
|
|
|
|
const item = category || product;
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const { editMode } = attributes;
|
|
|
|
const permalink =
|
|
|
|
( item as WP_REST_API_Category )?.link ||
|
|
|
|
( item as ProductResponseItem )?.permalink;
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-07-26 12:30:18 +00:00
|
|
|
const block = useSelect( ( select ) => {
|
2022-06-15 09:56:52 +00:00
|
|
|
return select( 'core/block-editor' ).getBlock( clientId );
|
|
|
|
} );
|
2022-07-26 12:30:18 +00:00
|
|
|
const innerBlock = block?.innerBlocks[ 0 ]?.innerBlocks[ 0 ];
|
|
|
|
const buttonBlockId = innerBlock?.clientId || '';
|
2022-06-15 09:56:52 +00:00
|
|
|
const currentButtonAttributes = useMemo(
|
2022-07-26 12:30:18 +00:00
|
|
|
() => innerBlock?.attributes || {},
|
|
|
|
[ innerBlock ]
|
2022-06-15 09:56:52 +00:00
|
|
|
);
|
|
|
|
const { url } = currentButtonAttributes;
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const { updateBlockAttributes } = useDispatch( 'core/block-editor' );
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if (
|
|
|
|
doUrlUpdate &&
|
|
|
|
buttonBlockId &&
|
|
|
|
! editMode &&
|
|
|
|
permalink &&
|
|
|
|
url &&
|
|
|
|
permalink !== url
|
|
|
|
) {
|
|
|
|
updateBlockAttributes( buttonBlockId, {
|
|
|
|
url: permalink,
|
|
|
|
} );
|
|
|
|
setDoUrlUpdate( false );
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
buttonBlockId,
|
|
|
|
doUrlUpdate,
|
|
|
|
editMode,
|
|
|
|
permalink,
|
|
|
|
updateBlockAttributes,
|
|
|
|
url,
|
|
|
|
] );
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const triggerUrlUpdate = () => setDoUrlUpdate( true );
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
return <Component { ...props } triggerUrlUpdate={ triggerUrlUpdate } />;
|
|
|
|
};
|