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

Product Collection - Add Inherit query from template control #9485

Merged
merged 74 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
e4abb25
Add columns control to product collection block editor settings
imanish003 May 15, 2023
43b2d1c
Merge branch 'trunk' into 9356-product-collection-editor-settings-col…
imanish003 May 15, 2023
65582af
Refactor: Simplify Fallback Return in ColumnsControl Component
imanish003 May 16, 2023
e03ecf6
Feature: Add 'Order By' Control to Product Collection Inspector
imanish003 May 16, 2023
91b5cb4
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 May 16, 2023
5e5c6b1
Add more options to OrderBy type
imanish003 May 16, 2023
502295f
Add Inherit global query control to the Product Collection
kmanijak May 16, 2023
9cfa07e
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 17, 2023
7954c13
Add orderby handling on frontend & editor
imanish003 May 17, 2023
b756198
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 17, 2023
bed6be4
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 18, 2023
58c518a
Merge branch 'trunk' into add/product-collection-inherit
kmanijak May 18, 2023
6978335
Fix the condition to show query controls (they shoul appear if query …
kmanijak May 18, 2023
df9fad6
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 18, 2023
d257e8b
Make Product Collection inheirt global query in product archive templ…
kmanijak May 18, 2023
724f894
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 May 19, 2023
e5a43cf
Add 'on sale' filter and enhance settings management in product colle…
imanish003 May 19, 2023
2e77956
Merge branch 'trunk' into 9361-product-collection-filters-on-sale
imanish003 May 22, 2023
8e9ee85
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 22, 2023
1022323
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 23, 2023
229d12b
Revert "Make Product Collection inheirt global query in product archi…
imanish003 May 23, 2023
1828047
Updated 'inherit' behavior in the Product Collection block
imanish003 May 23, 2023
c2d59c1
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 24, 2023
a832d97
Add stock status filter to WooCommerce product collection block
imanish003 May 24, 2023
c4188da
Refactor Stock Status control of Product Collection block
imanish003 May 24, 2023
2d5e3f9
Merge branch 'trunk' into 9362-product-collection-filters-stock-status
imanish003 May 24, 2023
07fc6fe
Add keyword search control to Product Collection block
imanish003 May 24, 2023
1f6a425
Resolve conflicts
imanish003 May 25, 2023
751b2b4
Add product attributes filter control to ProductCollection block
imanish003 May 25, 2023
ede31c9
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 25, 2023
c71c4c5
Merge branch 'trunk' into 9363-product-collection-filters-attributes
thealexandrelara May 25, 2023
c000167
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 26, 2023
acbdc06
Resolve conflicts
imanish003 May 26, 2023
3b1b6c6
remove unused import of getDefaultStockStatuses
imanish003 May 26, 2023
287b91f
Resolve conflicts
imanish003 May 26, 2023
67446de
Delete a duplicate file
imanish003 May 26, 2023
fa957fd
Resolve conflicts
imanish003 May 26, 2023
cacd280
Remove console log
imanish003 May 29, 2023
afa0e46
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 29, 2023
aef2878
Add taxonomies control to Product collection block
imanish003 May 29, 2023
24031a6
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 May 29, 2023
f9a6813
Address PR feedback & other improvements
imanish003 May 30, 2023
7218533
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 30, 2023
7cbd3a8
Merge branch '9363-product-collection-filters-attributes' of https://…
imanish003 May 30, 2023
e049cf8
Address PR review comments
imanish003 May 30, 2023
3a8dc95
Add wc-block-editor prefix to className
imanish003 May 30, 2023
edf824b
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 30, 2023
20fdaf9
Merge branch '9363-product-collection-filters-attributes' of https://…
imanish003 May 30, 2023
9e08ade
Resolve conflicts
imanish003 May 30, 2023
c10388e
Make improvements to 'inherit' functionality in Product collection bl…
imanish003 May 30, 2023
c5b92fc
Resolve conflicts
imanish003 May 30, 2023
0fecc33
Improve product collection query inheritance and fix URL typo
imanish003 May 30, 2023
5f60aa1
Resolve conflicts
imanish003 May 30, 2023
5528cf6
Minor improvemnets
imanish003 May 30, 2023
49f4fef
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 30, 2023
872b637
Resolve conflicts
imanish003 May 30, 2023
90d133a
Add wc-block-editor- prefix with classNames
imanish003 May 30, 2023
5f1a5bb
Handle duplicate taxonomy names in Taxonomy controls
imanish003 May 31, 2023
ba0ce51
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 May 31, 2023
d1cfe70
Remove isset() in if condition as it's unnecessary
imanish003 May 31, 2023
dbe02a9
Refactor TaxonomyItem component for better readability
imanish003 May 31, 2023
1be8967
Handling for duplicate term names & other improvements
imanish003 May 31, 2023
734d251
Restructure taxonomy controls in product collection block
imanish003 Jun 1, 2023
414d379
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 Jun 1, 2023
5e6d9d9
Merge branch '9364-product-collection-filters-taxonomies' of https://…
imanish003 Jun 1, 2023
6a10f3f
Fix case insensitive search support for FormTokenField
imanish003 Jun 2, 2023
d9d7cd7
Refactor getTermIdByTermValue function and update newSuggestions mapping
imanish003 Jun 2, 2023
7f72924
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 Jun 2, 2023
7b56958
Optimize term fetching and initial search state in TaxonomyItem
imanish003 Jun 2, 2023
028aa9b
Merge branch '9364-product-collection-filters-taxonomies' into add/pr…
imanish003 Jun 2, 2023
af2de33
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 Jun 2, 2023
34762c5
Address PR comments and other improvements
imanish003 Jun 2, 2023
ef6b436
Add with types import statement
imanish003 Jun 5, 2023
b13f560
Merge branch 'trunk' into add/product-collection-inherit
imanish003 Jun 5, 2023
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
2 changes: 1 addition & 1 deletion assets/js/base/hooks/use-previous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Validation< T > {
( value: T, previousValue: T | undefined ): boolean;
}
/**
* Use Previous based on https://usehooks.com/usePrevious/.
* Use Previous based on https://usehooks.com/useprevious/.
*
* @param {*} value
* @param {Function} [validation] Function that needs to validate for the value
Expand Down
3 changes: 2 additions & 1 deletion assets/js/blocks/product-collection/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const DEFAULT_QUERY: ProductCollectionQuery = {
search: '',
exclude: [],
sticky: '',
inherit: false,
inherit: null,
taxQuery: {},
parents: [],
isProductCollectionBlock: true,
Expand All @@ -69,6 +69,7 @@ export const getDefaultSettings = (
...currentAttributes.query,
orderBy: DEFAULT_QUERY.orderBy as TProductCollectionOrderBy,
order: DEFAULT_QUERY.order as TProductCollectionOrder,
inherit: DEFAULT_QUERY.inherit,
},
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { getDefaultSettings } from '../constants';
const ColumnsControl = (
props: BlockEditProps< ProductCollectionAttributes >
) => {
if ( ! props.attributes.displayLayout ) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by this change, does it have any relation to the rest of the PR or is it a generic code cleanup? (Sorry if it might seem obvious, but wanted to make sure I'm not missing anything)

As a side note, I noticed the ColumnsControl doesn't have any title or label. Is that on purpose? I feel it's a bit confusing to have a range input field without any info about what it does:

imatge

(No need to fix it in this PR, but wanted to mention it anyway 🙂 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by this change, does it have any relation to the rest of the PR or is it a generic code cleanup? (Sorry if it might seem obvious, but wanted to make sure I'm not missing anything)

Actually, there was an issue where the displayLayout sometimes became null. Therefore, I added an early return statement. However, I have now removed it in 34762c5 as it's no longer necessary due to other changes made in the commit.

I truly appreciate your thorough PR reviews. 🙇‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, I noticed the ColumnsControl doesn't have any title or label. Is that on purpose? I feel it's a bit confusing to have a range input field without any info about what it does:

I have also added a missing label in 34762c5. Thanks for pointing this out 🙌🏻


const { type, columns } = props.attributes.displayLayout;
const showColumnsControl = type === 'flex';

Expand Down
65 changes: 40 additions & 25 deletions assets/js/blocks/product-collection/inspector-controls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
/**
* Internal dependencies
*/
import { ProductCollectionAttributes } from '../types';
import { ProductCollectionAttributes, ProductCollectionQuery } from '../types';
import ColumnsControl from './columns-control';
import InheritQueryControl from './inherit-query-control';
import OrderByControl from './order-by-control';
import OnSaleControl from './on-sale-control';
import { setQueryAttribute } from '../utils';
Expand All @@ -29,6 +30,10 @@ import TaxonomyControls from './taxonomy-controls';
const ProductCollectionInspectorControls = (
props: BlockEditProps< ProductCollectionAttributes >
) => {
const query = props.attributes.query;
const inherit = query?.inherit;
const displayQueryControls = inherit === false;

const setQueryAttributeBind = useMemo(
() => setQueryAttribute.bind( null, props ),
[ props ]
Expand All @@ -46,33 +51,43 @@ const ProductCollectionInspectorControls = (
} }
>
<ColumnsControl { ...props } />
<OrderByControl { ...props } />
</ToolsPanel>

<ToolsPanel
label={ __( 'Filters', 'woo-gutenberg-products-block' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS );
resetAllFilters.forEach( ( resetFilter ) => resetFilter() );
} }
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
>
<OnSaleControl { ...props } />
<StockStatusControl { ...props } />
<KeywordControl { ...props } />
<AttributesControl
woocommerceAttributes={
props.attributes.query &&
( props.attributes.query
.woocommerceAttributes as AttributeMetadata[] )
}
setQueryAttribute={ setQueryAttributeBind }
/>
<TaxonomyControls
<InheritQueryControl
setQueryAttribute={ setQueryAttributeBind }
query={ props.attributes.query }
query={ query }
/>
{ displayQueryControls ? (
<OrderByControl { ...props } />
) : null }
</ToolsPanel>

{ displayQueryControls ? (
<ToolsPanel
label={ __( 'Filters', 'woo-gutenberg-products-block' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS );
resetAllFilters.forEach( ( resetFilter ) =>
resetFilter()
);
} }
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
>
<OnSaleControl { ...props } />
<StockStatusControl { ...props } />
<KeywordControl { ...props } />
<AttributesControl
woocommerceAttributes={
query?.woocommerceAttributes as AttributeMetadata[]
}
setQueryAttribute={ setQueryAttributeBind }
/>
<TaxonomyControls
setQueryAttribute={ setQueryAttributeBind }
query={
props.attributes.query as ProductCollectionQuery
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could be changed to query, now that we define it in line 33, no? Also, on the first render after inserting the block, query might be undefined, so I would remove or update the type (as ProductCollectionQuery). What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, Thanks for pointing this out 🙌🏻

/>
</ToolsPanel>
) : null }
</InspectorControls>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { select } from '@wordpress/data';
import { isSiteEditorPage } from '@woocommerce/utils';
import { usePrevious } from '@woocommerce/base-hooks';
import {
ToggleControl,
// @ts-expect-error Using experimental features
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import { ProductCollectionQuery } from '../types';
import { DEFAULT_QUERY } from '../constants';

const ARCHIVE_PRODUCT_TEMPLATES = [
'woocommerce/woocommerce//archive-product',
'woocommerce/woocommerce//taxonomy-product_cat',
'woocommerce/woocommerce//taxonomy-product_tag',
'woocommerce/woocommerce//taxonomy-product_attribute',
'woocommerce/woocommerce//product-search-results',
];

const label = __(
'Inherit query from template',
'woo-gutenberg-products-block'
);

interface InheritQueryControlProps {
setQueryAttribute: ( value: Partial< ProductCollectionQuery > ) => void;
query: ProductCollectionQuery | undefined;
}

const InheritQueryControl = ( {
setQueryAttribute,
query,
}: InheritQueryControlProps ) => {
const inherit = query?.inherit;
const editSiteStore = select( 'core/edit-site' );
const currentTemplateId = editSiteStore?.getEditedPostId() as string;

const queryObjectBeforeInheritEnabled = usePrevious(
query,
( value?: ProductCollectionQuery ) => {
return value?.inherit === false;
}
);

/**
* Set inherit value when Product Collection block is first added to the page.
* We want inherit value to be true when block is added to ARCHIVE_PRODUCT_TEMPLATES
* and false when added to somewhere else.
*/
const initialValue = currentTemplateId
? ARCHIVE_PRODUCT_TEMPLATES.includes( currentTemplateId )
: false;
if ( inherit === null ) {
setQueryAttribute( {
inherit: initialValue,
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes, when adding the Product Collection block into the Site Editor, I get this JS warning in the console:

Warning: Cannot update a component (DocumentActions) while rendering a different component (InheritQueryControl). To locate the bad setState() call inside InheritQueryControl, follow the stack trace as described in...

Can you reproduce as well?

I think the error is caused by this setQueryAttribute(). If I'm understanding the code correctly, it's trying to update the state during the render function, which causes an unnecessary re-render and might cause other side effects. I think we should set the value of inherit to initialValue right when we read it, so in assets/js/blocks/product-collection/inspector-controls/index.tsx. Would that work? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also see this warning. I have relocated the code responsible for calculating the inherit value to:
edit.tsx.

Now, I am setting the inherit value while defining the default attributes. Additionally, I have made some other minor improvements in 34762c5.

What are your thoughts on the recent changes? Do they look good to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, looks good. I initially thought the useEffect() would not be necessary, but I guess because of WordPress/gutenberg#7342, there is no way we can avoid it. 😞

return null;
}

// Hide the control if not in site editor.
const isSiteEditor = isSiteEditorPage( editSiteStore );
if ( ! isSiteEditor ) {
return null;
}

return (
<ToolsPanelItem
label={ label }
hasValue={ () => inherit !== initialValue }
isShownByDefault
onDeselect={ () => {
setQueryAttribute( {
inherit: null,
} );
} }
>
<ToggleControl
label={ label }
help={ __(
'Toggle to use the global query context that is set with the current template, such as an archive or search. Disable to customize the settings independently.',
'woo-gutenberg-products-block'
) }
checked={ !! inherit }
onChange={ ( newInherit ) => {
if ( newInherit ) {
setQueryAttribute( {
...DEFAULT_QUERY,
inherit: newInherit,
} );
} else {
setQueryAttribute( {
...DEFAULT_QUERY,
...queryObjectBeforeInheritEnabled,
inherit: newInherit,
} );
}
} }
/>
</ToolsPanelItem>
);
};

export default InheritQueryControl;
4 changes: 2 additions & 2 deletions assets/js/blocks/product-collection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { AttributeMetadata } from '@woocommerce/types';

export interface ProductCollectionAttributes {
query: ProductCollectionQuery;
query?: ProductCollectionQuery;
queryId: number;
queryContext: [
{
Expand All @@ -24,7 +24,7 @@ export interface ProductCollectionDisplayLayout {
export interface ProductCollectionQuery {
author: string;
exclude: string[];
inherit: boolean;
inherit: boolean | null;
offset: number;
order: TProductCollectionOrder;
orderBy: TProductCollectionOrderBy;
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function ( $carry, $item ) {
);

return array(
// phpcs:ignore WordPress.DB.SlowDBQuery
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array_values( $grouped_attributes ),
);
}
Expand Down