Skip to content

Commit

Permalink
Merge pull request #2575 from Hyperkid123/debounce-global-filter-requ…
Browse files Browse the repository at this point in the history
…ests

Debounce global filter callback actions.
  • Loading branch information
Hyperkid123 authored Jul 18, 2023
2 parents 423ae05 + 7e83a9f commit 8e36a99
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/components/GlobalFilter/GlobalFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { batch, shallowEqual, useDispatch, useSelector } from 'react-redux';
import { useTagsFilter } from '@redhat-cloud-services/frontend-components/FilterHooks';
import debounce from 'lodash/debounce';
import { fetchAllSIDs, fetchAllTags, fetchAllWorkloads, globalFilterChange } from '../../redux/actions';
import { generateFilter } from './globalFilterApi';
import { useLocation, useNavigate } from 'react-router-dom';
Expand All @@ -17,7 +18,7 @@ const useLoadTags = (hasAccess = false) => {
const isDisabled = useSelector(({ globalFilter: { globalFilterHidden }, chrome: { appId } }: ReduxState) => globalFilterHidden || !appId);
const dispatch = useDispatch();
return useCallback(
(activeTags, search) => {
debounce((activeTags, search) => {
storeFilter(activeTags, hasAccess && !isDisabled, navigate);
batch(() => {
dispatch(
Expand All @@ -42,7 +43,7 @@ const useLoadTags = (hasAccess = false) => {
})
);
});
},
}, 600),
[registeredWith, hasAccess]
);
};
Expand Down Expand Up @@ -93,6 +94,10 @@ const GlobalFilter = ({ hasAccess }: { hasAccess: boolean }) => {
); // TODO: Fix types in FEC

const loadTags = useLoadTags(hasAccess);
const selectTags = useCallback(
debounce((selectedTags: FlagTagsFilter) => dispatch(globalFilterChange(selectedTags)), 600),
[globalFilterChange]
);

useEffect(() => {
setValue(() => generateFilter());
Expand All @@ -101,7 +106,7 @@ const GlobalFilter = ({ hasAccess }: { hasAccess: boolean }) => {
useEffect(() => {
if (hasAccess && !isDisabled) {
loadTags(selectedTags, filterTagsBy);
dispatch(globalFilterChange(selectedTags));
selectTags(selectedTags);
}
}, [selectedTags, filterTagsBy, hasAccess, isDisabled]);

Expand Down

0 comments on commit 8e36a99

Please sign in to comment.