All files / components Suggestions.js

100% Statements 11/11
50% Branches 1/2
100% Functions 5/5
100% Lines 9/9

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                            2x   2x         4x           4x                                       1x     1x   1x 1x 1x              
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import randomColor from 'randomcolor';
import { setInput, getSuggestions } from '../actions/formActions';
import { getGifs } from '../actions/resultActions';
 
type Props = {
  suggestions: string[],
  selectSuggestion: Function,
};
 
export class Suggestions extends React.Component<Props> {
  render() {
    const { suggestions, selectSuggestion } = this.props;
 
    return (
      <>
        {suggestions.length > 0 ? (
          <div className="gifpicker__suggestions">
            {suggestions.map((el, i) => {
              const buttonStyle = {
                backgroundColor: randomColor({
                  luminosity: 'light',
                  format: 'rgb', // e.g. 'rgb(225,200,20)'
                }),
              };
              return (
                <button
                  type="button"
                  key={i}
                  style={buttonStyle}
                  className="gifpicker__suggestions__button"
                  onClick={selectSuggestion}
                >
                  {el}
                </button>
              );
            })}
          </div>
        ) : (
          <div></div>
        )}
      </>
    );
  }
}
export const mapStateToProps = (state: Object) => ({
  suggestions: state.result.suggestions,
});
export const mapDispatchToProps = (dispatch: Function) => ({
  selectSuggestion: (e: Object) => {
    dispatch(setInput(e.target.innerText));
    dispatch(getSuggestions());
    dispatch(getGifs());
  },
});
export default connect<*, *, *, *, *, *>(
  mapStateToProps,
  mapDispatchToProps
)(Suggestions);