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 (
-
- );
- }
-}
-
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 (
+
+ );
+ }
+}
+
+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)}
+ />
+
+ );
+ }
+}