2021-09-17 15:29:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
withStoreCartApiHydration,
|
|
|
|
withRestApiHydration,
|
|
|
|
} from '@woocommerce/block-hocs';
|
2021-09-23 15:38:30 +00:00
|
|
|
import { getValidBlockAttributes } from '@woocommerce/base-utils';
|
|
|
|
import { Children, cloneElement, isValidElement } from '@wordpress/element';
|
|
|
|
import { useStoreCart } from '@woocommerce/base-context';
|
|
|
|
import { getRegisteredBlockComponents } from '@woocommerce/blocks-registry';
|
|
|
|
|
|
|
|
import { renderParentBlock } from '@woocommerce/atomic-utils';
|
|
|
|
|
2021-09-17 15:29:58 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-23 15:38:30 +00:00
|
|
|
import './inner-blocks/register-components';
|
|
|
|
import Block from './block';
|
|
|
|
import { blockName, blockAttributes } from './attributes';
|
2021-09-17 15:29:58 +00:00
|
|
|
|
|
|
|
const getProps = ( el ) => {
|
|
|
|
return {
|
2021-09-23 15:38:30 +00:00
|
|
|
attributes: getValidBlockAttributes(
|
|
|
|
blockAttributes,
|
|
|
|
!! el ? el.dataset : {}
|
|
|
|
),
|
2021-09-17 15:29:58 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-23 15:38:30 +00:00
|
|
|
const Wrapper = ( { children } ) => {
|
|
|
|
// we need to pluck out receiveCart.
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const { extensions, receiveCart, ...cart } = useStoreCart();
|
|
|
|
return Children.map( children, ( child ) => {
|
|
|
|
if ( isValidElement( child ) ) {
|
|
|
|
const componentProps = {
|
|
|
|
extensions,
|
|
|
|
cart,
|
|
|
|
};
|
|
|
|
return cloneElement( child, componentProps );
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
} );
|
2021-09-17 15:29:58 +00:00
|
|
|
};
|
|
|
|
|
2021-09-23 15:38:30 +00:00
|
|
|
renderParentBlock( {
|
2021-09-24 14:37:16 +00:00
|
|
|
Block: withStoreCartApiHydration( withRestApiHydration( Block ) ),
|
2021-09-23 15:38:30 +00:00
|
|
|
blockName,
|
|
|
|
selector: '.wp-block-woocommerce-cart-i2',
|
2021-09-17 15:29:58 +00:00
|
|
|
getProps,
|
2021-09-23 15:38:30 +00:00
|
|
|
blockMap: getRegisteredBlockComponents( blockName ),
|
|
|
|
blockWrapper: Wrapper,
|
2021-09-17 15:29:58 +00:00
|
|
|
} );
|