This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Create renderFrontend utils function #954
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) => { | ||
const props = getProps( el ); | ||
const attributes = { | ||
...el.dataset, | ||
...props.attributes, | ||
}; | ||
|
||
el.classList.remove( 'is-loading' ); | ||
|
||
render( <Block { ...props } attributes={ attributes } />, el ); | ||
} ); | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 becausecontainers
is an array? I see the comment about "for IE11" compatibility but won't babel transpile handle that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
containers
is aNodeList
and IE11 doesn't support theforEach
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): #, #.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh right! Thanks for verifying with polyfills too 👍