From c8a2ea9907ea21a4a901b105bb67e7d4140d3188 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 9 Jan 2018 16:28:34 +0100 Subject: [PATCH] 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. --- client/app/components/admin/PirateSearch.jsx | 130 ++++++++++--------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/client/app/components/admin/PirateSearch.jsx b/client/app/components/admin/PirateSearch.jsx index 25917fa..fcca252 100644 --- a/client/app/components/admin/PirateSearch.jsx +++ b/client/app/components/admin/PirateSearch.jsx @@ -1,6 +1,9 @@ import React, { Component } from 'react'; import { fetchJSON } from '../http.jsx'; +// Components +import TorrentTable from './torrentTable.jsx' + // Stylesheets import btnStylesheet from '../styles/buttons.jsx'; @@ -10,72 +13,77 @@ import Interactive from 'react-interactive'; import Loading from '../images/loading.jsx' class PirateSearch extends Component { - constructor() { - super(); - this.state = { - response: [], - name: '', - loading: '', - } - } + constructor() { + super(); + this.state = { + torrentResponse: undefined, + name: '', + loading: null, + showButton: true, + } + } - sendToDownload(torrent) { - let data = {magnet: torrent.magnet} - fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/add', 'POST', data) - .then((response) => { - console.log(response) - }) - } + componentWillReceiveProps(props) { + if (props.name != this.state.name) { + this.setState({ + torrentResponse: undefined, + showButton: true, + }) + } + } - searchTheBay() { - const query = this.props.name; - const type = this.props.type; + searchTheBay() { + const query = this.props.name; + const type = this.props.type; - this.setState({ - loading: - }) + this.setState({ + showButton: false, + loading: , + }) - fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/search?query='+query+'&type='+type, 'GET') - .then((response) => { - console.log(response.torrents) - this.setState({ - loading: '', - response: response.torrents.map((torrent, index) => { - return ( -
- {torrent.name}
- {torrent.size}
- {torrent.seed}
- -

-
- ) - }) - }) - }) - } + 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) => { + this.setState({ + torrentResponse: response.torrents, + loading: null, + }) + }) + .catch((error) => { + console.error(error); + this.setState({ + showButton: true, + }) + }) + } - render() { - return ( -
-
- {this.searchTheBay()}} - style={btnStylesheet.submit} - focus={btnStylesheet.submit_hover} - hover={btnStylesheet.submit_hover}> - - Search for torrents - -
+ render() { + btnStylesheet.submit.top = '50%' + btnStylesheet.submit.position = 'absolute' + btnStylesheet.submit.marginLeft = '-75px' - { this.state.loading } - - {this.state.response} -
- ) - } + return ( +
+ { this.state.showButton ? +
+ {this.searchTheBay()}} + style={btnStylesheet.submit} + focus={btnStylesheet.submit_hover} + hover={btnStylesheet.submit_hover}> + + Search for torrents + +
+ : null } + + { this.state.loading } + + +
+ ) + } } -export default PirateSearch \ No newline at end of file +export default PirateSearch