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-08-22 15:50:37 +00:00
|
|
|
import { StoreNoticesContainer } from '@woocommerce/base-context';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreEvents } from '@woocommerce/base-context/hooks';
|
2020-06-05 12:18:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { BLOCK_NAME } from './constants';
|
2020-05-27 14:45:18 +00:00
|
|
|
|
2020-09-20 23:54:08 +00:00
|
|
|
/** @typedef {import('react')} React */
|
|
|
|
|
2020-05-27 14:45:18 +00:00
|
|
|
/**
|
|
|
|
* The Single Product Block.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {boolean} props.isLoading
|
|
|
|
* @param {Object} props.product
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {React.ReactChildren} props.children
|
2020-05-27 14:45:18 +00:00
|
|
|
*/
|
2020-06-05 12:18:16 +00:00
|
|
|
const Block = ( { isLoading, product, children } ) => {
|
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 );
|