2019-09-09 13:25:22 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render } from 'react-dom';
|
2019-11-21 08:08:47 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2019-09-09 13:25:22 +00:00
|
|
|
|
2020-04-27 16:06:58 +00:00
|
|
|
/**
|
|
|
|
* Given some block attributes, gets attributes from the dataset or uses defaults.
|
|
|
|
*
|
|
|
|
* @param {Object} blockAttributes Object containing block attributes.
|
|
|
|
* @param {Array} dataset Dataset from DOM.
|
|
|
|
* @return {Array} Array of parsed attributes.
|
|
|
|
*/
|
|
|
|
export const getAttributesFromDataset = ( blockAttributes, dataset ) => {
|
|
|
|
const attributes = [];
|
|
|
|
|
|
|
|
Object.keys( blockAttributes ).forEach( ( key ) => {
|
|
|
|
if ( typeof dataset[ key ] !== 'undefined' ) {
|
|
|
|
switch ( blockAttributes[ key ].type ) {
|
|
|
|
case 'boolean':
|
|
|
|
attributes[ key ] = dataset[ key ] !== 'false';
|
|
|
|
break;
|
|
|
|
case 'number':
|
|
|
|
attributes[ key ] = parseInt( dataset[ key ], 10 );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
attributes[ key ] = dataset[ key ];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attributes[ key ] = blockAttributes[ key ].default;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return attributes;
|
|
|
|
};
|
|
|
|
|
2019-09-09 13:25:22 +00:00
|
|
|
/**
|
|
|
|
* Renders a block component in the place of a specified set of selectors.
|
|
|
|
*
|
2020-05-27 10:11:30 +00:00
|
|
|
* @param {Object} props Render props.
|
|
|
|
* @param {string} props.selector CSS selector to match the elements to replace.
|
|
|
|
* @param {Function} props.Block React component to use as a replacement.
|
|
|
|
* @param {Function} [props.getProps ] Function to generate the props object for the block.
|
|
|
|
* @param {Function} [props.getErrorBoundaryProps] Function to generate the props object for the error boundary.
|
2019-09-09 13:25:22 +00:00
|
|
|
*/
|
2020-05-27 10:11:30 +00:00
|
|
|
export const renderFrontend = ( {
|
2020-03-06 11:43:40 +00:00
|
|
|
selector,
|
|
|
|
Block,
|
|
|
|
getProps = () => {},
|
2020-05-27 10:11:30 +00:00
|
|
|
getErrorBoundaryProps = () => {},
|
|
|
|
} ) => {
|
2019-09-09 13:25:22 +00:00
|
|
|
const containers = document.querySelectorAll( selector );
|
|
|
|
|
|
|
|
if ( containers.length ) {
|
2019-11-23 16:29:35 +00:00
|
|
|
// Use Array.forEach for IE11 compatibility.
|
2019-10-28 13:53:09 +00:00
|
|
|
Array.prototype.forEach.call( containers, ( el, i ) => {
|
|
|
|
const props = getProps( el, i );
|
2020-03-06 11:43:40 +00:00
|
|
|
const errorBoundaryProps = getErrorBoundaryProps( el, i );
|
2019-09-09 13:25:22 +00:00
|
|
|
const attributes = {
|
|
|
|
...el.dataset,
|
|
|
|
...props.attributes,
|
|
|
|
};
|
|
|
|
|
|
|
|
el.classList.remove( 'is-loading' );
|
|
|
|
|
2019-11-21 08:08:47 +00:00
|
|
|
render(
|
2020-03-06 11:43:40 +00:00
|
|
|
<BlockErrorBoundary { ...errorBoundaryProps }>
|
2019-11-21 08:08:47 +00:00
|
|
|
<Block { ...props } attributes={ attributes } />
|
|
|
|
</BlockErrorBoundary>,
|
|
|
|
el
|
|
|
|
);
|
2019-09-09 13:25:22 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
2020-04-27 16:06:58 +00:00
|
|
|
|
|
|
|
export default renderFrontend;
|