Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UISACQCOMP-228 Provide default filters for the useLocationFilters hook #829

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading