2022-05-30 14:38:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
|
2023-12-08 11:39:31 +00:00
|
|
|
import {
|
|
|
|
ProductResponseItem,
|
|
|
|
ProductCategoryResponseItem,
|
|
|
|
} from '@woocommerce/types';
|
2022-05-30 14:38:52 +00:00
|
|
|
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';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { ComponentType } from 'react';
|
2022-05-30 14:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 >;
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
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;
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const className = getClassPrefixFromName( name );
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const onDone = () => {
|
|
|
|
setAttributes( { editMode: false } );
|
|
|
|
debouncedSpeak( editLabel );
|
|
|
|
};
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
if ( attributes.editMode ) {
|
|
|
|
return (
|
|
|
|
<Placeholder
|
|
|
|
icon={ <Icon icon={ icon } /> }
|
|
|
|
label={ label }
|
|
|
|
className={ className }
|
|
|
|
>
|
|
|
|
{ description }
|
|
|
|
<div className={ `${ className }__selection` }>
|
|
|
|
{ name === BLOCK_NAMES.featuredCategory && (
|
|
|
|
<ProductCategoryControl
|
2023-12-08 11:39:31 +00:00
|
|
|
selected={
|
|
|
|
attributes.categoryId
|
|
|
|
? [ attributes.categoryId ]
|
|
|
|
: []
|
|
|
|
}
|
2022-06-15 09:56:52 +00:00
|
|
|
onChange={ (
|
2023-12-08 11:39:31 +00:00
|
|
|
value: ProductCategoryResponseItem[] = []
|
2022-06-15 09:56:52 +00:00
|
|
|
) => {
|
|
|
|
const id = value[ 0 ] ? value[ 0 ].id : 0;
|
|
|
|
setAttributes( {
|
|
|
|
categoryId: id,
|
|
|
|
mediaId: 0,
|
|
|
|
mediaSrc: '',
|
|
|
|
} );
|
|
|
|
triggerUrlUpdate();
|
|
|
|
} }
|
|
|
|
isSingle
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ name === BLOCK_NAMES.featuredProduct && (
|
|
|
|
<ProductControl
|
|
|
|
selected={
|
|
|
|
attributes.productId
|
|
|
|
? [ attributes.productId ]
|
|
|
|
: []
|
|
|
|
}
|
|
|
|
showVariations
|
|
|
|
onChange={ (
|
|
|
|
value: ProductResponseItem[] = []
|
|
|
|
) => {
|
|
|
|
const id = value[ 0 ] ? value[ 0 ].id : 0;
|
|
|
|
setAttributes( {
|
|
|
|
productId: id,
|
|
|
|
mediaId: 0,
|
|
|
|
mediaSrc: '',
|
|
|
|
} );
|
|
|
|
triggerUrlUpdate();
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
2023-12-08 23:30:33 +00:00
|
|
|
<Button variant="primary" onClick={ onDone }>
|
2022-06-15 09:56:52 +00:00
|
|
|
{ __( 'Done', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Placeholder>
|
|
|
|
);
|
|
|
|
}
|
2022-05-30 14:38:52 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
return <Component { ...props } />;
|
|
|
|
};
|