From 176312617c968bc13182c2aa6bfab1b2e5f65dd2 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 10 Sep 2017 20:17:32 +0900 Subject: [PATCH] Make torrent options reusable --- src/bitrate.js | 4 + src/ui/add_torrent.js | 191 ++++++-------------------------------- src/ui/torrent_options.js | 157 +++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 165 deletions(-) create mode 100644 src/ui/torrent_options.js diff --git a/src/bitrate.js b/src/bitrate.js index b30d94d..91b89f6 100644 --- a/src/bitrate.js +++ b/src/bitrate.js @@ -10,6 +10,10 @@ export function convertToBitrate(value, unit) { return value * Rates[unit]; } +export function convertFromBitrate(value, unit) { + return value / Rates[unit]; +} + export function formatBitrate(bitrate) { if (bitrate > Rates["GiB/s"]) { return `${(bitrate / Rates["GiB/s"]).toFixed(2)} GiB/s`; diff --git a/src/ui/add_torrent.js b/src/ui/add_torrent.js index 656cd32..958d41b 100644 --- a/src/ui/add_torrent.js +++ b/src/ui/add_torrent.js @@ -16,129 +16,10 @@ import fetch from 'isomorphic-fetch'; import bencode from 'bencode'; import moment from 'moment'; import ToggleContainer from './toggle_container'; +import TorrentOptions from './torrent_options'; import ws_send from '../socket'; -import { convertToBitrate } from '../bitrate'; import date from '../date'; -class Throttle extends Component { - constructor() { - super(); - this.state = { - strategy: "global", - unit: "MiB/s", - limit: 1 - }; - this.setStrategy = this.setStrategy.bind(this); - } - - invokeChange() { - const { onChange } = this.props; - if (!onChange) { - return; - } - - const { strategy, limit, unit } = this.state; - switch (strategy) { - case "global": - onChange(null); - break; - case "unlimited": - onChange(-1); - break; - default: - onChange(convertToBitrate(limit, unit)); - break; - } - } - - setStrategy(strategy) { - this.setState({ strategy }); - this.invokeChange(); - } - - setLimit(limit) { - if (limit <= 0) { - this.setState({ limit: this.state.limit }); - return; - } - this.setState({ limit }); - this.invokeChange(); - } - - setUnit(unit) { - this.setState({ unit }); - this.invokeChange(); - } - - render() { - const { legend, prop } = this.props; - return ( -
- - {legend} - - - - - - - - - - - {this.state.strategy === "custom" && -
- - this.setLimit(parseFloat(e.target.value))} - /> - - - this.setUnit(e.target.value)} - > - - - - - - -
- } -
- ); - } -} - class AddTorrent extends Component { constructor() { super(); @@ -150,9 +31,9 @@ class AddTorrent extends Component { useMagnet: false, torrent: null, files: [], - startImmediately: true, - uploadThrottle: -1, - downloadThrottle: -1, + start: true, + uploadThrottle: null, + downloadThrottle: null, priority: 3, }; } @@ -210,7 +91,7 @@ class AddTorrent extends Component { throttle_down: downloadThrottle } }, async done => { - if (this.state.startImmediately) { + if (this.state.start) { ws_send("RESUME_TORRENT", { id }); } dispatch(push(`/torrents/${id}`)); @@ -219,7 +100,7 @@ class AddTorrent extends Component { uploadFile() { this.setState({ loading: true }); - const { magnet, file, startImmediately } = this.state; + const { magnet, file, start } = this.state; const { dispatch } = this.props; const customize = // TODO: File options this.state.priority !== 3 || @@ -238,12 +119,12 @@ class AddTorrent extends Component { if (magnet) { ws_send("UPLOAD_MAGNET", { uri: magnet, - start: startImmediately && !customize + start: start && !customize }, handleOffer); } else { ws_send("UPLOAD_TORRENT", { size: file.size, - start: startImmediately && !customize + start: start && !customize }, handleOffer); } } @@ -285,47 +166,27 @@ class AddTorrent extends Component { } renderOptions() { + const { + start, + priority, + downloadThrottle, + uploadThrottle + } = this.state; + return ( - - - - - - - this.setState({ priority: parseInt(e.target.value)}) - } - > - - - - - - - - this.setState({ downloadThrottle: limit })} - /> - this.setState({ uploadThrottle: limit })} + this.setState({ start })} + priority={priority} + priorityChanged={priority => this.setState({ priority })} + downloadThrottle={downloadThrottle} + downloadThrottleChanged={downloadThrottle => + this.setState({ downloadThrottle })} + uploadThrottle={uploadThrottle} + uploadThrottleChanged={uploadThrottle => + { console.log(uploadThrottle); this.setState({ uploadThrottle }); }} /> diff --git a/src/ui/torrent_options.js b/src/ui/torrent_options.js new file mode 100644 index 0000000..99587e3 --- /dev/null +++ b/src/ui/torrent_options.js @@ -0,0 +1,157 @@ +import React, { Component } from 'react'; +import { + FormGroup, + Label, + Input +} from 'reactstrap'; +import { convertFromBitrate, convertToBitrate } from '../bitrate'; + +class Throttle extends Component { + constructor() { + super(); + this.setLimit = this.setLimit.bind(this); + this.setUnit = this.setUnit.bind(this); + this.state = { unit: "MiB/s" }; + } + + setLimit(limit) { + const { onChange } = this.props; + const { unit } = this.state; + const converted = limit <= 0 || limit === null ? + limit : convertToBitrate(limit, unit); + onChange && onChange(converted); + } + + setUnit(unit) { + const limit = convertFromBitrate(this.props.limit, this.state.unit); + this.setState({ unit }); + this.setLimit(limit); + } + + render() { + const { limit, legend, prop } = this.props; + const { unit } = this.state; + return ( +
+ + {legend} + + + + + + + + + + + {limit !== -1 && limit !== null && +
+ + this.setLimit(parseFloat(e.target.value))} + /> + + + this.setUnit(e.target.value)} + > + + + + + + +
+ } +
+ ); + } +} + +export default class TorrentOptions extends Component { + render() { + const { + start, + startChanged, + priority, + priorityChanged, + downloadThrottle, + downloadThrottleChanged, + uploadThrottle, + uploadThrottleChanged, + } = this.props; + return ( +
+ {typeof start !== undefined && + + + + } + + + priorityChanged(parseInt(e.target.value))} + > + + + + + + + + downloadThrottleChanged(limit)} + /> + uploadThrottleChanged(limit)} + /> +
+ ); + } +}