Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 945065f

Browse files
committed
refactor: debounce filter
1 parent 31582a1 commit 945065f

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/constants/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export const TEAMS_PER_PAGE = 20;
2222
*/
2323
export const POSITION_CANDIDATES_PER_PAGE = 5;
2424

25+
/**
26+
* Input debounce delay (ms)
27+
*/
28+
export const INPUT_DEBOUNCE_DELAY = 200;
29+
2530
/**
2631
* Position statuses
2732
*/

src/routes/MyTeamsList/index.jsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,40 @@ import LoadingIndicator from "../../components/LoadingIndicator";
1616
import { useDebounce } from "react-use";
1717
import { TEAMS_PER_PAGE } from "constants";
1818
import "./styles.module.scss";
19+
import { INPUT_DEBOUNCE_DELAY } from "constants/";
1920

2021
const MyTeamsList = () => {
2122
let [myTeams, setMyTeams] = useState(null);
22-
const [filter, setFilter] = useState("");
23-
const [tempFilter, setTempFilter] = React.useState('');
23+
const [debouncedFilter, setDebouncedFilter] = useState("");
24+
const [filter, setFilter] = React.useState("");
2425
const [loadingError, setLoadingError] = useState(false);
2526
const [page, setPage] = useState(1);
2627
const [total, setTotal] = useState(0);
28+
2729
const onFilterChange = (evt) => {
28-
setTempFilter(evt.target.value)
29-
}
30+
setFilter(evt.target.value);
31+
};
3032

31-
useDebounce((value) => {
32-
console.log('xxxx', value)
33-
setFilter(tempFilter);
34-
setPage(1);
35-
}, 200, [tempFilter]);
33+
useDebounce(
34+
() => {
35+
setDebouncedFilter(filter);
36+
setPage(1);
37+
},
38+
INPUT_DEBOUNCE_DELAY,
39+
[filter]
40+
);
3641

3742
useEffect(() => {
3843
setMyTeams(null);
39-
getMyTeams(filter, page, TEAMS_PER_PAGE)
44+
getMyTeams(debouncedFilter, page, TEAMS_PER_PAGE)
4045
.then((response) => {
4146
setMyTeams(response.data);
4247
setTotal(response.headers["x-total"]);
4348
})
4449
.catch((responseError) => {
4550
setLoadingError(responseError);
4651
});
47-
}, [filter, page]);
52+
}, [debouncedFilter, page]);
4853

4954
const onPageClick = useCallback(
5055
(newPage) => {
@@ -65,7 +70,9 @@ const MyTeamsList = () => {
6570
/>
6671
}
6772
/>
68-
{myTeams && myTeams.length === 0 && (<div styleName="empty">No teams found</div>)}
73+
{myTeams && myTeams.length === 0 && (
74+
<div styleName="empty">No teams found</div>
75+
)}
6976
{!myTeams ? (
7077
<LoadingIndicator error={loadingError && loadingError.toString()} />
7178
) : (

0 commit comments

Comments
 (0)