Product Collection: fix "Reset All" button in Editor filters (#46226)

* Add deselect callabck to attribute filter

* Add deselect callabck to created filter

* Add deselect callabck to featured filter

* Add deselect callabck to handpicked filter

* Cleanup deselect callabck to keyword filter

* Add deselect callabck to on salefilter

* Add deselect callabck to order by option

* Add deselect callabck to stock status filter

* Add deselect callabck to price range filter

* Add deselect callabck to price taxonomies filter

* Call all the reset filer callbacks on reset

* Add changelog

* Remove unused variable

* Replace hardcoded default filter values with the DEFAULT_FILTERS constant
This commit is contained in:
Karol Manijak 2024-04-05 13:16:00 +02:00 committed by GitHub
parent 66ac06e9fc
commit 1c4b793f06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 113 additions and 51 deletions

View File

@ -94,15 +94,25 @@ export const getDefaultSettings = (
query: getDefaultQuery( currentAttributes.query ), query: getDefaultQuery( currentAttributes.query ),
} ); } );
export const DEFAULT_FILTERS: Partial< ProductCollectionQuery > = { export const DEFAULT_FILTERS: Pick<
ProductCollectionQuery,
| 'woocommerceOnSale'
| 'woocommerceStockStatus'
| 'woocommerceAttributes'
| 'woocommerceHandPickedProducts'
| 'taxQuery'
| 'featured'
| 'timeFrame'
| 'priceRange'
> = {
woocommerceOnSale: DEFAULT_QUERY.woocommerceOnSale, woocommerceOnSale: DEFAULT_QUERY.woocommerceOnSale,
woocommerceStockStatus: getDefaultStockStatuses(), woocommerceStockStatus: DEFAULT_QUERY.woocommerceStockStatus,
woocommerceAttributes: [], woocommerceAttributes: DEFAULT_QUERY.woocommerceAttributes,
woocommerceHandPickedProducts: DEFAULT_QUERY.woocommerceHandPickedProducts,
taxQuery: DEFAULT_QUERY.taxQuery, taxQuery: DEFAULT_QUERY.taxQuery,
woocommerceHandPickedProducts: [],
featured: DEFAULT_QUERY.featured, featured: DEFAULT_QUERY.featured,
timeFrame: undefined, timeFrame: DEFAULT_QUERY.timeFrame,
priceRange: undefined, priceRange: DEFAULT_QUERY.priceRange,
}; };
/** /**

View File

@ -16,6 +16,7 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../types'; import { QueryControlProps } from '../../types';
import { DEFAULT_FILTERS } from '../../constants';
const EDIT_ATTRIBUTES_URL = `${ ADMIN_URL }edit.php?post_type=product&page=product_attributes`; const EDIT_ATTRIBUTES_URL = `${ ADMIN_URL }edit.php?post_type=product&page=product_attributes`;
@ -30,10 +31,18 @@ const AttributesControl = ( {
} ) } )
); );
const deselectCallback = () => {
setQueryAttribute( {
woocommerceAttributes: DEFAULT_FILTERS.woocommerceAttributes,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Product Attributes', 'woocommerce' ) } label={ __( 'Product Attributes', 'woocommerce' ) }
hasValue={ () => !! woocommerceAttributes?.length } hasValue={ () => !! woocommerceAttributes?.length }
onDeselect={ deselectCallback }
resetAllFilter={ deselectCallback }
> >
<ProductAttributeTermControl <ProductAttributeTermControl
messages={ { messages={ {

View File

@ -23,20 +23,24 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { ETimeFrameOperator, QueryControlProps } from '../../types'; import { ETimeFrameOperator, QueryControlProps } from '../../types';
import { DEFAULT_FILTERS } from '../../constants';
const CreatedControl = ( props: QueryControlProps ) => { const CreatedControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props; const { query, setQueryAttribute } = props;
const { timeFrame } = query; const { timeFrame } = query;
const deselectCallback = () => {
setQueryAttribute( {
timeFrame: DEFAULT_FILTERS.timeFrame,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Created', 'woocommerce' ) } label={ __( 'Created', 'woocommerce' ) }
hasValue={ () => timeFrame?.operator && timeFrame?.value } hasValue={ () => timeFrame?.operator && timeFrame?.value }
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { resetAllFilter={ deselectCallback }
timeFrame: undefined,
} );
} }
> >
<Flex direction="column" gap={ 3 }> <Flex direction="column" gap={ 3 }>
<FlexItem> <FlexItem>

View File

@ -14,19 +14,23 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../types'; import { QueryControlProps } from '../../types';
import { DEFAULT_FILTERS } from '../../constants';
const FeaturedProductsControl = ( props: QueryControlProps ) => { const FeaturedProductsControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props; const { query, setQueryAttribute } = props;
const deselectCallback = () => {
setQueryAttribute( {
featured: DEFAULT_FILTERS.featured,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Featured', 'woocommerce' ) } label={ __( 'Featured', 'woocommerce' ) }
hasValue={ () => query.featured === true } hasValue={ () => query.featured === true }
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { resetAllFilter={ deselectCallback }
featured: false,
} );
} }
> >
<BaseControl <BaseControl
id="product-collection-featured-products-control" id="product-collection-featured-products-control"

View File

@ -17,6 +17,7 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../types'; import { QueryControlProps } from '../../types';
import { DEFAULT_FILTERS } from '../../constants';
/** /**
* Returns: * Returns:
@ -115,15 +116,19 @@ const HandPickedProductsControl = ( {
return decodeEntities( product?.name ) || ''; return decodeEntities( product?.name ) || '';
}; };
const deselectCallback = () => {
setQueryAttribute( {
woocommerceHandPickedProducts:
DEFAULT_FILTERS.woocommerceHandPickedProducts,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Hand-picked Products', 'woocommerce' ) } label={ __( 'Hand-picked Products', 'woocommerce' ) }
hasValue={ () => !! selectedProductIds?.length } hasValue={ () => !! selectedProductIds?.length }
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { resetAllFilter={ deselectCallback }
woocommerceHandPickedProducts: [],
} );
} }
> >
<FormTokenField <FormTokenField
disabled={ ! productsMap.size } disabled={ ! productsMap.size }

View File

@ -31,7 +31,7 @@ import {
FilterName, FilterName,
} from '../../types'; } from '../../types';
import { setQueryAttribute } from '../../utils'; import { setQueryAttribute } from '../../utils';
import { DEFAULT_FILTERS, getDefaultSettings } from '../../constants'; import { getDefaultSettings } from '../../constants';
import UpgradeNotice from './upgrade-notice'; import UpgradeNotice from './upgrade-notice';
import ColumnsControl from './columns-control'; import ColumnsControl from './columns-control';
import InheritQueryControl from './inherit-query-control'; import InheritQueryControl from './inherit-query-control';
@ -107,10 +107,9 @@ const ProductCollectionInspectorControls = (
<ToolsPanel <ToolsPanel
label={ __( 'Filters', 'woocommerce' ) } label={ __( 'Filters', 'woocommerce' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => { resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS ); resetAllFilters.forEach( ( resetFilter ) => {
resetAllFilters.forEach( ( resetFilter ) => resetFilter();
resetFilter() } );
);
} } } }
className="wc-block-editor-product-collection-inspector-toolspanel__filters" className="wc-block-editor-product-collection-inspector-toolspanel__filters"
> >

View File

@ -33,12 +33,16 @@ const KeywordControl = ( props: QueryControlProps ) => {
return onChangeDebounced.cancel; return onChangeDebounced.cancel;
}, [ querySearch, onChangeDebounced ] ); }, [ querySearch, onChangeDebounced ] );
const deselectCallback = () => {
setQuerySearch( '' );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
hasValue={ () => !! querySearch } hasValue={ () => !! querySearch }
label={ __( 'Keyword', 'woocommerce' ) } label={ __( 'Keyword', 'woocommerce' ) }
onDeselect={ () => setQuerySearch( '' ) } onDeselect={ deselectCallback }
resetAllFilter={ () => setQuerySearch( '' ) } resetAllFilter={ deselectCallback }
> >
<TextControl <TextControl
label={ __( 'Keyword', 'woocommerce' ) } label={ __( 'Keyword', 'woocommerce' ) }

View File

@ -13,20 +13,24 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../types'; import { QueryControlProps } from '../../types';
import { DEFAULT_FILTERS } from '../../constants';
const OnSaleControl = ( props: QueryControlProps ) => { const OnSaleControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props; const { query, setQueryAttribute } = props;
const deselectCallback = () => {
setQueryAttribute( {
woocommerceOnSale: DEFAULT_FILTERS.woocommerceOnSale,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'On Sale', 'woocommerce' ) } label={ __( 'On Sale', 'woocommerce' ) }
hasValue={ () => query.woocommerceOnSale === true } hasValue={ () => query.woocommerceOnSale === true }
isShownByDefault isShownByDefault
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { resetAllFilter={ deselectCallback }
woocommerceOnSale: false,
} );
} }
> >
<ToggleControl <ToggleControl
label={ __( 'Show only products on sale', 'woocommerce' ) } label={ __( 'Show only products on sale', 'woocommerce' ) }

View File

@ -51,6 +51,10 @@ const OrderByControl = ( props: QueryControlProps ) => {
const { order, orderBy } = query; const { order, orderBy } = query;
const defaultQuery = getDefaultQuery( query ); const defaultQuery = getDefaultQuery( query );
const deselectCallback = () => {
setQueryAttribute( { orderBy: defaultQuery.orderBy } );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Order by', 'woocommerce' ) } label={ __( 'Order by', 'woocommerce' ) }
@ -59,9 +63,8 @@ const OrderByControl = ( props: QueryControlProps ) => {
orderBy !== defaultQuery?.orderBy orderBy !== defaultQuery?.orderBy
} }
isShownByDefault isShownByDefault
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( defaultQuery ); resetAllFilter={ deselectCallback }
} }
> >
<SelectControl <SelectControl
value={ `${ orderBy }/${ order }` } value={ `${ orderBy }/${ order }` }

View File

@ -15,6 +15,7 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../../types'; import { QueryControlProps } from '../../../types';
import { DEFAULT_FILTERS } from '../../../constants';
import PriceTextField from './PriceTextField'; import PriceTextField from './PriceTextField';
const PriceRangeControl = ( props: QueryControlProps ) => { const PriceRangeControl = ( props: QueryControlProps ) => {
@ -22,15 +23,18 @@ const PriceRangeControl = ( props: QueryControlProps ) => {
const value = query.priceRange; const value = query.priceRange;
const deselectCallback = () => {
setQueryAttribute( { priceRange: DEFAULT_FILTERS.priceRange } );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Price Range', 'woocommerce' ) } label={ __( 'Price Range', 'woocommerce' ) }
hasValue={ () => { hasValue={ () => {
return value?.min !== undefined || value?.max !== undefined; return value?.min !== undefined || value?.max !== undefined;
} } } }
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { priceRange: undefined } ); resetAllFilter={ deselectCallback }
} }
className="wc-block-product-price-range-control" className="wc-block-product-price-range-control"
> >
<BaseControl.VisualLabel> <BaseControl.VisualLabel>

View File

@ -14,7 +14,11 @@ import {
* Internal dependencies * Internal dependencies
*/ */
import { QueryControlProps } from '../../types'; import { QueryControlProps } from '../../types';
import { STOCK_STATUS_OPTIONS, getDefaultStockStatuses } from '../../constants'; import {
STOCK_STATUS_OPTIONS,
DEFAULT_FILTERS,
getDefaultStockStatuses,
} from '../../constants';
/** /**
* Gets the id of a specific stock status from its text label * Gets the id of a specific stock status from its text label
@ -36,6 +40,12 @@ function getStockStatusIdByLabel( statusLabel: FormTokenField.Value ) {
const StockStatusControl = ( props: QueryControlProps ) => { const StockStatusControl = ( props: QueryControlProps ) => {
const { query, setQueryAttribute } = props; const { query, setQueryAttribute } = props;
const deselectCallback = () => {
setQueryAttribute( {
woocommerceStockStatus: DEFAULT_FILTERS.woocommerceStockStatus,
} );
};
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Stock status', 'woocommerce' ) } label={ __( 'Stock status', 'woocommerce' ) }
@ -45,11 +55,8 @@ const StockStatusControl = ( props: QueryControlProps ) => {
getDefaultStockStatuses() getDefaultStockStatuses()
) )
} }
onDeselect={ () => { onDeselect={ deselectCallback }
setQueryAttribute( { resetAllFilter={ deselectCallback }
woocommerceStockStatus: getDefaultStockStatuses(),
} );
} }
isShownByDefault isShownByDefault
> >
<FormTokenField <FormTokenField

View File

@ -17,6 +17,7 @@ import {
*/ */
import TaxonomyItem from './taxonomy-item'; import TaxonomyItem from './taxonomy-item';
import { ProductCollectionQuery } from '../../../types'; import { ProductCollectionQuery } from '../../../types';
import { DEFAULT_FILTERS } from '../../../constants';
interface TaxonomyControlProps { interface TaxonomyControlProps {
query: ProductCollectionQuery; query: ProductCollectionQuery;
@ -53,6 +54,9 @@ function TaxonomyControls( {
return null; return null;
} }
const deselectCallback = () =>
setQueryAttribute( { taxQuery: DEFAULT_FILTERS.taxQuery } );
return ( return (
<ToolsPanelItem <ToolsPanelItem
label={ __( 'Taxonomies', 'woocommerce' ) } label={ __( 'Taxonomies', 'woocommerce' ) }
@ -61,7 +65,8 @@ function TaxonomyControls( {
( terms ) => !! terms.length ( terms ) => !! terms.length
) )
} }
onDeselect={ () => setQueryAttribute( { taxQuery: {} } ) } onDeselect={ deselectCallback }
resetAllFilter={ deselectCallback }
> >
{ taxonomies.map( ( taxonomy: Taxonomy ) => { { taxonomies.map( ( taxonomy: Taxonomy ) => {
const termIds = taxQuery?.[ taxonomy.slug ] || []; const termIds = taxQuery?.[ taxonomy.slug ] || [];

View File

@ -81,11 +81,11 @@ export interface ProductCollectionQuery {
* ), * ),
* ``` * ```
*/ */
woocommerceStockStatus?: string[]; woocommerceStockStatus: string[];
woocommerceAttributes?: AttributeMetadata[]; woocommerceAttributes: AttributeMetadata[];
isProductCollectionBlock?: boolean; isProductCollectionBlock: boolean;
woocommerceHandPickedProducts?: string[]; woocommerceHandPickedProducts: string[];
priceRange?: undefined | PriceRange; priceRange: undefined | PriceRange;
} }
export type ProductCollectionEditComponentProps = export type ProductCollectionEditComponentProps =

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Product Collection: Fix the "Reset All" funtionality in Editor filters