Skip to content

Instantly share code, notes, and snippets.

@UiCandy
Created October 29, 2017 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UiCandy/56432a33fec22c75ec9b671c12ff6610 to your computer and use it in GitHub Desktop.
Save UiCandy/56432a33fec22c75ec9b671c12ff6610 to your computer and use it in GitHub Desktop.
import React from 'react';
import 'whatwg-fetch';
import { Button, Icon, Input } from 'semantic-ui-react';
class SearchBox extends React.Component {
constructor(props) {
super(props);
this.searchQuery = this.searchQuery.bind(this);
this.createCity = this.createCity.bind(this);
this.state = { search: '' };
}
createCity(e) {
e.preventDefault();
this.props.isLoading(true);
fetch(`url`)
.then((resp) => {
console.log(resp);
if (resp.ok) {
return resp.json();
}
this.props.isLoading(false);
throw new Error('API was down', resp);
})
.then((data) => {
if (this.state.search) {
this.props.addCity(data);
} else {
this.props.showCities(data);
}
this.props.isLoading(false);
localStorage.setItem('cities', JSON.stringify(data));
this.refs.cityForm.reset();
})
.catch((data) => console.log(data));
}
searchQuery(e) {
this.state.search = e.target.value || '';
}
render() {
return (
<div>
<form ref="cityForm">
<Input icon="search" placeholder="Search..."
onChange={this.searchQuery}
/>
<Button basic onClick={this.createCity}>
<Icon name="send" />
</Button>
</form>
</div>
);
}
}
SearchBox.propTypes = {
addCity: React.PropTypes.func,
showCities: React.PropTypes.func,
isLoading: React.PropTypes.func
};
export default SearchBox;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment