Fix torrent search on every keystroke

Cleanup the search results when leaving the page.
This commit is contained in:
Grégoire Delattre 2020-04-09 17:13:48 +02:00
parent b66c32402a
commit c9c3541b4e
3 changed files with 30 additions and 30 deletions

View File

@ -22,6 +22,12 @@ export function searchTorrents(url) {
return request("TORRENTS_SEARCH", configureAxios().get(url)); return request("TORRENTS_SEARCH", configureAxios().get(url));
} }
export function clearTorrentSearch() {
return {
type: "TORRENTS_SEARCH_CLEAR",
};
}
export function setFetchedTorrents(torrents) { export function setFetchedTorrents(torrents) {
return { return {
type: "TORRENTS_FETCH_FULFILLED", type: "TORRENTS_FETCH_FULFILLED",

View File

@ -1,40 +1,39 @@
import React, { useState, useEffect, useCallback } from "react"; import React, { useState, useEffect } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useDispatch, useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { addTorrent, searchTorrents } from "../../actions/torrents";
import Loader from "../loader/loader"; import Loader from "../loader/loader";
import { OverlayTrigger, Tooltip } from "react-bootstrap"; import { OverlayTrigger, Tooltip } from "react-bootstrap";
import { prettySize } from "../../utils"; import { prettySize } from "../../utils";
import {
addTorrent,
searchTorrents,
clearTorrentSearch,
} from "../../actions/torrents";
export const TorrentSearch = ({ history, match }) => { export const TorrentSearch = ({ history, match }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
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 = useCallback(() => { const searchFunc = (type = "") => {
if (search === "" || type === "") { if (search === "" || type === "") {
return "";
}
return `/torrents/search/${type}/${encodeURI(search)}`;
}, [type, search]);
const searchFunc = useCallback(() => {
const searchURL = url();
if (searchURL === "") {
return; return;
} }
dispatch(searchTorrents(searchURL)); const url = `/torrents/search/${type}/${encodeURI(search)}`;
history.push(searchURL); setType(type);
}, [dispatch, history, url]); dispatch(searchTorrents(url));
history.push(url);
};
useEffect(() => { useEffect(() => {
searchFunc(); searchFunc(type);
}, [searchFunc]); return () => dispatch(clearTorrentSearch());
}, []); // eslint-disable-line react-hooks/exhaustive-deps
return ( return (
<div className="row"> <div className="row">
@ -52,8 +51,7 @@ export const TorrentSearch = ({ history, match }) => {
type="movies" type="movies"
typeFromURL={type} typeFromURL={type}
handleClick={() => { handleClick={() => {
setType("movies"); searchFunc("movies");
searchFunc();
}} }}
/> />
<SearchButton <SearchButton
@ -61,14 +59,13 @@ export const TorrentSearch = ({ history, match }) => {
type="shows" type="shows"
typeFromURL={type} typeFromURL={type}
handleClick={() => { handleClick={() => {
setType("shows"); searchFunc("shows");
searchFunc();
}} }}
/> />
</div> </div>
</div> </div>
<div className="col-12"> <div className="col-12">
<TorrentList search={search} /> <TorrentList />
</div> </div>
</div> </div>
); );
@ -98,7 +95,7 @@ SearchButton.propTypes = {
handleClick: PropTypes.func.isRequired, handleClick: PropTypes.func.isRequired,
}; };
const TorrentList = ({ search }) => { const TorrentList = () => {
const searching = useSelector((state) => state.torrents.searching); const searching = useSelector((state) => state.torrents.searching);
const results = useSelector((state) => state.torrents.searchResults); const results = useSelector((state) => state.torrents.searchResults);
@ -106,10 +103,6 @@ const TorrentList = ({ search }) => {
return <Loader />; return <Loader />;
} }
if (search === "") {
return null;
}
if (results.size === 0) { if (results.size === 0) {
return ( return (
<div className="jumbotron"> <div className="jumbotron">
@ -126,9 +119,6 @@ const TorrentList = ({ search }) => {
</React.Fragment> </React.Fragment>
); );
}; };
TorrentList.propTypes = {
search: PropTypes.string,
};
const Torrent = ({ torrent }) => { const Torrent = ({ torrent }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();

View File

@ -28,6 +28,10 @@ export default (state = defaultState, action) =>
draft.searchResults = action.payload.response.data; draft.searchResults = action.payload.response.data;
break; break;
case "TORRENTS_SEARCH_CLEAR":
draft.searchResults = [];
break;
default: default:
return draft; return draft;
} }