Use redux hooks on torrent components
This commit is contained in:
parent
27f0b742a4
commit
27f5c5d558
@ -1,39 +1,28 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { Map, List } from "immutable";
|
import { Map, List } from "immutable";
|
||||||
import { connect } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
|
||||||
import { prettySize } from "../../utils";
|
import { prettySize } from "../../utils";
|
||||||
import {
|
import { addTorrent, removeTorrent } from "../../actions/torrents";
|
||||||
fetchTorrents,
|
|
||||||
addTorrent,
|
|
||||||
removeTorrent,
|
|
||||||
} from "../../actions/torrents";
|
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
const TorrentList = () => {
|
||||||
torrents: state.torrentStore.get("torrents"),
|
const torrents = useSelector((state) => state.torrentStore.get("torrents"));
|
||||||
});
|
const dispatch = useDispatch();
|
||||||
const mapDispatchToProps = {
|
|
||||||
fetchTorrents,
|
|
||||||
addTorrent,
|
|
||||||
removeTorrent,
|
|
||||||
};
|
|
||||||
|
|
||||||
const TorrentList = (props) => (
|
return (
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<div className="col-12">
|
<div className="col-12">
|
||||||
<AddTorrent addTorrent={props.addTorrent} />
|
<AddTorrent addTorrent={(url) => dispatch(addTorrent(url))} />
|
||||||
<Torrents torrents={props.torrents} removeTorrent={props.removeTorrent} />
|
<Torrents
|
||||||
|
torrents={torrents}
|
||||||
|
removeTorrent={(id) => dispatch(removeTorrent(id))}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
TorrentList.propTypes = {
|
|
||||||
fetchTorrents: PropTypes.func.isRequired,
|
|
||||||
addTorrent: PropTypes.func.isRequired,
|
|
||||||
removeTorrent: PropTypes.func.isRequired,
|
|
||||||
torrents: PropTypes.instanceOf(List),
|
|
||||||
};
|
};
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(TorrentList);
|
export default TorrentList;
|
||||||
|
|
||||||
const AddTorrent = (props) => {
|
const AddTorrent = (props) => {
|
||||||
const [url, setUrl] = useState("");
|
const [url, setUrl] = useState("");
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React, { useState, useEffect, useCallback } from "react";
|
import React, { useState, useEffect, useCallback } from "react";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
import { connect } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { addTorrent, searchTorrents } from "../../actions/torrents";
|
import { addTorrent, searchTorrents } from "../../actions/torrents";
|
||||||
import { Map, List } from "immutable";
|
import { Map, List } from "immutable";
|
||||||
import Loader from "../loader/loader";
|
import Loader from "../loader/loader";
|
||||||
@ -9,40 +9,38 @@ import { OverlayTrigger, Tooltip } from "react-bootstrap";
|
|||||||
|
|
||||||
import { prettySize } from "../../utils";
|
import { prettySize } from "../../utils";
|
||||||
|
|
||||||
const mapStateToProps = (state) => ({
|
const TorrentSearch = ({ history, match }) => {
|
||||||
searching: state.torrentStore.get("searching"),
|
const dispatch = useDispatch();
|
||||||
results: state.torrentStore.get("searchResults"),
|
|
||||||
});
|
const searching = useSelector((state) => state.torrentStore.get("searching"));
|
||||||
const mapDispatchToProps = { addTorrent, searchTorrents };
|
const results = useSelector((state) =>
|
||||||
|
state.torrentStore.get("searchResults")
|
||||||
|
);
|
||||||
|
|
||||||
const TorrentSearch = ({
|
|
||||||
searching,
|
|
||||||
searchTorrents,
|
|
||||||
results,
|
|
||||||
addTorrent,
|
|
||||||
history,
|
|
||||||
match,
|
|
||||||
}) => {
|
|
||||||
const [search, setSearch] = useState(match.params.search || "");
|
const [search, setSearch] = useState(match.params.search || "");
|
||||||
const [type, setType] = useState(match.params.type || "");
|
const [type, setType] = useState(match.params.type || "");
|
||||||
const [url, setUrl] = useState("");
|
|
||||||
|
|
||||||
const getUrl = useCallback(() => {
|
const url = useCallback(() => {
|
||||||
|
if (search === "" || type === "") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
return `/torrents/search/${type}/${encodeURI(search)}`;
|
return `/torrents/search/${type}/${encodeURI(search)}`;
|
||||||
}, [type, search]);
|
}, [type, search]);
|
||||||
|
|
||||||
useEffect(() => {
|
const searchFunc = useCallback(() => {
|
||||||
if (search === "") {
|
const searchURL = url();
|
||||||
return;
|
if (searchURL === "") {
|
||||||
}
|
|
||||||
if (type === "") {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = getUrl();
|
dispatch(searchTorrents(searchURL));
|
||||||
searchTorrents(url);
|
history.push(searchURL);
|
||||||
history.push(url);
|
}, [dispatch, history, url]);
|
||||||
}, [url, getUrl, searchTorrents, history, search, type]);
|
|
||||||
|
useEffect(() => {
|
||||||
|
searchFunc();
|
||||||
|
}, [searchFunc]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="row">
|
<div className="row">
|
||||||
@ -61,7 +59,7 @@ const TorrentSearch = ({
|
|||||||
typeFromURL={type}
|
typeFromURL={type}
|
||||||
handleClick={() => {
|
handleClick={() => {
|
||||||
setType("movies");
|
setType("movies");
|
||||||
setUrl(getUrl());
|
searchFunc();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<SearchButton
|
<SearchButton
|
||||||
@ -70,7 +68,7 @@ const TorrentSearch = ({
|
|||||||
typeFromURL={type}
|
typeFromURL={type}
|
||||||
handleClick={() => {
|
handleClick={() => {
|
||||||
setType("shows");
|
setType("shows");
|
||||||
setUrl(getUrl());
|
searchFunc();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -79,7 +77,7 @@ const TorrentSearch = ({
|
|||||||
<TorrentList
|
<TorrentList
|
||||||
searching={searching}
|
searching={searching}
|
||||||
results={results}
|
results={results}
|
||||||
addTorrent={addTorrent}
|
addTorrent={(url) => dispatch(addTorrent(url))}
|
||||||
searchFromURL={search}
|
searchFromURL={search}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -87,24 +85,20 @@ const TorrentSearch = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
TorrentSearch.propTypes = {
|
TorrentSearch.propTypes = {
|
||||||
searching: PropTypes.bool.isRequired,
|
|
||||||
results: PropTypes.instanceOf(List),
|
|
||||||
searchFromURL: PropTypes.string,
|
searchFromURL: PropTypes.string,
|
||||||
match: PropTypes.object,
|
match: PropTypes.object,
|
||||||
history: PropTypes.object,
|
history: PropTypes.object,
|
||||||
addTorrent: PropTypes.func.isRequired,
|
|
||||||
searchTorrents: PropTypes.func.isRequired,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const SearchButton = (props) => {
|
const SearchButton = ({ type, typeFromURL, text, handleClick }) => {
|
||||||
const variant = props.type === props.typeFromURL ? "primary" : "secondary";
|
const variant = type === typeFromURL ? "primary" : "secondary";
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={`w-50 btn m-1 btn-lg btn-${variant}`}
|
className={`w-50 btn m-1 btn-lg btn-${variant}`}
|
||||||
onClick={props.handleClick}
|
onClick={handleClick}
|
||||||
>
|
>
|
||||||
<i className="fa fa-search" aria-hidden="true"></i> {props.text}
|
<i className="fa fa-search" aria-hidden="true"></i> {text}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -233,4 +227,4 @@ TorrentHealth.propTypes = {
|
|||||||
leechers: PropTypes.number,
|
leechers: PropTypes.number,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(TorrentSearch);
|
export default TorrentSearch;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user