import React, { Component } from 'react'; import './search-box.css'; /** * Search for location */ class SearchBox extends Component { constructor(props) { super(props); this.state = { q: "", results: [], fetching: false } this.handleChange = this.handleChange.bind(this); this.search = this.search.bind(this); } // Update search term handleChange(e) { this.setState({ q: e.target.value }) } // Query search endpoint search(e) { e.preventDefault(); this.setState({ fetching: true }) fetch( '/search?q='+this.state.q ).then( (res) => res.json() ).then((data) => { if (data && data.results){ this.setState({ results: data.results, fetching: false }) } else { console.error(data); this.setState({ results: [], fetching: false }) } }).catch((err) => { console.error(err) this.setState({ results: [], fetching: false }) }) } render() { const resultsList = this.state.results.length? : null; return (
{ resultsList }
) } } export default SearchBox;