From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- finished-application/frontend/components/Search.js | 143 +++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 finished-application/frontend/components/Search.js (limited to 'finished-application/frontend/components/Search.js') diff --git a/finished-application/frontend/components/Search.js b/finished-application/frontend/components/Search.js new file mode 100644 index 0000000..a972db2 --- /dev/null +++ b/finished-application/frontend/components/Search.js @@ -0,0 +1,143 @@ +import React from 'react'; +import Downshift from 'downshift'; +import Router from 'next/router'; +import { ApolloConsumer } from 'react-apollo'; +import gql from 'graphql-tag'; +import styled, { keyframes } from 'styled-components'; +import debounce from 'lodash.debounce'; + +const SEARCH_ITEMS_QUERY = gql` + query SEARCH_ITEMS_QUERY($searchTerm: String!) { + items(where: { OR: [{ title_contains: $searchTerm }, { description_contains: $searchTerm }] }) { + id + image + title + } + } +`; +function routeToItem(item) { + Router.push({ + pathname: '/item', + query: { + id: item.id, + }, + }); +} + +const DropDown = styled.div` + position: absolute; + width: 100%; + z-index: 2; + border: 1px solid ${props => props.theme.lightgrey}; +`; + +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 glow = keyframes` + from { + box-shadow: 0 0 0px yellow; + } + + to { + box-shadow: 0 0 10px 1px yellow; + } +`; + +const SearchStyles = styled.div` + position: relative; + input { + width: 100%; + padding: 10px; + border: 0; + font-size: 2rem; + &.loading { + animation: ${glow} 0.5s ease-in-out infinite alternate; + } + } +`; + +class AutoComplete extends React.Component { + state = { + items: [], + loading: false, + }; + onChange = async (e, client) => { + if (!e.target.value) { + return this.setState({ items: [] }); + } + this.setState({ loading: true }); + this.search(client, e.target.value); + }; + + search = debounce(async (client, searchTerm) => { + const res = await client.query({ + query: SEARCH_ITEMS_QUERY, + variables: { searchTerm }, + }); + this.setState({ items: res.data.items, loading: false }); + }, 350); + + render() { + return ( + + (i === null ? '' : i.title)}> + {({ getInputProps, getItemProps, isOpen, inputValue, highlightedIndex }) => ( +
+ {/* This is the searchInput */} + + {client => ( + { + e.persist(); + this.onChange(e, client); + }, + })} + /> + )} + + {/* This is the Dropdown */} + {isOpen && ( + + {this.state.items.map((item, index) => ( + + {item.title} + {item.title} + + ))} + {/* Found Nothing State */} + {!this.state.items.length && + !this.state.loading && ( + Nothing Found for {inputValue}... + )} + + )} +
+ )} +
+
+ ); + } +} + +export default AutoComplete; +export { SEARCH_ITEMS_QUERY }; -- cgit v1.3