All files / components/Autosuggest index.js

31.25% Statements 15/48
16.13% Branches 10/62
41.67% Functions 5/12
29.79% Lines 14/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                  1x 1x           1x                                                                         1x                                       1x             1x               1x 1x                     2x 3x               1x                                 1x                     1x                                     1x                  
import React, { Component } from 'react';
import PropTypes from 'proptypes';
import Dropdown from '../Dropdown';
import { UnbxdConnect } from '../../core';
import { debounce } from '../../helper/generic';
import { searchProducts } from '../../core/actions';
 
class Autosuggest extends Component {
  constructor(props) {
    super(props);
    this.state = {
      q: (this.props.currentSearch && this.props.currentSearch.q && this.props.currentSearch.q !== '*') ? this.props.currentSearch.q : '',
      products: [],
      inFocus: 0,
      dropdown: props.dropdown ? Dropdown : false,
    };
    this.fetchResults = debounce((value) => {
      const params = {};
      this.setState({
        inFocus: 0,
      });
      params.q = value;
      let action;
      if (this.props.updateSearch) {
        action = 'ADD';
      }
      if (this.props.dropdown && this.props.limit) {
        params.rows = this.props.limit;
      }
      if ((this.props.dropdown && params.q.length > 0) || this.props.updateSearch) {
        searchProducts(params, this.props.dispatch, action, (results) => {
          if (this.props.updateSearch) {
            this.props.dispatch({
              type: 'UPDATE_QUERY',
              query: params.q,
            });
          }
          if (this.props.dropdown) {
            const { products } = results.response;
            if (products.length > 0) {
              this.setState({
                products,
              });
            }
          }
        });
      }
      if (this.props.dropdown && params.q.length === 0) {
        this.setState({
          products: [],
        });
      }
    }, 300);
    this.move = (e) => {
      if (this.props.dropdown) {
        const { key } = e;
        let { inFocus } = this.state;
        if (this.state.products.length > 0 && (key === 'ArrowDown' || key === 'ArrowUp')) {
          if (key === 'ArrowDown') {
            inFocus = (inFocus + 1 <= this.state.products.length) ? inFocus + 1 : 1;
          }
          if (key === 'ArrowUp') {
            inFocus = (inFocus - 1 > 0) ? inFocus - 1 : this.state.products.length;
          }
          this.setState({
            inFocus,
          });
        }
        if (key === 'Enter' && this.state.inFocus > 0) {
          // Todo onclick of enter select the element
        }
      }
    };
    this.closeDropdown = () => {
      this.setState({
        inFocus: 0,
        products: [],
        q: (this.props.currentSearch && this.props.currentSearch.q && this.props.currentSearch.q !== '*') ? this.props.currentSearch.q : '',
      });
    };
    this.clickOutSide = (e) => {
      if (!this.suggest.contains(e.target)) {
        this.closeDropdown();
      }
    };
  }
 
  componentDidMount() {
    Eif (this.props.dropdown) {
      document.addEventListener('click', this.clickOutSide);
    }
  }
 
  componentWillUnmount() {
    if (this.props.dropdown) {
      document.removeEventListener('click', this.clickOutSide);
    }
  }
 
  render() {
    return (
      <section className="ub-suggest" ref={(c) => { this.suggest = c; }}>
        <input
          className="ub-suggest-input"
          placeholder={this.props.placeholder}
          type="text"
          name="query"
          value={this.state.q}
          onChange={(e) => {
            this.setState({ q: e.target.value }); this.fetchResults(e.target.value);
          }}
          onKeyUp={this.move}
        />
        { this.props.dropdown && !this.props.updateSearch && this.state.dropdown &&
          <this.state.dropdown
            showThumbPreview={this.props.showThumbPreview}
            products={this.state.products}
            keywordMapping={this.props.keywordMapping}
            inFocus={this.state.inFocus}
          />
        }
      </section>
    );
  }
}
 
Autosuggest.defaultProps = {
  updateSearch: false,
  dropdown: true,
  showThumbPreview: false,
  currentSearch: {
    q: '*',
  },
  placeholder: 'What are you looking for ?',
  limit: 10,
};
 
Autosuggest.propTypes = {
  updateSearch: PropTypes.bool,
  dropdown: PropTypes.bool,
  showThumbPreview: PropTypes.bool,
  keywordMapping: PropTypes.shape({
    name: PropTypes.string,
    image: PropTypes.string,
    price: PropTypes.string,
    url: PropTypes.string,
    id: PropTypes.string,
  }).isRequired,
  currentSearch: PropTypes.shape({
    q: PropTypes.string,
  }),
  dispatch: PropTypes.func.isRequired,
  limit: PropTypes.number,
  placeholder: PropTypes.string,
};
 
const mapStateToProps = state => (
  {
    currentSearch: state.currentSearch,
  }
);
 
export { Autosuggest as AutosuggestDumb };
 
export default UnbxdConnect(mapStateToProps, null)(Autosuggest);