Skip to content

Commit

Permalink
fix: fix no_dataset in combination with other filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 3, 2024
1 parent fa941b4 commit e83bc88
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/lib/sectionList/filters/parseFiltersToQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ type FilterToQueryParamsMap = {
[key in keyof ParsedFilterParams]: (
value: NonNullable<ParsedFilterParams[key]>,
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,
Expand All @@ -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)
},
Expand All @@ -56,7 +60,7 @@ const getQueryParamForFilter = (
key: FilterKey,
value: AllValues,
section: ModelSection
): string | undefined => {
): string | string[] | undefined => {
if (!value) {
return undefined
}
Expand All @@ -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
}

0 comments on commit e83bc88

Please sign in to comment.