Client feature #14
@@ -13,12 +13,13 @@ class SearchRequest extends React.Component {
|
|||||||
|
|
||||||
componentDidMount(){
|
componentDidMount(){
|
||||||
var that = this;
|
var that = this;
|
||||||
fetch("https://apollo.kevinmidboe.com/api/v1/plex/request?query=interstellar")
|
this.setState({items: []})
|
||||||
.then(response => response.json())
|
// fetch("https://apollo.kevinmidboe.com/api/v1/plex/request?query=interstellar")
|
||||||
.then(data => this.setState({
|
// .then(response => response.json())
|
||||||
items: data
|
// .then(data => this.setState({
|
||||||
})
|
// items: data
|
||||||
).catch(err => console.error('Error load: ', err.toString()));
|
// })
|
||||||
|
// ).catch(err => console.error('Error load: ', err.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
_handleKeyPress(e) {
|
_handleKeyPress(e) {
|
||||||
@@ -27,15 +28,32 @@ class SearchRequest extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleErrors(response) {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw Error(response.statusText);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
fetchQuery() {
|
fetchQuery() {
|
||||||
var query = 'https://apollo.kevinmidboe.com/api/v1/plex/request?query=' + this.state.searchQuery;
|
var query = 'https://apollo.kevinmidboe.com/api/v1/plex/request?query=' + this.state.searchQuery;
|
||||||
|
|
||||||
fetch(query)
|
fetch(query)
|
||||||
.then(response => response.json())
|
// Check if the response is ok
|
||||||
.then(data => this.setState({
|
.then(response => this.handleErrors(response))
|
||||||
items: data
|
.then(response => response.json()) // Convert to json object and pass to next then
|
||||||
})
|
.then(data => { // Parse the data of the JSON response
|
||||||
).catch(err => console.error('Error submit: ', err.toString()));
|
if (data.length > 0) {
|
||||||
|
this.setState({
|
||||||
|
items: data
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
this.setState({
|
||||||
|
items: null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => console.log('Error submit: ', error.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
printMovies(item) {
|
printMovies(item) {
|
||||||
@@ -52,13 +70,27 @@ class SearchRequest extends React.Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mapResponseToMovies() {
|
||||||
|
// Here we have some movie response items in our state
|
||||||
|
if (this.state.items) {
|
||||||
|
console.log('something')
|
||||||
|
}
|
||||||
|
// And here we are going to print a 404 message because the response was empty
|
||||||
|
else {
|
||||||
|
console.log('nothing')
|
||||||
|
}
|
||||||
|
for (var i = this.state.items.length - 1; i >= 0; i--) {
|
||||||
|
this.printMovies(this.state.items[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render(){
|
render(){
|
||||||
return(
|
return(
|
||||||
<div>
|
<div>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
onKeyPress={(event) => this._handleKeyPress(event)}
|
onKeyPress={(event) => this._handleKeyPress(event)}
|
||||||
onChange={(event) => this.handleChange(event)}
|
onChange={event => this.handleChange(event)}
|
||||||
value={this.state.searchQuery}
|
value={this.state.searchQuery}
|
||||||
/>
|
/>
|
||||||
<button onClick={() => this.fetchQuery()}>Search</button>
|
<button onClick={() => this.fetchQuery()}>Search</button>
|
||||||
@@ -67,7 +99,7 @@ class SearchRequest extends React.Component {
|
|||||||
{this.state.searchQuery}
|
{this.state.searchQuery}
|
||||||
<br></br>
|
<br></br>
|
||||||
|
|
||||||
{this.state.items.map((item) => this.printMovies(item))}
|
{this.mapResponseToMovies()}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user