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

Commit

Permalink
Merge branch 'update/reviews-by-product-hook-order-by-endpoint' into …
Browse files Browse the repository at this point in the history
…fix/hide-ratings-if-disabled-in-settings
  • Loading branch information
Aljullu committed Jul 22, 2019
2 parents 56940d6 + b64641f commit 22e26bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions assets/js/blocks/reviews-by-product/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class ReviewsByProduct extends Component {
}

onChangeOrderby( event ) {
const { attributes } = this.props;
const { perPage } = attributes;
const { totalReviews } = this.state;
const newReviews = Math.min( totalReviews, perPage );
this.setState( {
reviews: Array( newReviews ).fill( {} ),
orderby: event.target.value,
} );
this.getReviews( event.target.value );
Expand Down Expand Up @@ -111,7 +116,10 @@ class ReviewsByProduct extends Component {
if ( page === 1 ) {
this.setState( { reviews: newReviews, totalReviews } );
} else {
this.setState( { reviews: reviews.concat( newReviews ), totalReviews } );
this.setState( {
reviews: reviews.filter( ( review ) => Object.keys( review ).length ).concat( newReviews ),
totalReviews,
} );
}
} ).catch( () => {
this.setState( { reviews: [] } );
Expand All @@ -125,8 +133,14 @@ class ReviewsByProduct extends Component {
}

appendReviews() {
const page = Math.round( this.state.reviews.length / this.props.attributes.perPage ) + 1;
const { attributes } = this.props;
const { perPage } = attributes;
const { reviews, totalReviews } = this.state;

const newReviews = Math.min( totalReviews - reviews.length, perPage );
this.setState( { reviews: reviews.concat( Array( newReviews ).fill( {} ) ) } );

const page = Math.round( reviews.length / perPage ) + 1;
this.getReviews( null, page );
}

Expand Down Expand Up @@ -182,7 +196,7 @@ class ReviewsByProduct extends Component {
(
renderReview( attrs )
) : (
reviews.map( ( review ) => renderReview( attrs, review ) )
reviews.map( ( review, i ) => renderReview( attrs, review, i ) )
)
}
</ul>
Expand Down
4 changes: 2 additions & 2 deletions assets/js/blocks/reviews-by-product/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import classNames from 'classnames';
*
* @return {Object} React JSx nodes of the block
*/
export function renderReview( attributes, review = {} ) {
export function renderReview( attributes, review = {}, i = 0 ) {
const { showAvatar, showProductRating: showProductRatingAttr, showReviewDate, showReviewerName } = attributes;
const { id = null, date_created: dateCreated, rating, review: text = '', reviewer = '', reviewer_avatar_urls: avatarUrls = {} } = review;
const isLoading = ! Object.keys( review ).length > 0;
Expand All @@ -31,7 +31,7 @@ export function renderReview( attributes, review = {} ) {
};

return (
<li className={ classes } key={ id } aria-hidden={ isLoading }>
<li className={ classes } key={ id || i } aria-hidden={ isLoading }>
<span
className="wc-block-reviews-by-product__text"
dangerouslySetInnerHTML={ {
Expand Down

0 comments on commit 22e26bf

Please sign in to comment.