diff --git a/src/lib/sectionList/filters/parseFiltersToQueryParams.ts b/src/lib/sectionList/filters/parseFiltersToQueryParams.ts index a5b26530..5ee52a6f 100644 --- a/src/lib/sectionList/filters/parseFiltersToQueryParams.ts +++ b/src/lib/sectionList/filters/parseFiltersToQueryParams.ts @@ -10,11 +10,11 @@ type FilterToQueryParamsMap = { [key in keyof ParsedFilterParams]: ( value: NonNullable, section: Section - ) => string + ) => string[] | string } const inFilter = (filterPath: string, value: string[]) => - `${filterPath}:in:[${value.join(',')}]` + value.length > 0 ? `${filterPath}:in:[${value.join(',')}]` : '' const defaultFilter = ( key: FilterKey, @@ -41,11 +41,15 @@ const filterToQueryParamMap: FilterToQueryParamsMap = { categoryOptionGroup: (value) => inFilter('categoryOptionGroups.id', value), dataSet: (value, section) => { if (section.name === SchemaName.dataElement) { - const emptyIsSelected = value.filter((id) => id === 'NO_DATASET') - if (emptyIsSelected.length > 0) { - return `dataSetElements:empty` + const withoutEmpty = value.filter((id) => id !== 'NO_DATASET') + const dataSetInFilter = inFilter( + 'dataSetElements.dataSet.id', + withoutEmpty + ) + if (value.length !== withoutEmpty.length) { + return [dataSetInFilter, 'dataSetElements:empty'] } - return inFilter('dataSetElements.dataSet.id', value) + return dataSetInFilter } return defaultFilter('dataSet', value) }, @@ -56,7 +60,7 @@ const getQueryParamForFilter = ( key: FilterKey, value: AllValues, section: ModelSection -): string | undefined => { +): string | string[] | undefined => { if (!value) { return undefined } @@ -75,11 +79,12 @@ export const parseFiltersToQueryParams = ( section: ModelSection ): string[] => { const queryFilters = Object.entries(filters) - .map(([key, value]) => + .flatMap(([key, value]) => getQueryParamForFilter(key as FilterKey, value, section) ) .filter( - (queryFilter): queryFilter is string => queryFilter !== undefined + (queryFilter): queryFilter is string => + queryFilter != undefined && queryFilter?.length > 0 ) return queryFilters }