From e3849d5fd3d96d3c20a5e99139218eff1a898b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Sun, 21 May 2017 19:32:01 +0200 Subject: [PATCH] Remove the torrents store from the global scope --- src/public/js/app.js | 4 +- src/public/js/components/torrents/list.js | 131 ++++++++++++---------- 2 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/public/js/app.js b/src/public/js/app.js index bec00f3..cd70a6b 100644 --- a/src/public/js/app.js +++ b/src/public/js/app.js @@ -45,7 +45,7 @@ function mapStateToProps(state) { movieStore: state.movieStore, showStore: state.showStore, userStore: state.userStore, - torrentStore: state.torrentStore, + torrentCount: state.torrentStore.torrents.length, alerts: state.alerts, } } @@ -60,7 +60,7 @@ function Main(props) { - - - - ); +function mapStateToProps(state) { + return { torrents: state.torrentStore.torrents }; } +const mapDispatchToProps = (dipatch) => + bindActionCreators({ addTorrent }, dipatch) -class AddTorrent extends React.Component { +class TorrentList extends React.PureComponent { + render() { + return ( +
+ + +
+ ); + } +} +export default connect(mapStateToProps, mapDispatchToProps)(TorrentList); + +class AddTorrent extends React.PureComponent { constructor(props) { super(props); this.state = { url: '' }; @@ -53,72 +65,75 @@ class AddTorrent extends React.Component { } } -function List(props){ - if (props.torrents.length === 0) { +class List extends React.PureComponent { + render() { + if (this.props.torrents.length === 0) { + return ( +
+
+

Torrents

+
+
No torrents
+
+
+
+ ); + } return (

Torrents

-
-
No torrents
-
-
-
- ); - } - return ( -
-
-

Torrents

- {props.torrents.map(function(el, index) { + {this.props.torrents.map(function(el, index) { return ( ); })}
- ); + ); + } } +class Torrent extends React.PureComponent { + render() { + const done = this.props.data.is_finished; + var progressStyle = 'progress-bar progress-bar-warning'; + if (done) { + progressStyle = 'progress-bar progress-bar-success'; + } + var percentDone = this.props.data.percent_done; + const started = (percentDone !== 0); + if (started) { + percentDone = Number(percentDone).toFixed(1) + '%'; + } -function Torrent(props){ - const done = props.data.is_finished; - var progressStyle = 'progress-bar progress-bar-warning'; - if (done) { - progressStyle = 'progress-bar progress-bar-success'; - } - var percentDone = props.data.percent_done; - const started = (percentDone !== 0); - if (started) { - percentDone = Number(percentDone).toFixed(1) + '%'; - } - - var downloadedSize = prettySize(props.data.downloaded_size); - var totalSize = prettySize(props.data.total_size); - var downloadRate = prettySize(props.data.download_rate) + "/s"; - return ( -
-
{props.data.name}
-
- {started && -
-
+ var downloadedSize = prettySize(this.props.data.downloaded_size); + var totalSize = prettySize(this.props.data.total_size); + var downloadRate = prettySize(this.props.data.download_rate) + "/s"; + return ( +
+
{this.props.data.name}
+
+ {started && +
+
+
-
- } - {!started && -

Download not yet started

- } - {started && -
-

{downloadedSize} / {totalSize} - {percentDone} - {downloadRate}

+ } + {!started && +

Download not yet started

+ } + {started && +
+

{downloadedSize} / {totalSize} - {percentDone} - {downloadRate}

+
+ }
- }
-
- ); + ); + } } function prettySize(fileSizeInBytes) {