Skip to content

Commit

Permalink
fix: update and simplify filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Jul 9, 2023
1 parent ab00bfd commit c4c507f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/components/sectionList/SectionListWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const SectionListWrapper = <Model extends IdentifiableObject>({

const SectionListMessage = () => {
if (error) {
console.log(error.details)
console.log(error.details || error)
return <SectionListError />
}
if (!data?.result) {
Expand Down
28 changes: 10 additions & 18 deletions src/components/sectionList/filters/useSectionListFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,31 @@ export const useSectionListFilter = (
return [filters?.[filterKey] ?? undefined, boundSetFilter]
}

export type ParseToQueryFilterResult = {
filter: string[]
rootJunction: GistParams['rootJunction']
}

const parseToGistQueryFilter = (filters: Filters): ParseToQueryFilterResult => {
const parseToGistQueryFilter = (filters: Filters): string[] => {
const { [IDENTIFIABLE_KEY]: identifiableValue, ...restFilters } = filters
const queryFilters: string[] = []

const hasOtherFilters = Object.keys(restFilters).length > 0
// Groups are a powerful way to combine filters,
// here we use them for identifiable filters, to group them with "OR" and
// rest of the filters with "AND".
// Unfortunately, it doesn't work to use groups without at least two,
// so we need to add them conditionally.
// see https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-239/metadata-gist.html#gist_parameters_filter
const identifiableFilterGroup = hasOtherFilters ? `0:` : ''
if (identifiableValue) {
const identifiableFilterGroup = `0:`
Object.entries(IDENTIFIABLE_FIELDS).forEach(([key, { operator }]) => {
queryFilters.push(
`${identifiableFilterGroup}${key}:${operator}:${identifiableValue}`
)
})
}
const restFilterGroup = identifiableValue ? `1:` : ''
let restFilterGroup: number | undefined
if (identifiableValue) {
restFilterGroup = 1
}
Object.entries(restFilters).forEach(([key, value]) => {
queryFilters.push(`${restFilterGroup}${key}:eq:${value}`)
const group = restFilterGroup ? `${restFilterGroup++}:` : ''
queryFilters.push(`${group}${key}:eq:${value}`)
})
// when there are no other filters than identifiable, we can't group them
// and thus we need to set rootJunction to OR
const rootJunction =
identifiableValue && !hasOtherFilters ? 'OR' : undefined
return { filter: queryFilters, rootJunction }
return queryFilters
}

export const useSectionListQueryFilter = () => {
Expand All @@ -155,7 +147,7 @@ export const useQueryParamsForModelGist = (): GistParams => {
return useMemo(() => {
return {
...paginationParams,
...filterParams,
filter: filterParams,
}
}, [paginationParams, filterParams])
}
12 changes: 4 additions & 8 deletions src/components/sectionList/useSectionListParamsRefetch.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { useEffect } from 'react'
import { QueryRefetchFunction } from '../../types'
import { useSectionListQueryFilter } from './filters'
import { usePaginationQueryParams } from './SectionListPagination'
import { useQueryParamsForModelGist } from './filters'

/** Refetches when filter and pagination params change */
export const useSectionListParamsRefetch = (refetch: QueryRefetchFunction) => {
const { filter, rootJunction } = useSectionListQueryFilter()
const [paginationParams] = usePaginationQueryParams()
const params = useQueryParamsForModelGist()

useEffect(() => {
refetch({
...paginationParams,
filter,
rootJunction,
...params,
})
}, [refetch, paginationParams, filter, rootJunction])
}, [refetch, params])
}

0 comments on commit c4c507f

Please sign in to comment.