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

Add a Products by Rating filter block #7048

Merged
merged 30 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8cfef9c
First commit
danieldudzic Jul 4, 2022
e144542
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
danieldudzic Jul 25, 2022
02f551b
Add interactivity to the Product by Rating filter block
danieldudzic Jul 29, 2022
a903d8f
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
danieldudzic Aug 22, 2022
25398a2
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
danieldudzic Aug 24, 2022
28dff81
Fix block with the latest repo changes
danieldudzic Aug 25, 2022
602f563
Product by Rating: Code tidying up
danieldudzic Sep 3, 2022
44967f2
Add an experimental build gate and update block title and description
danieldudzic Sep 7, 2022
7d6fa6d
Remove redundant title and description
danieldudzic Sep 7, 2022
167b885
Add support for the CheckboxList component in the Products by Rating …
danieldudzic Sep 16, 2022
73df337
Products by Rating: Minor code clean-up
danieldudzic Sep 20, 2022
32436c8
Active Filters: Fix the Clear All button for Ratings. Closes ##7172
danieldudzic Sep 20, 2022
72654a4
Products by Rating: Add misc TS fixes
danieldudzic Sep 22, 2022
339770c
First commit
danieldudzic Jul 4, 2022
e6c3c98
Add interactivity to the Product by Rating filter block
danieldudzic Jul 29, 2022
907ef8a
Fix block with the latest repo changes
danieldudzic Aug 25, 2022
e55d55a
Product by Rating: Code tidying up
danieldudzic Sep 3, 2022
89e93c6
Add an experimental build gate and update block title and description
danieldudzic Sep 7, 2022
ad3ff5e
Remove redundant title and description
danieldudzic Sep 7, 2022
a4e1af8
Add support for the CheckboxList component in the Products by Rating …
danieldudzic Sep 16, 2022
8179cbc
Products by Rating: Minor code clean-up
danieldudzic Sep 20, 2022
7c9723c
Active Filters: Fix the Clear All button for Ratings. Closes ##7172
danieldudzic Sep 20, 2022
4f2751b
Products by Rating: Add misc TS fixes
danieldudzic Sep 22, 2022
aaaacc1
Products by Rating: Add new skeleton design
danieldudzic Sep 23, 2022
986cf18
Merge branch 'try/add-rating-component' of github.com:woocommerce/woo…
danieldudzic Sep 23, 2022
7c9d6ae
Products by Rating: Fix skeleton size
danieldudzic Sep 26, 2022
75d8bdd
Products by Rating: Fix TS errors
danieldudzic Sep 28, 2022
fbb11eb
Products by Rating: Remove duplicate style selector
danieldudzic Sep 28, 2022
dd737c2
Merge branch 'trunk' into try/add-rating-component
danieldudzic Sep 28, 2022
f47f62a
Merge branch 'trunk' into try/add-rating-component
danieldudzic Sep 28, 2022
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
3 changes: 2 additions & 1 deletion assets/js/base/components/filter-placeholder/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
.wc-block-stock-filter__title,
.wc-block-price-filter__title,
.wc-block-active-filters__title,
.wc-block-attribute-filter__title {
.wc-block-attribute-filter__title,
.wc-block-rating-filter__title {
margin: 0;
height: 1em;
}
Expand Down
7 changes: 7 additions & 0 deletions assets/js/base/components/product-list/product-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ const ProductList = ( {
'stock_status',
[]
);
const [ productRating, setProductRating ] = useQueryStateByKey(
'rating',
[]
);

const [ minPrice, setMinPrice ] = useQueryStateByKey( 'min_price' );
const [ maxPrice, setMaxPrice ] = useQueryStateByKey( 'max_price' );

Expand Down Expand Up @@ -215,6 +220,7 @@ const ProductList = ( {
const hasFilters =
productAttributes.length > 0 ||
productStockStatus.length > 0 ||
productRating.length > 0 ||
Number.isFinite( minPrice ) ||
Number.isFinite( maxPrice );

Expand All @@ -231,6 +237,7 @@ const ProductList = ( {
resetCallback={ () => {
setProductAttributes( [] );
setProductStockStatus( [] );
setProductRating( [] );
setMinPrice( null );
setMaxPrice( null );
} }
Expand Down
64 changes: 64 additions & 0 deletions assets/js/base/components/product-rating/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './style.scss';

const Rating = ( {
className,
key,
rating,
ratedProductsCount,
}: RatingProps ): JSX.Element => {
const ratingClassName = classNames(
'wc-block-components-product-rating',
className
);

const starStyle = {
width: ( rating / 5 ) * 100 + '%',
};

const ratingText = sprintf(
/* translators: %f is referring to the average rating value */
__( 'Rated %f out of 5', 'woo-gutenberg-products-block' ),
rating
);

const ratingHTML = {
__html: sprintf(
/* translators: %f is referring to the rating value */
__( 'Rated %f out of 5', 'woo-gutenberg-products-block' ),
sprintf( '<strong class="rating">%f</strong>', rating )
),
};

return (
<div className={ ratingClassName } key={ key }>
<div
className={ 'wc-block-components-product-rating__stars' }
role="img"
aria-label={ ratingText }
>
<span
style={ starStyle }
dangerouslySetInnerHTML={ ratingHTML }
/>
</div>
{ ratedProductsCount ? `(${ ratedProductsCount })` : null }
</div>
);
};
interface RatingProps {
className: string;
key: 0 | 1 | 2 | 3 | 4 | 5;
rating: 0 | 1 | 2 | 3 | 4 | 5;
ratedProductsCount: number;
}

export default Rating;
7 changes: 7 additions & 0 deletions assets/js/base/components/product-rating/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.wc-block-components-product-rating {
&__stars {
display: inline-block;
line-height: 1;
height: 1em;
}
}
18 changes: 18 additions & 0 deletions assets/js/base/context/hooks/collections/use-collection-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ interface UseCollectionDataProps {
};
queryPrices?: boolean;
queryStock?: boolean;
queryRating?: boolean;
queryState: Record< string, unknown >;
}

export const useCollectionData = ( {
queryAttribute,
queryPrices,
queryStock,
queryRating,
queryState,
}: UseCollectionDataProps ) => {
let context = useQueryStateContext();
Expand All @@ -66,10 +68,13 @@ export const useCollectionData = ( {
calculateStockStatusQueryState,
setCalculateStockStatusQueryState,
] = useQueryStateByKey( 'calculate_stock_status_counts', null, context );
const [ calculateRatingQueryState, setCalculateRatingQueryState ] =
useQueryStateByKey( 'calculate_rating_counts', null, context );

const currentQueryAttribute = useShallowEqual( queryAttribute || {} );
const currentQueryPrices = useShallowEqual( queryPrices );
const currentQueryStock = useShallowEqual( queryStock );
const currentQueryRating = useShallowEqual( queryRating );

useEffect( () => {
if (
Expand Down Expand Up @@ -124,6 +129,19 @@ export const useCollectionData = ( {
calculateStockStatusQueryState,
] );

useEffect( () => {
if (
calculateRatingQueryState !== currentQueryRating &&
currentQueryRating !== undefined
) {
setCalculateRatingQueryState( currentQueryRating );
}
}, [
currentQueryRating,
setCalculateRatingQueryState,
calculateRatingQueryState,
] );

// Defer the select query so all collection-data query vars can be gathered.
const [ shouldSelect, setShouldSelect ] = useState( false );
const [ debouncedShouldSelect ] = useDebounce( shouldSelect, 200 );
Expand Down
3 changes: 2 additions & 1 deletion assets/js/blocks/active-filters/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const ActiveFiltersBlock = ( {
] );

const [ productRatings, setProductRatings ] =
useQueryStateByKey( 'ratings' );
useQueryStateByKey( 'rating' );

/**
* Parse the filter URL to set the active rating fitlers.
Expand Down Expand Up @@ -348,6 +348,7 @@ const ActiveFiltersBlock = ( {
setMaxPrice( undefined );
setProductAttributes( [] );
setProductStockStatus( [] );
setProductRatings( [] );
}
} }
>
Expand Down
11 changes: 11 additions & 0 deletions assets/js/blocks/rating-filter/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';

export const blockAttributes = {
heading: {
type: 'string',
default: __( 'Filter by rating', 'woo-gutenberg-products-block' ),
},
};
34 changes: 34 additions & 0 deletions assets/js/blocks/rating-filter/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "woocommerce/rating-filter",
"version": "1.0.0",
"title": "Filter by Rating",
"description": "Enable customers to filter the product grid by rating.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"html": false,
"multiple": false
},
"example": {
"attributes": {
"isPreview": true
}
},
"attributes": {
"className": {
"type": "string",
"default": ""
},
"headingLevel": {
"type": "number",
"default": 3
},
"isPreview": {
"type": "boolean",
"default": false
}
},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
Loading