Skip to content

Commit

Permalink
chore: refactor useNameSearch and remove debounce (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavbm authored Jun 21, 2024
1 parent d44800c commit 7aaa12a
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions frontend/src/hooks/staffing/useNameSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ export function useNameSearch() {

function setNameSearch(newSearch: string) {
setActiveNameSearch(newSearch);
setLastSearchKeyStrokeTime(Date.now());
}

const [activeNameSearch, setActiveNameSearch] =
useState<string>(searchFilter);

const [lastSearchKeyStrokeTime, setLastSearchKeyStrokeTime] =
useState<number>();

useEffect(() => {
const nameSearchDebounceTimer = setTimeout(() => {
if (
lastSearchKeyStrokeTime &&
Date.now() - lastSearchKeyStrokeTime > 180
) {
updateFilters({ search: activeNameSearch });
}
}, 180);

// this will clear Timeout
// when component unmount like in willComponentUnmount
// and show will not change to true
return () => {
clearTimeout(nameSearchDebounceTimer);
};
}, [activeNameSearch, lastSearchKeyStrokeTime, updateFilters]);
updateFilters({ search: activeNameSearch });
}, [activeNameSearch, updateFilters]);

return { activeNameSearch, setNameSearch };
}

0 comments on commit 7aaa12a

Please sign in to comment.