Skip to content

Commit

Permalink
UISACQCOMP-228 Provide default filters for the useLocationFilters h…
Browse files Browse the repository at this point in the history
…ook (#829)

* UISACQCOMP-228 Provide default filters for the useLocationFilters hook

* fix sonar lint
  • Loading branch information
usavkov-epam authored Nov 11, 2024
1 parent d543693 commit 676e061
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
16 changes: 11 additions & 5 deletions lib/AcqList/hooks/useFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,38 @@ import {
SEARCH_PARAMETER,
} from '../constants';

const useFilters = (resetData, initialFilters = {}, options) => {
const INITIAL_FILTERS = {};
const OPTIONS = {};

const useFilters = (resetData, initialFilters = INITIAL_FILTERS, options = OPTIONS) => {
const [filters, setFilters] = useState(initialFilters);
const [searchQuery, setSearchQuery] = useState('');
const [searchIndex, setSearchIndex] = useState('');

const {
skipTrimOnChange = false,
} = options || {};
} = options;

const applyFilters = useCallback(
(type, value) => {
const newFilters = { ...filters };
const newFilters = {};

if (Array.isArray(value) && value.length === 0) {
newFilters[type] = undefined;
} else {
newFilters[type] = value;
}

setFilters(newFilters);
setFilters((currentFilters) => ({
...currentFilters,
...newFilters,
}));

resetData();

return newFilters;
},
[filters, resetData],
[resetData],
);

const applySearch = useCallback(
Expand Down
29 changes: 27 additions & 2 deletions lib/AcqList/hooks/useLocationFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import {
import {
buildFiltersObj,
buildSearch,
getFiltersCount,
} from '../utils';
import useFilters from './useFilters';

const useLocationFilters = (location, history, resetData, options) => {
const DEFAULT_FILTERS = {};

const useLocationFilters = (
location,
history,
resetData,
options,
defaultFilters = DEFAULT_FILTERS,
) => {
const {
filters,
searchQuery,
Expand All @@ -32,7 +41,23 @@ const useLocationFilters = (location, history, resetData, options) => {
() => {
const initialFilters = buildFiltersObj(location.search);

setFilters(initialFilters);
setFilters((currentFilters) => {
const _defaultFilters = getFiltersCount(initialFilters) === 0 ? defaultFilters : {};

// If there are no filters in the URL, we need to add the default filters to the URL
if (Object.keys(_defaultFilters).length) {
history.push({
pathname: '',
search: `${buildSearch(_defaultFilters, location.search)}`,
});
}

return {
...currentFilters,
..._defaultFilters,
...initialFilters,
};
});
setSearchQuery(initialFilters[SEARCH_PARAMETER] || '');
setSearchIndex(initialFilters[SEARCH_INDEX_PARAMETER] || '');
},
Expand Down

0 comments on commit 676e061

Please sign in to comment.