2020-05-27 14:45:18 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-03-31 14:22:27 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2020-05-27 14:45:18 +00:00
|
|
|
import { withProduct } from '@woocommerce/block-hocs';
|
2020-06-05 12:18:16 +00:00
|
|
|
import {
|
|
|
|
InnerBlockLayoutContextProvider,
|
|
|
|
ProductDataContextProvider,
|
|
|
|
} from '@woocommerce/shared-context';
|
2022-11-17 13:33:58 +00:00
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-checkout';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreEvents } from '@woocommerce/base-context/hooks';
|
2022-12-23 11:59:02 +00:00
|
|
|
import { ProductResponseItem } from '@woocommerce/types';
|
2020-06-05 12:18:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { BLOCK_NAME } from './constants';
|
2020-05-27 14:45:18 +00:00
|
|
|
|
2022-12-22 07:23:11 +00:00
|
|
|
interface BlockProps {
|
|
|
|
isLoading: boolean;
|
|
|
|
product: ProductResponseItem;
|
|
|
|
children: JSX.Element | JSX.Element[];
|
|
|
|
}
|
2020-09-20 23:54:08 +00:00
|
|
|
|
2022-12-22 07:23:11 +00:00
|
|
|
const Block = ( { isLoading, product, children }: BlockProps ) => {
|
2021-03-31 14:22:27 +00:00
|
|
|
const { dispatchStoreEvent } = useStoreEvents();
|
2020-07-22 12:20:54 +00:00
|
|
|
const className = 'wc-block-single-product wc-block-layout';
|
2020-07-30 10:57:22 +00:00
|
|
|
const noticeContext = `woocommerce/single-product/${ product?.id || 0 }`;
|
2020-06-05 12:18:16 +00:00
|
|
|
|
2021-03-31 14:22:27 +00:00
|
|
|
useEffect( () => {
|
|
|
|
dispatchStoreEvent( 'product-render', {
|
|
|
|
product,
|
|
|
|
listName: BLOCK_NAME,
|
|
|
|
} );
|
|
|
|
}, [ product, dispatchStoreEvent ] );
|
|
|
|
|
2020-06-05 12:18:16 +00:00
|
|
|
return (
|
|
|
|
<InnerBlockLayoutContextProvider
|
|
|
|
parentName={ BLOCK_NAME }
|
|
|
|
parentClassName={ className }
|
|
|
|
>
|
2020-07-22 12:20:54 +00:00
|
|
|
<ProductDataContextProvider
|
|
|
|
product={ product }
|
|
|
|
isLoading={ isLoading }
|
|
|
|
>
|
2022-08-22 15:50:37 +00:00
|
|
|
<StoreNoticesContainer context={ noticeContext } />
|
2022-04-08 12:11:50 +00:00
|
|
|
<div className={ className }>{ children }</div>
|
2020-06-05 12:18:16 +00:00
|
|
|
</ProductDataContextProvider>
|
|
|
|
</InnerBlockLayoutContextProvider>
|
|
|
|
);
|
2020-05-27 14:45:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default withProduct( Block );
|