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

Filter by Rating Block > Display a notice in the editor if the store has no products with ratings #7763

Merged
merged 9 commits into from
Dec 12, 2022
22 changes: 22 additions & 0 deletions assets/js/blocks/rating-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import Rating from '@woocommerce/base-components/product-rating';
import { Notice } from 'wordpress-components';
import { usePrevious, useShallowEqual } from '@woocommerce/base-hooks';
import {
useQueryStateByKey,
Expand Down Expand Up @@ -90,6 +91,9 @@ const RatingFilterBlock = ( {
initialFilters
);

const [ displayNoProductRatingsNotice, setDisplayNoProductRatingsNotice ] =
useState( false );

/**
* Used to redirect the page when filters are changed so templates using the Classic Template block can filter.
*
Expand Down Expand Up @@ -189,13 +193,20 @@ const RatingFilterBlock = ( {
if ( filteredCountsLoading || blockAttributes.isPreview ) {
return;
}

const orderedRatings =
! filteredCountsLoading &&
objectHasProp( filteredCounts, 'rating_counts' ) &&
Array.isArray( filteredCounts.rating_counts )
? [ ...filteredCounts.rating_counts ].reverse()
: [];

if ( isEditor && orderedRatings.length === 0 ) {
setDisplayedOptions( previewOptions );
setDisplayNoProductRatingsNotice( true );
return;
}

const newOptions = orderedRatings
.filter(
( item ) => isObject( item ) && Object.keys( item ).length > 0
Expand Down Expand Up @@ -229,6 +240,7 @@ const RatingFilterBlock = ( {
filteredCounts,
filteredCountsLoading,
productRatings,
isEditor,
] );

/**
Expand Down Expand Up @@ -292,6 +304,16 @@ const RatingFilterBlock = ( {

return (
<>
{ displayNoProductRatingsNotice && (
<Notice status="warning" isDismissible={ false }>
<p>
{ __(
"Your store doesn't have any products with ratings yet. This filter option will display when a product receives a review.",
'woo-gutenberg-products-block'
) }
</p>
</Notice>
) }
<div
className={ classnames( 'wc-block-rating-filter', {
'is-loading': isLoading,
Expand Down