diff options
| author | Wes Bos <wesbos@gmail.com> | 2017-09-21 15:39:12 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2017-09-21 15:39:12 -0400 |
| commit | 3b45dd7b593825721ec9ee9f6a49c732601b1ae0 (patch) | |
| tree | a2e8e73d6b6b6bc32eda2bb79af817397e4304f5 /components/Search.js | |
| parent | 1450e579c67e3d969cb099dfe6c1cefd1e313fb5 (diff) | |
Cha Ching
Diffstat (limited to 'components/Search.js')
| -rw-r--r-- | components/Search.js | 103 |
1 files changed, 47 insertions, 56 deletions
diff --git a/components/Search.js b/components/Search.js index afab3e8..63c467e 100644 --- a/components/Search.js +++ b/components/Search.js @@ -1,78 +1,69 @@ -import Downshift from 'downshift' +import Downshift from 'downshift'; import { SEARCH_ITEMS_QUERY } from '../queries'; -import { graphql, compose } from 'react-apollo' +import { graphql, compose } from 'react-apollo'; import makeImage from '../lib/image'; -import Router from 'next/router' +import { Router } from '../routes'; import slugify from 'slugify'; function routeToItem(item) { - Router.push({ - pathname: '/item', - query: { - slug: slugify(item.title), - itemId: item.id - } - }) + Router.pushRoute('item', { + slug: slugify(item.title), + itemId: item.id, + }); } function BasicAutocomplete(props) { const { items, onChange } = props; console.log(props); return ( - <Downshift - onChange={routeToItem} - itemToString={(item) => item.title} - > - {({ - getInputProps, - getItemProps, - isOpen, - inputValue, - selectedItem, - highlightedIndex - }) => ( + <Downshift onChange={routeToItem} itemToString={item => item.title}> + {({ getInputProps, getItemProps, isOpen, inputValue, selectedItem, highlightedIndex }) => ( <div> - <input {...getInputProps({ - placeholder: 'Search For Item', - onChange: (e) => props.refetch({ searchTerm: e.target.value }), - style: { fontSize: '20px', padding: '20px', display: 'block', width: '100%' } - })} /> + <input + {...getInputProps({ + placeholder: 'Search For Item', + onChange: e => props.refetch({ searchTerm: e.target.value }), + style: { fontSize: '20px', padding: '20px', display: 'block', width: '100%' }, + })} + /> {isOpen ? ( <div> - {items - .map((item, index) => ( - <div - {...getItemProps({item})} - key={item.id} - style={{ - backgroundColor: highlightedIndex === index ? '#e8e8e8' : 'white', - borderLeft: highlightedIndex === index ? '10px solid #ffc600' : '10px solid white', - padding: '10px', - display: 'flex', - alignItems: 'center' - }} - > - <img width="50" style={{ 'margin-right': '10px'}} src={makeImage(item.image)} alt={item.title}/> - {item.title} - </div> - ))} + {items.map((item, index) => ( + <div + {...getItemProps({ item })} + key={item.id} + style={{ + backgroundColor: highlightedIndex === index ? '#e8e8e8' : 'white', + borderLeft: highlightedIndex === index ? '10px solid #ffc600' : '10px solid white', + padding: '10px', + display: 'flex', + alignItems: 'center', + }} + > + <img width="50" style={{ 'margin-right': '10px' }} src={makeImage(item.image)} alt={item.title} /> + {item.title} + </div> + ))} </div> ) : null} </div> )} </Downshift> - ) + ); } -const Search = (props) => ( - <BasicAutocomplete - items={props.searchItems.allItems} - onChange={selectedItem => console.log(selectedItem)} - refetch={props.searchItems.refetch} - /> -) +const Search = props => ( + <BasicAutocomplete + items={props.searchItems.allItems} + onChange={selectedItem => console.log(selectedItem)} + refetch={props.searchItems.refetch} + /> +); -const searchEnhancer = graphql(SEARCH_ITEMS_QUERY, { name: 'searchItems', options: { - variables: { searchTerm: 'camo' } -} }); -export default compose(searchEnhancer)(Search) +const searchEnhancer = graphql(SEARCH_ITEMS_QUERY, { + name: 'searchItems', + options: { + variables: { searchTerm: 'camo' }, + }, +}); +export default compose(searchEnhancer)(Search); |
