-
Notifications
You must be signed in to change notification settings - Fork 219
Product Collection - Add Inherit query from template control #9485
Changes from 71 commits
e4abb25
43b2d1c
65582af
e03ecf6
91b5cb4
5e5c6b1
502295f
9cfa07e
7954c13
b756198
bed6be4
58c518a
6978335
df9fad6
d257e8b
724f894
e5a43cf
2e77956
8e9ee85
1022323
229d12b
1828047
c2d59c1
a832d97
c4188da
2d5e3f9
07fc6fe
1f6a425
751b2b4
ede31c9
c71c4c5
c000167
acbdc06
3b1b6c6
287b91f
67446de
fa957fd
cacd280
afa0e46
aef2878
24031a6
f9a6813
7218533
7cbd3a8
e049cf8
3a8dc95
edf824b
20fdaf9
9e08ade
c10388e
c5b92fc
0fecc33
5f60aa1
5528cf6
49f4fef
872b637
90d133a
5f1a5bb
ba0ce51
d1cfe70
dbe02a9
1be8967
734d251
414d379
5e6d9d9
6a10f3f
d9d7cd7
7f72924
7b56958
028aa9b
af2de33
34762c5
ef6b436
b13f560
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 ] | ||
|
@@ -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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this could be changed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated, Thanks for pointing this out 🙌🏻 |
||
/> | ||
</ToolsPanel> | ||
) : null } | ||
</InspectorControls> | ||
); | ||
}; | ||
|
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, | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Can you reproduce as well? I think the error is caused by this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, looks good. I initially thought the |
||
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; |
There was a problem hiding this comment.
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:(No need to fix it in this PR, but wanted to mention it anyway 🙂 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. 🙇♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have also added a missing label in 34762c5. Thanks for pointing this out 🙌🏻