Skip to content

Commit

Permalink
debouncer fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danibanezRepos committed Jan 31, 2024
1 parent 9e49b96 commit f30f4a9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/components/SearchNodes/searchNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ function SearchNodes({ varData, colorList, addNode }) {
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
if (searchField.length >= 3 || !data || searchField === '')
debounceFilteredNodeData()(searchField, setData, setIsLoading);
if (searchField.length >= 3 || !data || searchField === '') {
debounceFilteredNodeData()(searchField, data, setData, setIsLoading);
}
}, [searchField, data]);

const handleChange = (e) => setSearchField(e.target.value);
Expand All @@ -36,6 +37,7 @@ function SearchNodes({ varData, colorList, addNode }) {
<input
className={SearchNodesStyles.input}
type="search"
disabled={true} // TODO implement indexes to speed up nodes
placeholder={placeholderText}
onChange={handleChange}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/utils/petitionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ let debounceTimeout;
let lastFilter = null;

export const debounceFilteredNodeData = () => {
return (filter, setData, setIsLoading) => {
if (filter !== lastFilter) {
return (filter, data, setData, setIsLoading) => {
if (filter !== lastFilter || !data) {
clearTimeout(debounceTimeout);
setIsLoading(true);
debounceTimeout = setTimeout(() =>
populateWithFilteredNodeData(filter, setData, setIsLoading), config.debounceDelay);
lastFilter = filter;
}
};
}
}

export const populateWithFilteredNodeData = (filter, setData, setIsLoading) => {
Expand Down

0 comments on commit f30f4a9

Please sign in to comment.