Now the search request updates based on the input in the text field. Super simple, need better handling and styling.

This commit is contained in:
2017-06-02 19:37:08 +02:00
parent 4f2a69f32d
commit 30dd05ebf3

View File

@@ -1,24 +1,51 @@
import React from 'react'; import React from 'react';
import MovieObject from './MovieObject.jsx';
class SearchRequest extends React.Component { class SearchRequest extends React.Component {
constructor(props){ constructor(props){
super(props) super(props)
this.state = { this.state = {
searchQuery: '' searchQuery: '',
items: []
} }
} }
componentDidMount(){
var that = this;
fetch("https://apollo.kevinmidboe.com/api/v1/plex/request?query=interstellar")
.then(response => response.json())
.then(data => this.setState({
items: data
})
).catch(err => console.error('Error load: ', err.toString()));
}
_handleKeyPress(e) { _handleKeyPress(e) {
if (e.key === 'Enter') { if (e.key === 'Enter') {
console.log('do validate'); var query = 'https://apollo.kevinmidboe.com/api/v1/plex/request?query=' + this.state.searchQuery;
fetch(query)
.then(response => response.json())
.then(data => this.setState({
items: data
})
).catch(err => console.error('Error submit: ', err.toString()));
} }
} }
printMovies(item) {
if (item != undefined) {
let a = new MovieObject(item);
return a.getElement();
}
}
handleChange(event){ handleChange(event){
this.setState({ this.setState({
searchQuery: event.target.value searchQuery: event.target.value
}) });
console.log(this.state.searchQuery);
} }
render(){ render(){
@@ -26,11 +53,16 @@ class SearchRequest extends React.Component {
<div> <div>
<input <input
type="text" type="text"
onKeyPress={this._handleKeyPress} onKeyPress={(event) => this._handleKeyPress(event)}
onChange={this.handleChange.bind(this)} onChange={event => this.handleChange(event)}
defaultValue={this.state.searchItem} value={this.state.searchQuery}
/> />
<br></br>
{this.state.searchQuery}
<br></br>
{this.state.items.map((item) => this.printMovies(item))}
</div> </div>
) )
} }