2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-10-25 14:46:34 +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';
|
|
|
|
|
2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-10-25 14:46:34 +00:00
|
|
|
import './inner-blocks/register-components';
|
|
|
|
import Block from './block';
|
|
|
|
import { blockName, blockAttributes } from './attributes';
|
2019-12-10 15:41:57 +00:00
|
|
|
|
2020-03-13 13:41:59 +00:00
|
|
|
const getProps = ( el ) => {
|
|
|
|
return {
|
2021-10-25 14:46:34 +00:00
|
|
|
attributes: getValidBlockAttributes(
|
|
|
|
blockAttributes,
|
|
|
|
!! el ? el.dataset : {}
|
|
|
|
),
|
2020-03-13 13:41:59 +00:00
|
|
|
};
|
|
|
|
};
|
2020-02-18 23:06:37 +00:00
|
|
|
|
2021-10-25 14:46:34 +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;
|
|
|
|
} );
|
2020-03-06 11:43:40 +00:00
|
|
|
};
|
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
renderParentBlock( {
|
2021-11-04 11:05:58 +00:00
|
|
|
Block,
|
2021-10-25 14:46:34 +00:00
|
|
|
blockName,
|
2020-05-27 10:11:30 +00:00
|
|
|
selector: '.wp-block-woocommerce-cart',
|
2020-03-06 11:43:40 +00:00
|
|
|
getProps,
|
2021-10-25 14:46:34 +00:00
|
|
|
blockMap: getRegisteredBlockComponents( blockName ),
|
|
|
|
blockWrapper: Wrapper,
|
2020-05-27 10:11:30 +00:00
|
|
|
} );
|