From 0aa3b6fc590696d99e8f2553fc10f46fd8e0aed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Sat, 11 Apr 2020 18:17:33 +0200 Subject: [PATCH] Move torrent list components in separate files --- frontend/js/components/torrents/list.js | 130 +----------------- .../js/components/torrents/list/addTorrent.js | 35 +++++ .../js/components/torrents/list/torrent.js | 77 +++++++++++ .../js/components/torrents/list/torrents.js | 24 ++++ 4 files changed, 139 insertions(+), 127 deletions(-) create mode 100644 frontend/js/components/torrents/list/addTorrent.js create mode 100644 frontend/js/components/torrents/list/torrent.js create mode 100644 frontend/js/components/torrents/list/torrents.js diff --git a/frontend/js/components/torrents/list.js b/frontend/js/components/torrents/list.js index 8866198..e3594d7 100644 --- a/frontend/js/components/torrents/list.js +++ b/frontend/js/components/torrents/list.js @@ -1,9 +1,7 @@ -import React, { useState } from "react"; -import PropTypes from "prop-types"; -import { useDispatch, useSelector } from "react-redux"; +import React from "react"; -import { prettySize, prettyEpisodeName } from "../../utils"; -import { addTorrent, removeTorrent } from "../../actions/torrents"; +import { AddTorrent } from "./list/addTorrent"; +import { Torrents } from "./list/torrents"; export const TorrentList = () => { return ( @@ -15,125 +13,3 @@ export const TorrentList = () => { ); }; - -const AddTorrent = () => { - const dispatch = useDispatch(); - const [url, setUrl] = useState(""); - - const handleSubmit = (e) => { - e.preventDefault(); - if (url === "") { - return; - } - dispatch( - addTorrent({ - result: { url: url }, - }) - ); - setUrl(""); - }; - - return ( -
handleSubmit(e)}> - setUrl(e.target.value)} - /> -
- ); -}; - -const Torrents = () => { - const torrents = useSelector((state) => state.torrents.torrents); - - if (torrents.length === 0) { - return ( -
-

No torrents

-
- ); - } - - return ( -
- {torrents.map((torrent, index) => ( - - ))} -
- ); -}; - -const Torrent = ({ torrent }) => { - const dispatch = useDispatch(); - - var progressStyle = torrent.status.is_finished - ? "success" - : "info progress-bar-striped progress-bar-animated"; - const progressBarClass = "progress-bar bg-" + progressStyle; - - var percentDone = torrent.status.percent_done; - const started = percentDone !== 0; - if (started) { - percentDone = Number(percentDone).toFixed(1) + "%"; - } - - const torrentTitle = (torrent) => { - switch (torrent.type) { - case "movie": - return torrent.video ? torrent.video.title : torrent.status.name; - case "episode": - return torrent.video - ? prettyEpisodeName( - torrent.video.show_title, - torrent.video.season, - torrent.video.episode - ) - : torrent.status.name; - default: - return torrent.status.name; - } - }; - - // Pretty sizes - const downloadedSize = prettySize(torrent.status.downloaded_size); - const totalSize = prettySize(torrent.status.total_size); - const downloadRate = prettySize(torrent.status.download_rate) + "/s"; - return ( -
-
- {torrentTitle(torrent)} - dispatch(removeTorrent(torrent.status.id))} - > -
-
- {started && ( - -
-
-
-

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

-
- )} - {!started &&

Download not yet started

} -
-
- ); -}; -Torrent.propTypes = { - torrent: PropTypes.object.isRequired, -}; diff --git a/frontend/js/components/torrents/list/addTorrent.js b/frontend/js/components/torrents/list/addTorrent.js new file mode 100644 index 0000000..9b0879f --- /dev/null +++ b/frontend/js/components/torrents/list/addTorrent.js @@ -0,0 +1,35 @@ +import React, { useState } from "react"; +import { useDispatch } from "react-redux"; + +import { addTorrent } from "../../../actions/torrents"; + +export const AddTorrent = () => { + const dispatch = useDispatch(); + const [url, setUrl] = useState(""); + + const handleSubmit = (e) => { + e.preventDefault(); + if (url === "") { + return; + } + dispatch( + addTorrent({ + result: { url: url }, + }) + ); + setUrl(""); + }; + + return ( +
handleSubmit(e)}> + setUrl(e.target.value)} + /> +
+ ); +}; diff --git a/frontend/js/components/torrents/list/torrent.js b/frontend/js/components/torrents/list/torrent.js new file mode 100644 index 0000000..dbe19ab --- /dev/null +++ b/frontend/js/components/torrents/list/torrent.js @@ -0,0 +1,77 @@ +import React from "react"; +import PropTypes from "prop-types"; +import { useDispatch } from "react-redux"; + +import { prettySize, prettyEpisodeName } from "../../../utils"; +import { removeTorrent } from "../../../actions/torrents"; + +export const Torrent = ({ torrent }) => { + const dispatch = useDispatch(); + + var progressStyle = torrent.status.is_finished + ? "success" + : "info progress-bar-striped progress-bar-animated"; + const progressBarClass = "progress-bar bg-" + progressStyle; + + var percentDone = torrent.status.percent_done; + const started = percentDone !== 0; + if (started) { + percentDone = Number(percentDone).toFixed(1) + "%"; + } + + const torrentTitle = (torrent) => { + switch (torrent.type) { + case "movie": + return torrent.video ? torrent.video.title : torrent.status.name; + case "episode": + return torrent.video + ? prettyEpisodeName( + torrent.video.show_title, + torrent.video.season, + torrent.video.episode + ) + : torrent.status.name; + default: + return torrent.status.name; + } + }; + + // Pretty sizes + const downloadedSize = prettySize(torrent.status.downloaded_size); + const totalSize = prettySize(torrent.status.total_size); + const downloadRate = prettySize(torrent.status.download_rate) + "/s"; + return ( +
+
+ {torrentTitle(torrent)} + dispatch(removeTorrent(torrent.status.id))} + > +
+
+ {started && ( + +
+
+
+

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

+
+ )} + {!started &&

Download not yet started

} +
+
+ ); +}; +Torrent.propTypes = { + torrent: PropTypes.object.isRequired, +}; diff --git a/frontend/js/components/torrents/list/torrents.js b/frontend/js/components/torrents/list/torrents.js new file mode 100644 index 0000000..5a08b38 --- /dev/null +++ b/frontend/js/components/torrents/list/torrents.js @@ -0,0 +1,24 @@ +import React from "react"; +import { useSelector } from "react-redux"; + +import { Torrent } from "./torrent"; + +export const Torrents = () => { + const torrents = useSelector((state) => state.torrents.torrents); + + if (torrents.length === 0) { + return ( +
+

No torrents

+
+ ); + } + + return ( +
+ {torrents.map((torrent, index) => ( + + ))} +
+ ); +};