This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Hand-Picked Products block (#7925)
Implements the ProductSelector advanced filter within the “Products (Beta)” block. The filter allows the merchant to narrow down the exact products to which all subsequent filters will be applied, mirroring the functionality of the existing “Hand-picked Products” plus all the other functionalities available from the “Products (Beta)” block.
- Loading branch information
1 parent
1832630
commit da19120
Showing
7 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
assets/js/blocks/product-query/inspector-controls/product-selector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { getProducts } from '@woocommerce/editor-components/utils'; | ||
import { ProductResponseItem } from '@woocommerce/types'; | ||
import { objectOmit } from '@woocommerce/utils'; | ||
import { useState, useEffect } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
FormTokenField, | ||
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis | ||
__experimentalToolsPanelItem as ToolsPanelItem, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ProductQueryBlock } from '../types'; | ||
import { setQueryAttribute } from '../utils'; | ||
|
||
function useProductsList() { | ||
const [ productsList, setProductsList ] = useState< ProductResponseItem[] >( | ||
[] | ||
); | ||
|
||
useEffect( () => { | ||
getProducts( { selected: [] } ).then( ( results ) => { | ||
setProductsList( results as ProductResponseItem[] ); | ||
} ); | ||
}, [] ); | ||
|
||
return productsList; | ||
} | ||
|
||
export const ProductSelector = ( props: ProductQueryBlock ) => { | ||
const { query } = props.attributes; | ||
|
||
const productsList = useProductsList(); | ||
|
||
const onTokenChange = ( values: FormTokenField.Value[] ) => { | ||
const ids = values | ||
.map( | ||
( nameOrId ) => | ||
productsList.find( | ||
( product ) => | ||
product.name === nameOrId || | ||
product.id === Number( nameOrId ) | ||
)?.id | ||
) | ||
.filter( Boolean ) | ||
.map( String ); | ||
|
||
if ( ! ids.length && props.attributes.query.include ) { | ||
const prunedQuery = objectOmit( props.attributes.query, 'include' ); | ||
|
||
setQueryAttribute( | ||
{ | ||
...props, | ||
attributes: { | ||
...props.attributes, | ||
query: prunedQuery, | ||
}, | ||
}, | ||
{} | ||
); | ||
} else { | ||
setQueryAttribute( props, { | ||
include: ids, | ||
} ); | ||
} | ||
}; | ||
|
||
return ( | ||
<ToolsPanelItem | ||
label={ __( | ||
'Hand-picked Products', | ||
'woo-gutenberg-products-block' | ||
) } | ||
hasValue={ () => query.include?.length } | ||
> | ||
<FormTokenField | ||
disabled={ ! productsList.length } | ||
displayTransform={ ( token: string ) => | ||
Number.isNaN( Number( token ) ) | ||
? token | ||
: productsList.find( | ||
( product ) => product.id === Number( token ) | ||
)?.name || '' | ||
} | ||
label={ __( | ||
'Pick some products', | ||
'woo-gutenberg-products-block' | ||
) } | ||
onChange={ onTokenChange } | ||
suggestions={ productsList.map( ( product ) => product.name ) } | ||
validateInput={ ( value: string ) => | ||
productsList.find( ( product ) => product.name === value ) | ||
} | ||
value={ | ||
! productsList.length | ||
? [ __( 'Loading…', 'woo-gutenberg-products-block' ) ] | ||
: query?.include || [] | ||
} | ||
__experimentalExpandOnFocus={ true } | ||
/> | ||
</ToolsPanelItem> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Returns an object without a key. | ||
*/ | ||
export function objectOmit< T, K extends keyof T >( obj: T, key: K ) { | ||
const { [ key ]: omit, ...rest } = obj; | ||
|
||
return rest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters