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

Commit

Permalink
Fix JS error in Filter Products by Attribute block (#5301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored Dec 6, 2021
1 parent dfe4c46 commit ba2bdd0
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions assets/js/blocks/attribute-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,6 @@ const AttributeFilterBlock = ( {
queryState.attributes,
] );

const checkedQuery = useMemo( () => {
return productAttributesQuery
.filter(
( { attribute } ) => attribute === attributeObject?.taxonomy
)
.flatMap( ( { slug } ) => slug );
}, [ productAttributesQuery, attributeObject?.taxonomy ] );

const currentCheckedQuery = useShallowEqual( checkedQuery );
const previousCheckedQuery = usePrevious( currentCheckedQuery );
// Track ATTRIBUTES QUERY changes so the block reflects current filters.
useEffect( () => {
if (
! isShallowEqual( previousCheckedQuery, currentCheckedQuery ) && // checked query changed
! isShallowEqual( checked, currentCheckedQuery ) // checked query doesn't match the UI
) {
setChecked( currentCheckedQuery );
if ( ! blockAttributes.showFilterButton ) {
onSubmit( currentCheckedQuery );
}
}
}, [
checked,
currentCheckedQuery,
previousCheckedQuery,
onSubmit,
blockAttributes.showFilterButton,
] );

/**
* Returns an array of term objects that have been chosen via the checkboxes.
*/
Expand Down Expand Up @@ -230,6 +201,35 @@ const AttributeFilterBlock = ( {
]
);

const checkedQuery = useMemo( () => {
return productAttributesQuery
.filter(
( { attribute } ) => attribute === attributeObject?.taxonomy
)
.flatMap( ( { slug } ) => slug );
}, [ productAttributesQuery, attributeObject?.taxonomy ] );

const currentCheckedQuery = useShallowEqual( checkedQuery );
const previousCheckedQuery = usePrevious( currentCheckedQuery );
// Track ATTRIBUTES QUERY changes so the block reflects current filters.
useEffect( () => {
if (
! isShallowEqual( previousCheckedQuery, currentCheckedQuery ) && // checked query changed
! isShallowEqual( checked, currentCheckedQuery ) // checked query doesn't match the UI
) {
setChecked( currentCheckedQuery );
if ( ! blockAttributes.showFilterButton ) {
onSubmit( currentCheckedQuery );
}
}
}, [
checked,
currentCheckedQuery,
previousCheckedQuery,
onSubmit,
blockAttributes.showFilterButton,
] );

const multiple =
blockAttributes.displayStyle !== 'dropdown' ||
blockAttributes.queryType === 'or';
Expand Down

0 comments on commit ba2bdd0

Please sign in to comment.