/**
* External dependencies
*/
import { WP_REST_API_Category } from 'wp-types';
import { ProductResponseItem } from '@woocommerce/types';
import { Placeholder, Icon, Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import ProductCategoryControl from '@woocommerce/editor-components/product-category-control';
import ProductControl from '@woocommerce/editor-components/product-control';
import { ComponentType } from 'react';
/**
* Internal dependencies
*/
import { BLOCK_NAMES } from './constants';
import { EditorBlock, GenericBlockUIConfig } from './types';
import { getClassPrefixFromName } from './utils';
interface EditModeConfiguration extends GenericBlockUIConfig {
description: string;
editLabel: string;
}
type EditModeRequiredAttributes = {
categoryId?: number;
editMode: boolean;
mediaId: number;
mediaSrc: string;
productId?: number;
};
interface EditModeRequiredProps< T > {
attributes: EditModeRequiredAttributes & EditorBlock< T >[ 'attributes' ];
debouncedSpeak: ( label: string ) => void;
setAttributes: ( attrs: Partial< EditModeRequiredAttributes > ) => void;
triggerUrlUpdate: () => void;
}
type EditModeProps< T extends EditorBlock< T > > = T &
EditModeRequiredProps< T >;
export const withEditMode =
( { description, editLabel, icon, label }: EditModeConfiguration ) =>
< T extends EditorBlock< T > >( Component: ComponentType< T > ) =>
( props: EditModeProps< T > ) => {
const {
attributes,
debouncedSpeak,
name,
setAttributes,
triggerUrlUpdate = () => void null,
} = props;
const className = getClassPrefixFromName( name );
const onDone = () => {
setAttributes( { editMode: false } );
debouncedSpeak( editLabel );
};
if ( attributes.editMode ) {
return (