import React, { useState } from "react";
import PropTypes from "prop-types";
import { Map, List } from "immutable";
import { connect } from "react-redux";
import { prettySize } from "../../utils";
import {
fetchTorrents,
addTorrent,
removeTorrent
} from "../../actions/torrents";
const mapStateToProps = state => ({
torrents: state.torrentStore.get("torrents")
});
const mapDispatchToProps = {
fetchTorrents,
addTorrent,
removeTorrent
};
const TorrentList = props => (
);
TorrentList.propTypes = {
fetchTorrents: PropTypes.func.isRequired,
addTorrent: PropTypes.func.isRequired,
removeTorrent: PropTypes.func.isRequired,
torrents: PropTypes.instanceOf(List)
};
export default connect(mapStateToProps, mapDispatchToProps)(TorrentList);
const AddTorrent = props => {
const [url, setUrl] = useState("");
const handleSubmit = e => {
e.preventDefault();
if (url === "") {
return;
}
props.addTorrent(url);
setUrl("");
};
return (
);
};
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)
};