const { __ } = wp.i18n; const { Component } = wp.element; const { Dashicon } = wp.components; const { apiFetch } = wp; /** * Product data cache. * Reduces the number of API calls and makes UI smoother and faster. */ const PRODUCT_DATA = {}; /** * When the display mode is 'Specific products' search for and add products to the block. */ export class ProductsSpecificSelect extends Component { /** * Constructor. */ constructor( props ) { super( props ); this.state = { selectedProducts: props.selected_display_setting || [], }; } /** * Add a product to the list of selected products. * * @param id int Product ID. */ addOrRemoveProduct( id ) { let selectedProducts = this.state.selectedProducts; if ( ! selectedProducts.includes( id ) ) { selectedProducts.push( id ); } else { selectedProducts = selectedProducts.filter( ( product ) => product !== id ); } this.setState( { selectedProducts: selectedProducts, } ); /** * We need to copy the existing data into a new array. * We can't just push the new product onto the end of the existing array because Gutenberg seems * to do some sort of check by reference to determine whether to *actually* update the attribute * and will not update it if we just pass back the same array with an extra element on the end. */ this.props.update_display_setting_callback( selectedProducts.slice() ); } /** * Render the product specific select screen. */ render() { return (
); } } /** * Product search area */ class ProductsSpecificSearchField extends Component { /** * Constructor. */ constructor( props ) { super( props ); this.state = { searchText: '', dropdownOpen: false, }; this.updateSearchResults = this.updateSearchResults.bind( this ); this.setWrapperRef = this.setWrapperRef.bind( this ); this.handleClickOutside = this.handleClickOutside.bind( this ); this.isDropdownOpen = this.isDropdownOpen.bind( this ); } /** * Hook in the listener for closing menu when clicked outside. */ componentDidMount() { document.addEventListener( 'mousedown', this.handleClickOutside ); } /** * Remove the listener for closing menu when clicked outside. */ componentWillUnmount() { document.removeEventListener( 'mousedown', this.handleClickOutside ); } /** * Set the wrapper reference. * * @param node DOMNode */ setWrapperRef( node ) { this.wrapperRef = node; } /** * Close the menu when user clicks outside the search area. */ handleClickOutside( event ) { if ( this.wrapperRef && ! this.wrapperRef.contains( event.target ) ) { this.setState( { searchText: '', } ); } } isDropdownOpen( isOpen ) { this.setState( { dropdownOpen: !! isOpen, } ); } /** * Event handler for updating results when text is typed into the input. * * @param evt Event object. */ updateSearchResults( evt ) { this.setState( { searchText: evt.target.value, } ); } /** * Render the product search UI. */ render() { const divClass = 'wc-products-list-card__search-wrapper'; return (
); } } /** * Render product search results based on the text entered into the textbox. */ class ProductSpecificSearchResults extends Component { /** * Constructor. */ constructor( props ) { super( props ); this.state = { products: [], query: '', loaded: false, }; this.updateResults = this.updateResults.bind( this ); this.getQuery = this.getQuery.bind( this ); } /** * Get the preview when component is first loaded. */ componentDidMount() { this.updateResults(); } /** * Update the preview when component is updated. */ componentDidUpdate() { if ( this.getQuery() !== this.state.query ) { this.updateResults(); } } /** * Get the endpoint for the current state of the component. * * @return string */ getQuery() { if ( ! this.props.searchString.length ) { return ''; } return '/wc-pb/v3/products?per_page=10&status=publish&search=' + this.props.searchString; } /** * Update the search results. */ updateResults() { const self = this; const query = this.getQuery(); self.setState( { query: query, loaded: false, } ); if ( query.length ) { apiFetch( { path: query } ).then( ( products ) => { // Only update the results if they are for the latest query. if ( query === self.getQuery() ) { self.setState( { products: products, loaded: true, } ); } } ); } else { self.setState( { products: [], loaded: true, } ); } } /** * Render. */ render() { if ( ! this.state.loaded || ! this.state.query.length ) { return null; } if ( 0 === this.state.products.length ) { return { __( 'No products found' ) } ; } // Populate the cache. for ( const product of this.state.products ) { PRODUCT_DATA[ product.id ] = product; } return ; } } /** * The dropdown of search results. */ class ProductSpecificSearchResultsDropdown extends Component { /** * Set the state of the dropdown to open. */ componentDidMount() { this.props.isDropdownOpenCallback( true ); } /** * Set the state of the dropdown to closed. */ componentWillUnmount() { this.props.isDropdownOpenCallback( false ); } /** * Render dropdown. */ render() { const { products, addOrRemoveProductCallback, selectedProducts } = this.props; const productElements = []; for ( const product of products ) { productElements.push( ); } return (
{ productElements }
); } } /** * One search result. */ class ProductSpecificSearchResultsDropdownElement extends Component { /** * Constructor. */ constructor( props ) { super( props ); this.handleClick = this.handleClick.bind( this ); } /** * Add product to main list and change UI to show it was added. */ handleClick() { this.props.addOrRemoveProductCallback( this.props.product.id ); } /** * Render one result in the search results. */ render() { const product = this.props.product; const icon = this.props.selected ? : null; /* eslint-disable jsx-a11y/click-events-have-key-events */ /* eslint-disable jsx-a11y/no-static-element-interactions */ /* reason: This interface will be deprecated, the new component is accessible. */ return (
{ product.name } { icon }
); /* eslint-enable */ } } /** * List preview of selected products. */ class ProductSpecificSelectedProducts extends Component { /** * Constructor */ constructor( props ) { super( props ); this.state = { query: '', loaded: false, }; this.updateProductCache = this.updateProductCache.bind( this ); this.getQuery = this.getQuery.bind( this ); } /** * Get the preview when component is first loaded. */ componentDidMount() { this.updateProductCache(); } /** * Update the preview when component is updated. */ componentDidUpdate() { if ( this.state.loaded && this.getQuery() !== this.state.query ) { this.updateProductCache(); } } /** * Get the endpoint for the current state of the component. */ getQuery() { if ( ! this.props.productIds.length ) { return ''; } // Determine which products are not already in the cache and only fetch uncached products. const uncachedProducts = []; for ( const productId of this.props.productIds ) { if ( ! PRODUCT_DATA.hasOwnProperty( productId ) ) { uncachedProducts.push( productId ); } } return uncachedProducts.length ? '/wc-pb/v3/products?include=' + uncachedProducts.join( ',' ) : ''; } /** * Add newly fetched products to the cache. */ updateProductCache() { const self = this; const query = this.getQuery(); self.setState( { query: query, loaded: false, } ); // Add new products to cache. if ( query.length ) { apiFetch( { path: query } ).then( ( products ) => { if ( products.length ) { for ( const product of products ) { PRODUCT_DATA[ product.id ] = product; } } self.setState( { loaded: true, } ); } ); } } /** * Render. */ render() { const self = this; const productElements = []; for ( const productId of this.props.productIds ) { // Skip products that aren't in the cache yet or failed to fetch. if ( ! PRODUCT_DATA.hasOwnProperty( productId ) ) { continue; } const productData = PRODUCT_DATA[ productId ]; productElements.push(
  • { productData.name }
  • ); } return (
    { productElements.length > 0 &&

    { __( 'Selected products' ) }

    }
      { productElements }
    ); } }