Skip to content

Commit

Permalink
fix search with same query & request error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ccloli committed Dec 31, 2023
1 parent 1203cc4 commit cc5e430
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,31 @@ const Home = ({ history }) => {
const { signal } = abort;

fetch(`/api/search${history.location.search}`, { signal }).then(res => res.json()).then((res) => {
const { data, total } = res;
const { data, total, code, message } = res;
aborter.current = null;
setLoading(false);

if (code !== 200) {
alert('Request error: ' + (message || 'unknown'));
return;
}

setList(data);
setTotal(total);
setLoading(false);
if (totalStatus.current) {
totalStatus.current.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}
}).catch((err) => {
if (err.name === 'AbortError') {
return;
}

aborter.current = null;
setLoading(false);
alert('Request error: ' + ((err || {}).message || 'unknown'));
});
};

Expand Down Expand Up @@ -74,7 +88,7 @@ const Home = ({ history }) => {
onSearch(data);
};

useEffect(getList, [history.location.search]);
useEffect(getList, [history.location]);

return (
<div className={styles.container}>
Expand Down

0 comments on commit cc5e430

Please sign in to comment.