add support for emptySearch (#109)

* support new searchTerm

* refacor

* refactor
This commit is contained in:
Aria Moradi
2021-11-29 19:04:29 +03:30
committed by GitHub
parent 1cbe99bfc3
commit a7e469c2ea

View File

@@ -35,28 +35,30 @@ export default function SearchSingle() {
.then((data: { name: string }) => setTitle(`Search: ${data.name}`)); .then((data: { name: string }) => setTitle(`Search: ${data.name}`));
}, []); }, []);
function resetSearch() {
setIsLoading(true);
setMangas([]);
setLastPageNum(1);
}
function processInput() { function processInput() {
if (textInput.current) { if (textInput.current) {
const { value } = textInput.current; const { value } = textInput.current;
if (value === '') { if (value === '') {
setError(true); setError(true);
} else if (value !== searchTerm) { } else {
setError(false); setError(false);
setSearchTerm(value); setSearchTerm(value);
setMangas([]); resetSearch();
} }
} }
} }
useEffect(() => { useEffect(() => {
if (searchTerm.length > 0) { if (searchTerm.length > 0) {
setIsLoading(true); client.get(`/api/v1/source/${sourceId}/search`, { params: { searchTerm, pageNum: lastPageNum } })
client.get(`/api/v1/source/${sourceId}/search/${searchTerm}/${lastPageNum}`)
.then((response) => response.data) .then((response) => response.data)
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => { .then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
setMessage('');
if (data.mangaList.length > 0) { if (data.mangaList.length > 0) {
setMangas([ setMangas([
...mangas, ...mangas,
@@ -71,7 +73,7 @@ export default function SearchSingle() {
setIsLoading(false); setIsLoading(false);
}); });
} }
}, [searchTerm]); }, [searchTerm, lastPageNum]);
return ( return (
<> <>