This now contains the button and request function for getting torrents matching a serach query given by the name of the elment. The result is passed to the child component torrentTable which renders the result in a table view.

This commit is contained in:
2018-01-09 16:28:34 +01:00
parent 262093d196
commit c8a2ea9907

View File

@@ -1,6 +1,9 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { fetchJSON } from '../http.jsx'; import { fetchJSON } from '../http.jsx';
// Components
import TorrentTable from './torrentTable.jsx'
// Stylesheets // Stylesheets
import btnStylesheet from '../styles/buttons.jsx'; import btnStylesheet from '../styles/buttons.jsx';
@@ -13,52 +16,56 @@ class PirateSearch extends Component {
constructor() { constructor() {
super(); super();
this.state = { this.state = {
response: [], torrentResponse: undefined,
name: '', name: '',
loading: '', loading: null,
showButton: true,
} }
} }
sendToDownload(torrent) { componentWillReceiveProps(props) {
let data = {magnet: torrent.magnet} if (props.name != this.state.name) {
fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/add', 'POST', data) this.setState({
.then((response) => { torrentResponse: undefined,
console.log(response) showButton: true,
}) })
} }
}
searchTheBay() { searchTheBay() {
const query = this.props.name; const query = this.props.name;
const type = this.props.type; const type = this.props.type;
this.setState({ this.setState({
loading: <Loading /> showButton: false,
loading: <Loading />,
}) })
fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/search?query='+query+'&type='+type, 'GET') fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/search?query='+query+'&type='+type, 'GET')
// fetchJSON('http://localhost:31459/api/v1/pirate/search?query='+query+'&type='+type, 'GET')
.then((response) => { .then((response) => {
console.log(response.torrents)
this.setState({ this.setState({
loading: '', torrentResponse: response.torrents,
response: response.torrents.map((torrent, index) => { loading: null,
return (
<div key={index}>
<span>{torrent.name}</span><br />
<span>{torrent.size}</span><br />
<span>{torrent.seed}</span><br />
<button onClick={() => {this.sendToDownload(torrent)}}>Send to download</button>
<br /><br />
</div>
)
}) })
}) })
.catch((error) => {
console.error(error);
this.setState({
showButton: true,
})
}) })
} }
render() { render() {
btnStylesheet.submit.top = '50%'
btnStylesheet.submit.position = 'absolute'
btnStylesheet.submit.marginLeft = '-75px'
return ( return (
<div> <div>
<div> { this.state.showButton ?
<div style={{textAlign:'center'}}>
<Interactive <Interactive
as='button' as='button'
onClick={() => {this.searchTheBay()}} onClick={() => {this.searchTheBay()}}
@@ -69,10 +76,11 @@ class PirateSearch extends Component {
<span style={{whiteSpace: 'nowrap'}}>Search for torrents</span> <span style={{whiteSpace: 'nowrap'}}>Search for torrents</span>
</Interactive> </Interactive>
</div> </div>
: null }
{ this.state.loading } { this.state.loading }
<span>{this.state.response}</span> <TorrentTable response={this.state.torrentResponse} />
</div> </div>
) )
} }