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

Convert filter blocks to inner blocks #6978

Merged
merged 66 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
0580f81
register filter wrapper block
dinhtungdu Aug 25, 2022
6ea437e
register block variation
dinhtungdu Aug 25, 2022
a81be07
rename the active filters wrapper
dinhtungdu Aug 25, 2022
8b6cb7c
prevent 404 error
dinhtungdu Aug 25, 2022
5a52b47
Revert "prevent 404 error"
dinhtungdu Aug 25, 2022
6593c13
render parent wrapper block
dinhtungdu Sep 1, 2022
b95a157
support price filter block
dinhtungdu Sep 1, 2022
a88647f
hide the active filter block from inserter
dinhtungdu Sep 1, 2022
225c0fa
Merge branch 'trunk' into try/filter-inner-blocks
dinhtungdu Sep 1, 2022
4c6f1f0
swap the title of wrapper and inner filter block for active filters
dinhtungdu Sep 1, 2022
49236d8
hide the legacy heading for the price filter
dinhtungdu Sep 1, 2022
37e78a8
update block title and description for active filters and price filter
dinhtungdu Sep 2, 2022
b5e039e
remove heading control for price filter
dinhtungdu Sep 2, 2022
b2754c7
remove heading control for active filters
dinhtungdu Sep 2, 2022
82889ff
update pattern
dinhtungdu Sep 2, 2022
3ba7237
try: upgrade button
dinhtungdu Sep 2, 2022
3d02766
limit the number of inner block to 2
dinhtungdu Sep 2, 2022
83b7fee
prevent removing the inner filter block
dinhtungdu Sep 2, 2022
00e94f1
Revert "prevent removing the inner filter block"
dinhtungdu Sep 2, 2022
e423340
convert stock filter to inner block
dinhtungdu Sep 2, 2022
c3b9c67
refactor block upgrade button to share between filter blocks
dinhtungdu Sep 2, 2022
e14fe84
update default heading
dinhtungdu Sep 2, 2022
bb6973a
update pattern
dinhtungdu Sep 2, 2022
42ac2b4
update icon and title
dinhtungdu Sep 2, 2022
1cc05c6
Merge branch 'trunk' into try/filter-inner-blocks
tjcafferkey Sep 16, 2022
0eb484c
Fix stock filter error by importing translations package
tjcafferkey Sep 16, 2022
deedc4a
Upgrade Active Filters name to Active Filter Controls
tjcafferkey Sep 16, 2022
7e6b2ea
Add upgrade support to price filter
tjcafferkey Sep 16, 2022
d1b4512
Convert attribute filter to inner block (#7101)
dinhtungdu Sep 16, 2022
311b512
Set correct attribute on the new filter blocks when they are upgraded
tjcafferkey Sep 16, 2022
0d17886
Use the Warning component to display the upgrade message so it is con…
tjcafferkey Sep 20, 2022
0827141
Merge branch 'trunk' into try/filter-inner-blocks
tjcafferkey Sep 20, 2022
69cea5b
address code review
dinhtungdu Sep 26, 2022
6731264
better detect legacy block to show the upgrade notice
dinhtungdu Sep 27, 2022
81bbadb
rename UpgradeToolbarButton to UpgradeNotice
dinhtungdu Sep 27, 2022
4e42ac4
add upgrade notice to the stock filter block
dinhtungdu Sep 27, 2022
d5ae52b
rename InnerBlockWrapper to BlockWrapper
dinhtungdu Sep 28, 2022
c0984ac
attribute-filter: control wrapper visibility
dinhtungdu Sep 28, 2022
ce3ed78
passing block attributes down to inner active filters control block
dinhtungdu Sep 28, 2022
5305611
fix styling of inner attribute filter control block
dinhtungdu Sep 28, 2022
29542e1
passing attribute to inner price filter control block
dinhtungdu Sep 28, 2022
a903151
passing down the attribute to inner stock filter control block
dinhtungdu Sep 28, 2022
e1f0f33
remove unneccessary parsing
dinhtungdu Sep 28, 2022
b3d4d56
use default scope for variations
dinhtungdu Sep 29, 2022
a44ad73
fix default attribute values
dinhtungdu Sep 29, 2022
e1a9901
use default block appender
dinhtungdu Sep 29, 2022
959c8b5
fix: lock control blocks
dinhtungdu Sep 29, 2022
5bb547a
remove dynamic title code from attribute filter block
dinhtungdu Sep 29, 2022
ddfc5d7
register active filters as variation and set it to the default that o…
dinhtungdu Sep 29, 2022
ef0b035
fix isActive for default variation
dinhtungdu Sep 29, 2022
a44513a
fix: isActive logic for the active filters block
dinhtungdu Oct 3, 2022
4e41b0a
register side effect
dinhtungdu Oct 3, 2022
f0b9ebc
fix ts error
dinhtungdu Oct 3, 2022
dd70a77
Merge branch 'trunk' into try/filter-inner-blocks
dinhtungdu Oct 4, 2022
941f1af
e2e: fix active filters block backend test
dinhtungdu Oct 4, 2022
2a9d2ee
e2e: fix frontend active filters test
dinhtungdu Oct 4, 2022
b538432
e2e: fix attribute filter test
dinhtungdu Oct 4, 2022
6a1e87d
e2e: fix price filter test
dinhtungdu Oct 4, 2022
4845321
e2e: fix stock filter test
dinhtungdu Oct 4, 2022
e861133
e2e: update fixture
dinhtungdu Oct 4, 2022
2660784
e2e: fix attribute filter test
dinhtungdu Oct 4, 2022
2e7344c
remove invalid test
dinhtungdu Oct 4, 2022
4686e91
e2e: update heading selector for price filter in backend test
dinhtungdu Oct 4, 2022
6f49f57
e2e: fixe backend price filter heading test
dinhtungdu Oct 4, 2022
c2c25c9
fix: patterns i18n
dinhtungdu Oct 6, 2022
4e91226
fix: heading level when upgrading the block
dinhtungdu Oct 6, 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/blocks/active-filters/block.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "woocommerce/active-filters",
"version": "1.0.0",
"title": "Active Product Filters",
"title": "Active Product Filter Controls",
"description": "Display the currently active product filters.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"html": false,
"multiple": false,
Copy link
Member

Choose a reason for hiding this comment

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

We're losing the ability to limit the number of Active Filters, Price Filter, and Stock Filter blocks on a page.

"inserter": false,
"color": {
"text": true,
"background": false
Expand Down
13 changes: 13 additions & 0 deletions assets/js/blocks/active-filters/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import {
import ActiveAttributeFilters from './active-attribute-filters';
import FilterPlaceholders from './filter-placeholders';
import { Attributes } from './types';
import metadata from './block.json';
import { useSetWraperVisibility } from '../filter-wrapper/context';

/**
* Component displaying active filters.
Expand All @@ -50,6 +52,13 @@ const ActiveFiltersBlock = ( {
attributes: Attributes;
isEditor?: boolean;
} ) => {
blockAttributes = {
...blockAttributes,
displayStyle:
blockAttributes?.displayStyle ||
metadata.attributes.displayStyle.default,
};
const setWrapperVisibility = useSetWraperVisibility();
const isMounted = useIsMounted();
const componentHasMounted = isMounted();
const filteringForPhpTemplate = getSettingWithCoercion(
Expand Down Expand Up @@ -259,6 +268,7 @@ const ActiveFiltersBlock = ( {
};

if ( ! shouldShowLoadingPlaceholders && ! hasFilters() && ! isEditor ) {
setWrapperVisibility( false );
dinhtungdu marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand All @@ -284,9 +294,12 @@ const ActiveFiltersBlock = ( {
);

if ( ! hasFilterableProducts ) {
setWrapperVisibility( false );
return null;
}

setWrapperVisibility( true );

const listClasses = classnames( 'wc-block-active-filters__list', {
'wc-block-active-filters__list--chips':
blockAttributes.displayStyle === 'chips',
Expand Down
42 changes: 18 additions & 24 deletions assets/js/blocks/active-filters/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
*/
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import HeadingToolbar from '@woocommerce/editor-components/heading-toolbar';
import { BlockEditProps } from '@wordpress/blocks';
import BlockTitle from '@woocommerce/editor-components/block-title';
import type { BlockEditProps } from '@wordpress/blocks';
import {
Disabled,
PanelBody,
Expand All @@ -22,10 +21,12 @@ import {
import Block from './block';
import type { Attributes } from './types';
import './editor.scss';
import { UpgradeToolbarButton } from '../filter-wrapper/upgrade';

const Edit = ( {
attributes,
setAttributes,
clientId,
}: BlockEditProps< Attributes > ) => {
const { className, displayStyle, heading, headingLevel } = attributes;

Expand Down Expand Up @@ -70,21 +71,6 @@ const Edit = ( {
) }
/>
</ToggleGroupControl>
<p>
{ __(
'Heading Level',
'woo-gutenberg-products-block'
) }
</p>
<HeadingToolbar
isCollapsed={ false }
minLevel={ 2 }
maxLevel={ 7 }
selectedLevel={ headingLevel }
onChange={ ( newLevel: Attributes[ 'headingLevel' ] ) =>
setAttributes( { headingLevel: newLevel } )
}
/>
</PanelBody>
</InspectorControls>
);
Expand All @@ -93,14 +79,22 @@ const Edit = ( {
return (
<div { ...blockProps }>
{ getInspectorControls() }
<BlockTitle
className="wc-block-active-filters__title"
headingLevel={ headingLevel }
heading={ heading }
onChange={ ( value: Attributes[ 'heading' ] ) =>
setAttributes( { heading: value } )
}
<UpgradeToolbarButton
attributes={ attributes }
clientId={ clientId }
setAttributes={ setAttributes }
filterType="active-filters"
/>
{ heading && (
<BlockTitle
className="wc-block-active-filters__title"
headingLevel={ headingLevel }
heading={ heading }
onChange={ ( value: Attributes[ 'heading' ] ) =>
setAttributes( { heading: value } )
}
/>
) }
<Disabled>
<Block attributes={ attributes } isEditor={ true } />
</Disabled>
Expand Down
5 changes: 4 additions & 1 deletion assets/js/blocks/active-filters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { blockAttributes } from './attributes';
import { Attributes } from './types';

registerBlockType( metadata, {
title: __( 'Active Product Filters', 'woo-gutenberg-products-block' ),
title: __(
'Active Product Filter Controls',
'woo-gutenberg-products-block'
),
description: __(
'Display the currently active product filters.',
'woo-gutenberg-products-block'
Expand Down
7 changes: 4 additions & 3 deletions assets/js/blocks/attribute-filter/block.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "woocommerce/attribute-filter",
"version": "1.0.0",
"title": "Filter by Attribute",
"description": "Enable customers to filter the product grid by selecting one or more attributes, such as color.",
"title": "Filter by Attribute Controls",
"description": "Allow customers to filter the grid by product attribute, such as color.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"html": false,
"color": {
"text": true,
"background": false
}
},
"inserter": false
},
"example": {
"attributes": {
Expand Down
60 changes: 29 additions & 31 deletions assets/js/blocks/attribute-filter/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Icon, category, external } from '@wordpress/icons';
import { SearchListControl } from '@woocommerce/editor-components/search-list-control';
import { sortBy } from 'lodash';
import { getAdminLink, getSetting } from '@woocommerce/settings';
import HeadingToolbar from '@woocommerce/editor-components/heading-toolbar';
import BlockTitle from '@woocommerce/editor-components/block-title';
import classnames from 'classnames';
import { SearchListItemsType } from '@woocommerce/editor-components/search-list-control/types';
Expand All @@ -37,10 +36,16 @@ import {
import Block from './block';
import './editor.scss';
import type { EditProps } from './types';
import { UpgradeToolbarButton } from '../filter-wrapper/upgrade';

const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] );

const Edit = ( { attributes, setAttributes, debouncedSpeak }: EditProps ) => {
const Edit = ( {
attributes,
setAttributes,
debouncedSpeak,
clientId,
}: EditProps ) => {
const {
attributeId,
className,
Expand Down Expand Up @@ -91,15 +96,15 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak }: EditProps ) => {
return;
}

const attributeName = productAttribute.attribute_label;
// const attributeName = productAttribute.attribute_label;
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we should remove this commented code?

Copy link
Member

Choose a reason for hiding this comment

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

We will do that eventually. I'm leaving it here while working on the dynamic title.

For the attribute filter block, the block title is updated automatically after store admins selected an attribute. We're losing this feature on this PR.

I will look at this again before other higher priority parts are addressed (E2E test, block upgrade)


setAttributes( {
attributeId: selectedId as number,
heading: sprintf(
/* translators: %s attribute name. */
__( 'Filter by %s', 'woo-gutenberg-products-block' ),
attributeName
),
// heading: sprintf(
// /* translators: %s attribute name. */
// __( 'Filter by %s', 'woo-gutenberg-products-block' ),
// attributeName
// ),
} );
};

Expand Down Expand Up @@ -183,21 +188,6 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak }: EditProps ) => {
} )
}
/>
<p>
{ __(
'Heading Level',
'woo-gutenberg-products-block'
) }
</p>
<HeadingToolbar
isCollapsed={ false }
minLevel={ 2 }
maxLevel={ 7 }
selectedLevel={ headingLevel }
onChange={ ( newLevel: number ) =>
setAttributes( { headingLevel: newLevel } )
}
/>
<ToggleGroupControl
label={ __(
'Allow selecting multiple options?',
Expand Down Expand Up @@ -412,6 +402,12 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak }: EditProps ) => {
<div { ...blockProps }>
{ getBlockControls() }
{ getInspectorControls() }
<UpgradeToolbarButton
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
filterType="attribute-filter"
/>
{ isEditing ? (
renderEditMode()
) : (
Expand All @@ -421,14 +417,16 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak }: EditProps ) => {
'wc-block-attribute-filter'
) }
>
<BlockTitle
className="wc-block-attribute-filter__title"
headingLevel={ headingLevel }
heading={ heading }
onChange={ ( value: string ) =>
setAttributes( { heading: value } )
}
/>
{ heading && (
<BlockTitle
className="wc-block-attribute-filter__title"
headingLevel={ headingLevel }
heading={ heading }
onChange={ ( value: string ) =>
setAttributes( { heading: value } )
}
/>
) }
<Disabled>
<Block attributes={ attributes } isEditor />
</Disabled>
Expand Down
20 changes: 2 additions & 18 deletions assets/js/blocks/attribute-filter/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@ import { renderFrontend } from '@woocommerce/base-utils';
* Internal dependencies
*/
import Block from './block';
import { blockAttributes } from './attributes';
import metadata from './block.json';
import { parseAttributes } from './utils';

const getProps = ( el: HTMLElement ) => {
return {
isEditor: false,
attributes: {
attributeId: parseInt( el.dataset.attributeId || '0', 10 ),
showCounts: el.dataset.showCounts === 'true',
queryType:
el.dataset.queryType || metadata.attributes.queryType.default,
heading: el.dataset.heading || blockAttributes.heading.default,
headingLevel: el.dataset.headingLevel
? parseInt( el.dataset.headingLevel, 10 )
: metadata.attributes.headingLevel.default,
displayStyle:
el.dataset.displayStyle ||
metadata.attributes.displayStyle.default,
showFilterButton: el.dataset.showFilterButton === 'true',
selectType:
el.dataset.selectType || metadata.attributes.selectType.default,
},
attributes: parseAttributes( el.dataset ),
};
};

Expand Down
5 changes: 0 additions & 5 deletions assets/js/blocks/attribute-filter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import { blockAttributes } from './attributes';
import metadata from './block.json';

registerBlockType( metadata, {
title: __( 'Filter by Attribute', 'woo-gutenberg-products-block' ),
description: __(
'Enable customers to filter the product grid by selecting one or more attributes, such as color.',
'woo-gutenberg-products-block'
),
icon: {
src: (
<Icon
Expand Down
11 changes: 11 additions & 0 deletions assets/js/blocks/attribute-filter/inner-block-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Internal dependencies
*/
import Block from './block';
import { parseAttributes } from './utils';

const InnerBlockWrapper = ( props: Record< string, unknown > ) => {
return <Block isEditor={ false } attributes={ parseAttributes( props ) } />;
};

export default InnerBlockWrapper;
38 changes: 37 additions & 1 deletion assets/js/blocks/attribute-filter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
PREFIX_QUERY_ARG_FILTER_TYPE,
PREFIX_QUERY_ARG_QUERY_TYPE,
} from '@woocommerce/utils';
import { AttributeObject } from '@woocommerce/types';
import { AttributeObject, isString } from '@woocommerce/types';

/**
* Internal dependencies
*/
import metadata from './block.json';

interface Param {
attribute: string;
Expand Down Expand Up @@ -104,3 +109,34 @@ export const formatSlug = ( slug: string ) =>
.replace( /_/g, '-' )
.replace( /-+/g, '-' )
.replace( /[^a-zA-Z0-9-]/g, '' );

export const parseAttributes = ( data: Record< string, unknown > ) => {
return {
className: isString( data?.className ) ? data.className : '',
attributeId: parseInt(
isString( data?.attributeId ) ? data.attributeId : '0',
10
),
showCounts:
data?.showCounts === 'true' ||
metadata.attributes.showCounts.default,
queryType:
( isString( data?.queryType ) && data.queryType ) ||
metadata.attributes.queryType.default,
heading: isString( data?.heading ) ? data.heading : '',
headingLevel:
( isString( data?.headingLevel ) &&
parseInt( data.headingLevel, 10 ) ) ||
metadata.attributes.headingLevel.default,
displayStyle:
( isString( data?.displayStyle ) && data.displayStyle ) ||
metadata.attributes.displayStyle.default,
showFilterButton:
data?.showFilterButton === 'true' ||
metadata.attributes.showFilterButton.default,
selectType:
( isString( data?.selectType ) && data.selectType ) ||
metadata.attributes.selectType.default,
isPreview: false,
};
};
17 changes: 17 additions & 0 deletions assets/js/blocks/filter-wrapper/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "woocommerce/filter-wrapper",
"version": "1.0.0",
"title": "Active Product Filters",
"description": "Show the currently active product filters. Works in combination with other filters blocks.",
Copy link
Member

Choose a reason for hiding this comment

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

it's probably better to register the Active Product Filters as a variation too, but we need to hide the main filter wrapper block.

"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"attributes": {
"filterType": {
"type": "string",
"default": "active-filters"
}
},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
Loading