Skip to content

Commit

Permalink
add support for emptySearch (#109)
Browse files Browse the repository at this point in the history
* support new searchTerm

* refacor

* refactor
  • Loading branch information
AriaMoradi authored Nov 29, 2021
1 parent 1cbe99b commit a7e469c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/screens/SearchSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,30 @@ export default function SearchSingle() {
.then((data: { name: string }) => setTitle(`Search: ${data.name}`));
}, []);

function resetSearch() {
setIsLoading(true);
setMangas([]);
setLastPageNum(1);
}

function processInput() {
if (textInput.current) {
const { value } = textInput.current;
if (value === '') {
setError(true);
} else if (value !== searchTerm) {
} else {
setError(false);
setSearchTerm(value);
setMangas([]);
resetSearch();
}
}
}

useEffect(() => {
if (searchTerm.length > 0) {
setIsLoading(true);

client.get(`/api/v1/source/${sourceId}/search/${searchTerm}/${lastPageNum}`)
client.get(`/api/v1/source/${sourceId}/search`, { params: { searchTerm, pageNum: lastPageNum } })
.then((response) => response.data)
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
setMessage('');

if (data.mangaList.length > 0) {
setMangas([
...mangas,
Expand All @@ -71,7 +73,7 @@ export default function SearchSingle() {
setIsLoading(false);
});
}
}, [searchTerm]);
}, [searchTerm, lastPageNum]);

return (
<>
Expand Down

0 comments on commit a7e469c

Please sign in to comment.