Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Create renderFrontend utils function #954

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions assets/js/blocks/product-categories/frontend.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
/**
* External dependencies
*/
import { render } from 'react-dom';

/**
* Internal dependencies
*/
import Block from './block.js';
import getCategories from './get-categories';
import renderFrontend from '../../utils/render-frontend.js';

const containers = document.querySelectorAll(
'.wp-block-woocommerce-product-categories'
);

if ( containers.length ) {
Array.prototype.forEach.call( containers, ( el ) => {
const data = JSON.parse( JSON.stringify( el.dataset ) );
const attributes = {
hasCount: data.hasCount === 'true',
hasEmpty: data.hasEmpty === 'true',
isDropdown: data.isDropdown === 'true',
isHierarchical: data.isHierarchical === 'true',
};
const categories = getCategories( attributes );
const getProps = ( el ) => {
const attributes = {
hasCount: el.dataset.hasCount === 'true',
hasEmpty: el.dataset.hasEmpty === 'true',
isDropdown: el.dataset.isDropdown === 'true',
isHierarchical: el.dataset.isHierarchical === 'true',
};

el.classList.remove( 'is-loading' );
return {
attributes,
categories: getCategories( attributes ),
};
};

render(
<Block attributes={ attributes } categories={ categories } />,
el
);
} );
}
renderFrontend( '.wp-block-woocommerce-product-categories', Block, getProps );
26 changes: 10 additions & 16 deletions assets/js/blocks/reviews/frontend.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
/**
* External dependencies
*/
import { render } from 'react-dom';

/**
* Internal dependencies
*/
import FrontendContainerBlock from './frontend-container-block.js';
import renderFrontend from '../../utils/render-frontend.js';

const containers = document.querySelectorAll( `
const selector = `
.wp-block-woocommerce-all-reviews,
.wp-block-woocommerce-reviews-by-product,
.wp-block-woocommerce-reviews-by-category
` );
`;

if ( containers.length ) {
// Use Array.forEach for IE11 compatibility
Array.prototype.forEach.call( containers, ( el ) => {
const attributes = {
...el.dataset,
const getProps = ( el ) => {
return {
attributes: {
showReviewDate: el.classList.contains( 'has-date' ),
showReviewerName: el.classList.contains( 'has-name' ),
showReviewImage: el.classList.contains( 'has-image' ),
showReviewRating: el.classList.contains( 'has-rating' ),
showReviewContent: el.classList.contains( 'has-content' ),
showProductName: el.classList.contains( 'has-product-name' ),
};
},
};
};

render( <FrontendContainerBlock attributes={ attributes } />, el );
} );
}
renderFrontend( selector, FrontendContainerBlock, getProps );
31 changes: 31 additions & 0 deletions assets/js/utils/render-frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* External dependencies
*/
import { render } from 'react-dom';

/**
* Renders a block component in the place of a specified set of selectors.
*
* @param {string} selector CSS selector to match the elements to replace.
* @param {function} Block React block to use as a replacement.
* @param {function} [getProps] Function to generate the props object for the
* block.
*/
export default ( selector, Block, getProps = () => {} ) => {
const containers = document.querySelectorAll( selector );

if ( containers.length ) {
// Use Array.forEach for IE11 compatibility
Array.prototype.forEach.call( containers, ( el ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, why not just containers.forEach which is functionally equivalent because containers is an array? I see the comment about "for IE11" compatibility but won't babel transpile handle that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

containers is a NodeList and IE11 doesn't support the forEach method:
https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach#Browser_Compatibility

I also thought Babel and the polyfills would take care of it, but it looks like they don't (just tested with IE11): #, #.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

containers is a NodeList

Ahh right! Thanks for verifying with polyfills too 👍

const props = getProps( el );
const attributes = {
...el.dataset,
...props.attributes,
};

el.classList.remove( 'is-loading' );

render( <Block { ...props } attributes={ attributes } />, el );
} );
}
};