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

fix(list): add list filters for de group and groupsets #390

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions src/components/sectionList/filters/DynamicFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
DomainTypeSelectionFilter,
ValueTypeSelectionFilter,
PublicAccessFilter,
DataElementFilter,
DataElementGroupFilter,
} from './filterSelectors'
import { useFilterKeys } from './useFilterKeys'

Expand All @@ -15,6 +17,8 @@ type FilterKeyToComponentMap = Partial<Record<ConfigurableFilterKey, React.FC>>
const filterKeyToComponentMap: FilterKeyToComponentMap = {
categoryCombo: CategoryComboFilter,
dataSet: DataSetFilter,
dataElement: DataElementFilter,
dataElementGroup: DataElementGroupFilter,
domainType: DomainTypeSelectionFilter,
valueType: ValueTypeSelectionFilter,
aggregationType: AggregationTypeFilter,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import i18n from '@dhis2/d2-i18n'
import React from 'react'
import { useSectionListFilter } from '../../../../lib'
import { createFilterDataQuery } from './createFilterDataQuery'
import { ModelFilterSelect } from './ModelFilter'

const query = createFilterDataQuery('dataElements')

export const DataElementFilter = () => {
const [filter, setFilter] = useSectionListFilter('dataElement')

const selected = filter?.[0]

return (
<ModelFilterSelect
placeholder={i18n.t('Data element')}
query={query}
selected={selected}
onChange={({ selected }) =>
setFilter(selected ? [selected] : undefined)
}
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import i18n from '@dhis2/d2-i18n'
import React from 'react'
import { useSectionListFilter } from '../../../../lib'
import { createFilterDataQuery } from './createFilterDataQuery'
import { ModelFilterSelect } from './ModelFilter'

const query = createFilterDataQuery('dataElementGroups')

export const DataElementGroupFilter = () => {
const [filter, setFilter] = useSectionListFilter('dataElementGroup')

const selected = filter?.[0]

return (
<ModelFilterSelect
placeholder={i18n.t('Data element group')}
query={query}
selected={selected}
onChange={({ selected }) =>
setFilter(selected ? [selected] : undefined)
}
/>
)
}
2 changes: 2 additions & 0 deletions src/components/sectionList/filters/filterSelectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './CategoryComboFilter'
export * from './IdentifiableFilter'
export * from './ConstantSelectionFilter'
export * from './PublicAccessFilter'
export * from './DataElementFilter'
export * from './DataElementGroupFilter'
4 changes: 4 additions & 0 deletions src/lib/sectionList/filters/filterConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const filterParamsSchema = z
[IDENTIFIABLE_FILTER_KEY]: z.string(),
aggregationType: z.array(z.nativeEnum(DataElement.aggregationType)),
categoryCombo: zodArrayIds,
dataElement: zodArrayIds,
dataElementGroup: zodArrayIds,
dataSet: zodArrayIds,
domainType: z.array(z.nativeEnum(DataElement.domainType)),
publicAccess: z.array(
Expand All @@ -29,6 +31,8 @@ export const filterQueryParamType = {
aggregationType: CustomDelimitedArrayParam,
domainType: CustomDelimitedArrayParam,
valueType: CustomDelimitedArrayParam,
dataElement: CustomDelimitedArrayParam,
dataElementGroup: CustomDelimitedArrayParam,
dataSet: CustomDelimitedArrayParam,
categoryCombo: CustomDelimitedArrayParam,
publicAccess: CustomDelimitedArrayParam,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/sectionList/filters/parseFiltersToQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const filterToQueryParamMap: FilterToQueryParamsMap = {
section.name === SchemaName.dataElement
? inFilter('dataSetElements.dataSet.id', value)
: defaultFilter('dataSet', value),
dataElement: (value) => inFilter('dataElements.id', value),
dataElementGroup: (value) => inFilter('dataElementGroups.id', value),
publicAccess: (value) => inFilter('sharing.public', value),
}

Expand Down
25 changes: 24 additions & 1 deletion src/lib/sectionList/listViews/sectionListViewsConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import i18n from '@dhis2/d2-i18n'
import { SectionName } from '../../constants'
import type { ConfigurableFilterKey } from '../filters'

export interface ModelPropertyDescriptor {
Expand Down Expand Up @@ -97,4 +98,26 @@ export const modelListViewsConfig = {
default: ['domainType', 'valueType', 'dataSet', 'categoryCombo'],
},
},
} satisfies SectionListViewConfig
dataElementGroup: {
columns: {},
filters: {
default: [
{
label: i18n.t('Data element'),
filterKey: 'dataElement',
},
],
},
},
dataElementGroupSet: {
columns: {},
filters: {
default: [
{
label: i18n.t('Data element group'),
filterKey: 'dataElementGroup',
},
],
},
},
} satisfies SectionListViewConfig<SectionName>
4 changes: 3 additions & 1 deletion src/pages/DefaultSectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const DefaultSectionList = () => {
},
},
}
const { error, data } = useQuery({

const { error, data, refetch } = useQuery({
queryKey: [query],
queryFn: ({ queryKey: [query], signal }) => {
return engine.query(query, { signal }) as Promise<ModelListResponse>
Expand All @@ -48,6 +49,7 @@ export const DefaultSectionList = () => {
error={error as FetchError | undefined}
data={modelList}
pager={data?.result.pager}
refetch={refetch}
/>
</div>
)
Expand Down
65 changes: 2 additions & 63 deletions src/pages/dataElements/List.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,3 @@
import { useDataQuery } from '@dhis2/app-runtime'
import { SharingDialog } from '@dhis2/ui'
import React, { useEffect } from 'react'
import { SectionListWrapper } from '../../components'
import { useModelListView } from '../../components/sectionList/listView'
import {
useSchemaFromHandle,
useParamsForDataQuery,
DEFAULT_FIELD_FILTERS,
DefaultFields,
} from '../../lib/'
import { getFieldFilter } from '../../lib/models/path'
import { Query, WrapQueryResponse } from '../../types'
import { DataElement, ModelCollectionResponse } from '../../types/models'
import { DefaultSectionList } from '../DefaultSectionList'

type FilteredDataElement = Pick<DataElement, DefaultFields> &
Partial<DataElement>

type DataElements = ModelCollectionResponse<FilteredDataElement, 'dataElements'>

type DataElementsResponse = WrapQueryResponse<DataElements>

const query: Query = {
result: {
resource: 'dataElements',
params: (params) => params,
},
}

export const Component = () => {
const { columns, query: listViewQuery } = useModelListView()
const initialParams = useParamsForDataQuery()
const schema = useSchemaFromHandle()
const { refetch, error, data } = useDataQuery<DataElementsResponse>(
query,
// refetched on mount by effect below
{ lazy: true }
)

useEffect(() => {
// wait to fetch until selected-columns are loaded
// so we dont fetch data multiple times
if (listViewQuery.isLoading) {
return
}
refetch({
...initialParams,
fields: columns
.map((column) => getFieldFilter(schema, column.path))
.concat(DEFAULT_FIELD_FILTERS),
})
}, [refetch, initialParams, columns, listViewQuery.isLoading, schema])

return (
<div>
<SectionListWrapper
error={error}
data={data?.result.dataElements}
pager={data?.result.pager}
refetch={refetch}
/>
</div>
)
}
export const Component = DefaultSectionList
Loading