From 83d1894a25015d8f8eadd858230edff80d70e308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Thu, 9 Apr 2020 17:13:48 +0200 Subject: [PATCH] Fix torrent search on every keystroke Cleanup the search results when leaving the page. --- frontend/js/actions/torrents.js | 6 +++ frontend/js/components/torrents/search.js | 50 +++++++++-------------- frontend/js/reducers/torrents.js | 4 ++ 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/frontend/js/actions/torrents.js b/frontend/js/actions/torrents.js index e1608b3..c67932d 100644 --- a/frontend/js/actions/torrents.js +++ b/frontend/js/actions/torrents.js @@ -22,6 +22,12 @@ export function searchTorrents(url) { return request("TORRENTS_SEARCH", configureAxios().get(url)); } +export function clearTorrentSearch() { + return { + type: "TORRENTS_SEARCH_CLEAR", + }; +} + export function setFetchedTorrents(torrents) { return { type: "TORRENTS_FETCH_FULFILLED", diff --git a/frontend/js/components/torrents/search.js b/frontend/js/components/torrents/search.js index e2a85dd..30b9809 100644 --- a/frontend/js/components/torrents/search.js +++ b/frontend/js/components/torrents/search.js @@ -1,40 +1,39 @@ -import React, { useState, useEffect, useCallback } from "react"; +import React, { useState, useEffect } from "react"; import PropTypes from "prop-types"; import { useDispatch, useSelector } from "react-redux"; -import { addTorrent, searchTorrents } from "../../actions/torrents"; import Loader from "../loader/loader"; import { OverlayTrigger, Tooltip } from "react-bootstrap"; import { prettySize } from "../../utils"; +import { + addTorrent, + searchTorrents, + clearTorrentSearch, +} from "../../actions/torrents"; + export const TorrentSearch = ({ history, match }) => { const dispatch = useDispatch(); const [search, setSearch] = useState(match.params.search || ""); const [type, setType] = useState(match.params.type || ""); - const url = useCallback(() => { + const searchFunc = (type = "") => { if (search === "" || type === "") { - return ""; - } - - return `/torrents/search/${type}/${encodeURI(search)}`; - }, [type, search]); - - const searchFunc = useCallback(() => { - const searchURL = url(); - if (searchURL === "") { return; } - dispatch(searchTorrents(searchURL)); - history.push(searchURL); - }, [dispatch, history, url]); + const url = `/torrents/search/${type}/${encodeURI(search)}`; + setType(type); + dispatch(searchTorrents(url)); + history.push(url); + }; useEffect(() => { - searchFunc(); - }, [searchFunc]); + searchFunc(type); + return () => dispatch(clearTorrentSearch()); + }, []); // eslint-disable-line react-hooks/exhaustive-deps return (
@@ -52,8 +51,7 @@ export const TorrentSearch = ({ history, match }) => { type="movies" typeFromURL={type} handleClick={() => { - setType("movies"); - searchFunc(); + searchFunc("movies"); }} /> { type="shows" typeFromURL={type} handleClick={() => { - setType("shows"); - searchFunc(); + searchFunc("shows"); }} />
- +
); @@ -98,7 +95,7 @@ SearchButton.propTypes = { handleClick: PropTypes.func.isRequired, }; -const TorrentList = ({ search }) => { +const TorrentList = () => { const searching = useSelector((state) => state.torrents.searching); const results = useSelector((state) => state.torrents.searchResults); @@ -106,10 +103,6 @@ const TorrentList = ({ search }) => { return ; } - if (search === "") { - return null; - } - if (results.size === 0) { return (
@@ -126,9 +119,6 @@ const TorrentList = ({ search }) => { ); }; -TorrentList.propTypes = { - search: PropTypes.string, -}; const Torrent = ({ torrent }) => { const dispatch = useDispatch(); diff --git a/frontend/js/reducers/torrents.js b/frontend/js/reducers/torrents.js index a4c834f..3dee8bb 100644 --- a/frontend/js/reducers/torrents.js +++ b/frontend/js/reducers/torrents.js @@ -28,6 +28,10 @@ export default (state = defaultState, action) => draft.searchResults = action.payload.response.data; break; + case "TORRENTS_SEARCH_CLEAR": + draft.searchResults = []; + break; + default: return draft; }