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 51 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
26 changes: 26 additions & 0 deletions assets/js/blocks/active-filters/block-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* External dependencies
*/
import { useColorProps } from '@woocommerce/base-hooks';
import { isString } from '@woocommerce/types';

/**
* Internal dependencies
*/
import Block from './block';
import { parseAttributes } from './utils';

const BlockWrapper = ( props: Record< string, unknown > ) => {
const colorProps = useColorProps( props );

return (
<div
className={ isString( props.className ) ? props.className : '' }
style={ { ...colorProps.style } }
>
<Block isEditor={ false } attributes={ parseAttributes( props ) } />
</div>
);
};

export default BlockWrapper;
Comment on lines +1 to +26
Copy link
Member

Choose a reason for hiding this comment

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

This file and wrapper files of other filter blocks are identical, so there is room for improvement, refactor here.

6 changes: 4 additions & 2 deletions assets/js/blocks/active-filters/block.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"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
}
},
"lock": false
Copy link
Contributor Author

@tjcafferkey tjcafferkey Oct 6, 2022

Choose a reason for hiding this comment

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

Just to confirm my assumption: We set the lock status to false initially for users who already have the block inserted, but once they upgrade to the new inner blocks version we set this to true so users cannot remove the inner blocks?

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 disabling the lock support which only disables the ability to toggle the lock UI of the block, not the block locking itself.

When users upgrade or add the new filter block, we lock the remove of control blocks by setting the lock attribute object.

We can skip disabling lock support here, but then users can toggle the filter control blocks locking using the lock UI.

Copy link
Member

@dinhtungdu dinhtungdu Oct 6, 2022

Choose a reason for hiding this comment

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

With lock support disabled: ("lock": false)

image

With lock support enabled: (default)

image

},
"attributes": {
"displayStyle": {
Expand Down
10 changes: 8 additions & 2 deletions assets/js/blocks/active-filters/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import ActiveAttributeFilters from './active-attribute-filters';
import FilterPlaceholders from './filter-placeholders';
import { Attributes } from './types';
import { useSetWraperVisibility } from '../filter-wrapper/context';

/**
* Component displaying active filters.
Expand All @@ -50,6 +51,7 @@ const ActiveFiltersBlock = ( {
attributes: Attributes;
isEditor?: boolean;
} ) => {
const setWrapperVisibility = useSetWraperVisibility();
const isMounted = useIsMounted();
const componentHasMounted = isMounted();
const filteringForPhpTemplate = getSettingWithCoercion(
Expand Down Expand Up @@ -178,9 +180,9 @@ const ActiveFiltersBlock = ( {
);
} );
}, [
componentHasMounted,
setIsLoading,
productAttributes,
componentHasMounted,
STORE_ATTRIBUTES,
blockAttributes.displayStyle,
] );

Expand Down Expand Up @@ -259,6 +261,7 @@ const ActiveFiltersBlock = ( {
};

if ( ! shouldShowLoadingPlaceholders && ! hasFilters() && ! isEditor ) {
setWrapperVisibility( false );
return null;
}

Expand All @@ -284,9 +287,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 { UpgradeNotice } 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 } )
}
<UpgradeNotice
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
13 changes: 2 additions & 11 deletions assets/js/blocks/active-filters/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ import { renderFrontend } from '@woocommerce/base-utils';
* Internal dependencies
*/
import Block from './block';
import metadata from './block.json';
import { blockAttributes } from './attributes';
import { parseAttributes } from './utils';

const getProps = ( el: HTMLElement ) => {
return {
attributes: {
displayStyle:
el.dataset.displayStyle ||
metadata.attributes.displayStyle.default,
heading: el.dataset.heading || blockAttributes.heading.default,
headingLevel: el.dataset.headingLevel
? parseInt( el.dataset.headingLevel, 10 )
: metadata.attributes.headingLevel.default,
},
attributes: parseAttributes( el.dataset ),
isEditor: false,
};
};
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
19 changes: 19 additions & 0 deletions assets/js/blocks/active-filters/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import Label from '@woocommerce/base-components/label';
import { getQueryArgs, addQueryArgs, removeQueryArgs } from '@wordpress/url';
import { changeUrl } from '@woocommerce/utils';
import { Icon, closeSmall } from '@wordpress/icons';
import { isString } from '@woocommerce/types';

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

/**
* Format a min/max price range to display.
Expand Down Expand Up @@ -277,3 +283,16 @@ export const urlContainsAttributeFilter = (

return filterIsInUrl;
};

export const parseAttributes = ( data: Record< string, unknown > ) => {
return {
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,
};
};
26 changes: 26 additions & 0 deletions assets/js/blocks/attribute-filter/block-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* External dependencies
*/
import { useColorProps } from '@woocommerce/base-hooks';
import { isString } from '@woocommerce/types';

/**
* Internal dependencies
*/
import Block from './block';
import { parseAttributes } from './utils';

const BlockWrapper = ( props: Record< string, unknown > ) => {
const colorProps = useColorProps( props );

return (
<div
className={ isString( props.className ) ? props.className : '' }
style={ { ...colorProps.style } }
>
<Block isEditor={ false } attributes={ parseAttributes( props ) } />
</div>
);
};

export default BlockWrapper;
Copy link
Contributor

Choose a reason for hiding this comment

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

We decide to avoid default export (pdCMQf-sZ-p2)

Copy link
Member

Choose a reason for hiding this comment

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

We need default export here for lazy import in register-components.ts.

8 changes: 5 additions & 3 deletions assets/js/blocks/attribute-filter/block.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"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,
"lock": false
},
"example": {
"attributes": {
Expand Down
8 changes: 8 additions & 0 deletions assets/js/blocks/attribute-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
} from './utils';
import { BlockAttributes, DisplayOption } from './types';
import CheckboxFilter from './checkbox-filter';
import { useSetWraperVisibility } from '../filter-wrapper/context';

/**
* Formats filter values into a string for the URL parameters needed for filtering PHP templates.
Expand Down Expand Up @@ -468,7 +469,10 @@ const AttributeFilterBlock = ( {
filteringForPhpTemplate,
] );

const setWrapperVisibility = useSetWraperVisibility();

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

Expand All @@ -486,6 +490,7 @@ const AttributeFilterBlock = ( {
</Notice>
);
}
setWrapperVisibility( false );
return null;
}

Expand Down Expand Up @@ -513,6 +518,7 @@ const AttributeFilterBlock = ( {
( termsLoading || countsLoading ) && displayedOptions.length === 0;

if ( ! isLoading && displayedOptions.length === 0 ) {
setWrapperVisibility( false );
return null;
}

Expand All @@ -528,6 +534,8 @@ const AttributeFilterBlock = ( {
heading
);

setWrapperVisibility( true );

return (
<>
{ ! isEditor && blockAttributes.heading && filterHeading }
Expand Down
Loading