import React, { useState } from "react"; import PropTypes from "prop-types"; import { Map, List } from "immutable"; import { useDispatch, useSelector } from "react-redux"; import { prettySize } from "../../utils"; import { addTorrent, removeTorrent } from "../../actions/torrents"; const TorrentList = () => { const torrents = useSelector((state) => state.torrentStore.get("torrents")); const dispatch = useDispatch(); return (
dispatch(addTorrent(url))} /> dispatch(removeTorrent(id))} />
); }; export default TorrentList; const AddTorrent = (props) => { const [url, setUrl] = useState(""); const handleSubmit = (e) => { e.preventDefault(); if (url === "") { return; } props.addTorrent(url); setUrl(""); }; return (
handleSubmit(e)}> setUrl(e.target.value)} />
); }; AddTorrent.propTypes = { addTorrent: PropTypes.func.isRequired, }; const Torrents = (props) => { if (props.torrents.size === 0) { return (

No torrents

); } return (
{props.torrents.map((el, index) => ( ))}
); }; Torrents.propTypes = { removeTorrent: PropTypes.func.isRequired, torrents: PropTypes.instanceOf(List), }; const Torrent = (props) => { const handleClick = () => { props.removeTorrent(props.data.get("id")); }; const done = props.data.get("is_finished"); var progressStyle = done ? "success" : "info progress-bar-striped progress-bar-animated"; const progressBarClass = "progress-bar bg-" + progressStyle; var percentDone = props.data.get("percent_done"); const started = percentDone !== 0; if (started) { percentDone = Number(percentDone).toFixed(1) + "%"; } // Pretty sizes const downloadedSize = prettySize(props.data.get("downloaded_size")); const totalSize = prettySize(props.data.get("total_size")); const downloadRate = prettySize(props.data.get("download_rate")) + "/s"; return (
{props.data.get("name")} handleClick()} >
{started && (

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

)} {!started &&

Download not yet started

}
); }; Torrent.propTypes = { removeTorrent: PropTypes.func.isRequired, data: PropTypes.instanceOf(Map), };