Created
September 17, 2019 19:12
-
-
Save stoneboyindc/c18e5f3a45a5f588194fadf94b493e3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import SearchForm from './SearchForm/SearchForm'; | |
import DisplayList from './DisplayList/DisplayList'; | |
import './App.css'; | |
const searchURL = 'https://www.googleapis.com/books/v1/volumes' | |
function formatQueryParams(params) { | |
const queryItems = Object.keys(params) | |
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) | |
return queryItems.join('&'); | |
} | |
class App extends Component { | |
formtheUrl() { | |
const params = { | |
q: this.state.searchTerm | |
}; | |
const queryString = formatQueryParams(params) | |
const url = searchURL + '?' + queryString; | |
fetch(url) | |
.then(res => { | |
if(!res.ok) { | |
throw new Error('Something went wrong, please try again later.'); | |
} | |
return res; | |
}) | |
.then(res => res.json()) | |
.then(data => { | |
this.setState({ | |
books: data, | |
error: null | |
}); | |
}) | |
.catch(err => { | |
this.setState({ | |
error: err.message | |
}); | |
}); | |
} | |
updateSearchTerm(e) { | |
this.setState({ | |
searchTerm: e | |
}) | |
console.log(e) | |
} | |
submitTerm(e) { | |
e.preventDefault(); | |
console.log('submitted', this.state.searchTerm) | |
this.setState({ | |
searchTerm: e | |
}) | |
this.formtheUrl(); | |
} | |
updateFilterOption(e) { | |
this.setState({ | |
filterOption : e | |
}) | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
searchTerm: '', | |
filterOption: 'true', | |
books: [] | |
}; | |
this.updateFilterOption= this.updateFilterOption.bind(this) | |
this.updateSearchTerm= this.updateSearchTerm.bind(this) | |
this.submitTerm= this.submitTerm.bind(this) | |
// this.formtheUrl= this.formtheUrl.bind(this) | |
} | |
render() { | |
return ( | |
<div className="App"> | |
<h1>Google Book Search</h1> | |
<SearchForm | |
searchTerm={this.state.searchTerm} | |
filterOption={this.state.filterOption} | |
handleUpdate={this.updateSearchTerm} | |
handleFilterChange={this.updateFilterOption} | |
handleSubmission={this.submitTerm} | |
/> | |
<DisplayList booksretrieved={this.state.books}/> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment