summaryrefslogtreecommitdiffstats
path: root/frontend/components/Search.js
blob: 33017dde705c202b2938cdf0a7e803fe440cffa3 (plain)
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
import Downshift from 'downshift';
import { graphql, compose } from 'react-apollo';
import slugify from 'slugify';
import styled from 'styled-components';
import { SEARCH_ITEMS_QUERY } from '../queries';
import { Router } from '../routes';

function routeToItem(item) {
  Router.pushRoute('item', {
    slug: slugify(item.title),
    itemId: item.id,
  });
}

const DropDown = styled.div`
  position: absolute;
  width: 100%;
  z-index: 2;
  border: 1px solid ${props => props.theme.lightgrey};
  input {
    border: 0;
  }
`;

const DropDownItem = styled.div`
  border-bottom: 1px solid ${props => props.theme.lightgrey};
  background: ${props => (props.highlighted ? '#f7f7f7' : 'white')};
  padding: 1rem;
  transition: all 0.2s;
  ${props => (props.highlighted ? 'padding-left: 2rem;' : null)};
  display: flex;
  align-items: center;
  border-left: 10px solid ${props => (props.highlighted ? props.theme.lightgrey : 'white')};
  img {
    margin-right: 10px;
  }
`;

const SearchStyles = styled.div`
  position: relative;
`;

function AutoComplete(props) {
  const { items, onChange } = props;
  return (
    <Downshift onChange={routeToItem} itemToString={i => (i === null ? '' : i.title)}>
      {({ getInputProps, getItemProps, isOpen, inputValue, selectedItem, highlightedIndex }) => (
        <div>
          {/* This is the searchInput */}
          <input
            {...getInputProps({
              placeholder: 'Search For Item',
              id: 'search',
              onChange: e => {
                console.log('Change happened');
                props.refetch({ searchTerm: e.target.value });
              },
              style: { fontSize: '20px', padding: '10px', display: 'block', width: '100%' },
            })}
          />
          {isOpen ? (
            <DropDown>
              {/* This is the Dropdown */}
              {items.map((item, index) => (
                <DropDownItem {...getItemProps({ item })} key={item.id} highlighted={highlightedIndex === index}>
                  <img width="50" src={item.image} alt={item.title} />
                  {item.title}
                </DropDownItem>
              ))}
            </DropDown>
          ) : null}
        </div>
      )}
    </Downshift>
  );
}

const Search = props => (
  <SearchStyles>
    <AutoComplete
      items={props.searchItems.items}
      onChange={selectedItem => console.log(selectedItem)}
      refetch={props.searchItems.refetch}
    />
  </SearchStyles>
);

const searchEnhancer = graphql(SEARCH_ITEMS_QUERY, {
  name: 'searchItems',
  options: {
    // TODO: Why is this camo
    variables: { searchTerm: 'camo' },
  },
});
export default compose(searchEnhancer)(Search);