-
Notifications
You must be signed in to change notification settings - Fork 216
Filter all products block by attribute #1127
Changes from 1 commit
47a36b2
dd1fe39
7f36fdf
63c3489
2a4bf48
3f2e1df
378d861
8941166
d1695c5
b92b99d
7d90236
98fae73
4aeaed1
dcb704c
7c26a00
2792bac
9e3ef5d
56f8d95
62711cf
505ce96
b02150c
b8a7780
e95e19c
d11aa29
18562a0
c5dfef4
9b955e8
21c244d
df96edb
8aa6811
0109336
24bba5c
0af54f9
e830631
a0dc217
97561a6
171fb41
18cb5d8
81b8abb
0ba0591
ae21f20
d9a691c
1a8e4af
b1a24d6
b4bcd39
9c14ae7
2270310
bc8dbfd
ca426f4
4a02e64
cdb3f09
0867c94
1d53d56
e250eba
fba12a2
7631fb8
4e1f250
dc492a2
7785207
53ba361
8c22ba0
129aebe
4a77711
0432ef7
607aeee
ab2351f
ded36b6
e87da8c
bbcebcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,8 @@ import { | |
| useQueryStateContext, | ||
| } from '@woocommerce/base-hooks'; | ||
| import { useCallback, Fragment, useEffect, useState } from '@wordpress/element'; | ||
| import { keyBy, invert, map, trim, split, join } from 'lodash'; | ||
| import { find, join, findIndex } from 'lodash'; | ||
| import { ATTRIBUTES } from '@woocommerce/block-settings'; | ||
Aljullu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -22,6 +23,8 @@ const AttributeFilterBlock = ( { attributes } ) => { | |
| const { showCounts, attributeId, queryType } = attributes; | ||
|
|
||
| const [ options, setOptions ] = useState( [] ); | ||
| const [ checkedOptions, setCheckedOptions ] = useState( [] ); | ||
| const [ currentAttribute, setCurrentAttribute ] = useState( [] ); | ||
|
|
||
| const [ productAttributes, setProductAttributes ] = useQueryStateByKey( | ||
| 'product-grid', | ||
|
|
@@ -116,40 +119,43 @@ const AttributeFilterBlock = ( { attributes } ) => { | |
| allTermsIsLoading, | ||
mikejolley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ] ); | ||
|
|
||
| const onChange = useCallback( ( event ) => { | ||
| const checked = event.target.checked; | ||
| const slug = event.target.name; | ||
| const keyedAttributes = keyBy( productAttributes, 'attribute' ); | ||
|
|
||
| // Get current terms as array. | ||
| const terms = | ||
| keyedAttributes.pa_color && keyedAttributes.pa_color.slug | ||
| ? invert( | ||
| map( split( keyedAttributes.pa_color.slug, ',' ), trim ) | ||
| ) | ||
| : []; | ||
|
|
||
| if ( checked ) { | ||
| terms[ slug ] = 1; | ||
| } else { | ||
| delete terms[ slug ]; | ||
| } | ||
|
|
||
| keyedAttributes.pa_color = {}; | ||
| keyedAttributes.pa_color.attribute = 'pa_color'; | ||
| keyedAttributes.pa_color.slug = join( | ||
| Object.values( invert( terms ) ).filter( Boolean ), | ||
| ',' | ||
| useEffect( () => { | ||
| setCurrentAttribute( | ||
| find( ATTRIBUTES, [ 'attribute_id', attributeId + '' ] ) | ||
mikejolley marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| keyedAttributes.pa_color.operator = 'or' === queryType ? 'in' : 'and'; | ||
| }, [ attributeId ] ); | ||
|
|
||
| if ( ! keyedAttributes.pa_color.slug ) { | ||
| delete keyedAttributes.pa_color; | ||
| useEffect( () => { | ||
| const taxonomy = currentAttribute.attribute_name; | ||
| const newProductAttributes = productAttributes; | ||
| const currentQueryIndex = findIndex( newProductAttributes, [ | ||
| 'attribute', | ||
| taxonomy, | ||
| ] ); | ||
|
|
||
| const updatedQuery = { | ||
| attribute: taxonomy, | ||
| operator: 'or' === queryType ? 'in' : 'and', | ||
| slug: join( checkedOptions, ',' ), | ||
| }; | ||
|
|
||
| if ( ! updatedQuery.slug ) { | ||
| delete newProductAttributes[ currentQueryIndex ]; | ||
| } else { | ||
| newProductAttributes[ currentQueryIndex ] = updatedQuery; | ||
| } | ||
|
|
||
| setProductAttributes( Object.values( keyedAttributes ) ); | ||
| setProductAttributes( newProductAttributes ); | ||
| }, [ checkedOptions, currentAttribute, productAttributes ] ); | ||
|
|
||
| const onChange = useCallback( ( checked ) => { | ||
| setCheckedOptions( checked ); | ||
| }, [] ); | ||
|
|
||
| if ( ! currentAttribute ) { | ||
| return null; | ||
| } | ||
mikejolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <div className="wc-block-attribute-filter"> | ||
| <CheckboxList | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.