const { __ } = wp.i18n; const { Toolbar, withAPIData, Dropdown } = wp.components; /** * When the display mode is 'Specific products' search for and add products to the block. * * @todo Add the functionality and everything. */ export class ProductsSpecificSelect extends React.Component { /** * Constructor. */ constructor( props ) { super( props ); this.state = { selectedProducts: props.selected_display_setting || [], } } addProduct( id ) { let selectedProducts = this.state.selectedProducts; selectedProducts.push( id ); this.setState( { selectedProducts: selectedProducts } ); this.props.update_display_setting_callback( selectedProducts ); } removeProduct( id ) { let oldProducts = this.state.selectedProducts; let newProducts = []; for ( let productId of oldProducts ) { if ( productId !== id ) { newProducts.push( productId ); } } this.setState( { selectedProducts: newProducts } ); this.props.update_display_setting_callback( newProducts ); } render() { return (
); } } /** * Product search area */ class ProductsSpecificSearchField extends React.Component { constructor( props ) { super( props ); this.state = { searchText: '', } this.updateSearchResults = this.updateSearchResults.bind( this ); } updateSearchResults( evt ) { this.setState( { searchText: evt.target.value, } ); } render() { return (
{ this.setState( { searchText: '' } ); } } >X
); } } /** * Product search results based on the text entered into the textbox. */ const ProductSpecificSearchResults = withAPIData( ( props ) => { if ( ! props.searchString.length ) { return { products: [] }; } return { products: '/wc/v2/products?per_page=10&search=' + props.searchString }; } )( ( { products, addProductCallback } ) => { if ( ! products.data ) { return null; } if ( 0 === products.data.length ) { return __( 'No products found' ); } return (
); } ); /** * List preview of selected products. */ const ProductSpecificSelectedProducts = withAPIData( ( props ) => { if ( ! props.products.length ) { return { products: [] }; } return { products: '/wc/v2/products?include=' + props.products.join( ',' ) }; } )( ( { products, removeProductCallback } ) => { if ( ! products.data ) { return null; } if ( 0 === products.data.length ) { return __( 'No products selected' ); } return (
); } );