Skip to content

Commit

Permalink
refactor: dataSet filter use modelmultiselect
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 18, 2024
1 parent 7bb27bb commit dbfca87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/components/metadataFormControls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './DataElementsTransfer'
export * from './LegendSetTransfer'
export * from './OptionSetSelect'
export * from './ModelTransfer'
export * from './ModelMultiSelect'
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
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('dataSets')
import { PlainResourceQuery } from '../../../../types'
import { ModelMultiSelect } from '../../../metadataFormControls'
const query = {
resource: 'dataSets',
params: {
fields: ['id', 'displayName'],
order: 'displayName:asc',
pageSize: 5,
},
} as const satisfies PlainResourceQuery

export const DataSetFilter = () => {
const [filter, setFilter] = useSectionListFilter('dataSet')

const selected = filter?.[0]

return (
<ModelFilterSelect
placeholder={i18n.t('Data set')}
<ModelMultiSelect
query={query}
selected={selected}
selected={filter || []}
prefix={i18n.t('Data set')}
dense
onChange={({ selected }) =>
setFilter(selected ? [selected] : undefined)
selected.length < 1
? setFilter(undefined)
: setFilter(selected.map((s) => s.id))
}
select={(value) => [
{ id: 'NO_DATASET', displayName: '<No Data set>' },
...value,
]}
/>
)

// return (
// <ModelFilterSelect
// placeholder={i18n.t('Data set')}
// query={query}
// selected={selected}
// onChange={({ selected }) =>
// setFilter(selected ? [selected] : undefined)
// }
// />
// )
}
7 changes: 6 additions & 1 deletion src/lib/sectionList/filters/filterConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const filterParamsSchema = z
formType: z.array(z.nativeEnum(DataSet.formType)),
categoryOption: zodArrayIds,
categoryOptionGroup: zodArrayIds,
dataSet: zodArrayIds,
dataSet: z.array(
z
.string()
.refine((val) => isValidUid(val))
.or(z.literal('NO_DATASET'))
),
domainType: z.array(z.nativeEnum(DataElement.domainType)),
publicAccess: z.array(
z.string().refine((val) => parseAccessString(val) !== null)
Expand Down
14 changes: 10 additions & 4 deletions src/lib/sectionList/filters/parseFiltersToQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ const filterToQueryParamMap: FilterToQueryParamsMap = {
return inFilter('categoryCombo.id', value)
},
categoryOptionGroup: (value) => inFilter('categoryOptionGroups.id', value),
dataSet: (value, section) =>
section.name === SchemaName.dataElement
? inFilter('dataSetElements.dataSet.id', value)
: defaultFilter('dataSet', value),
dataSet: (value, section) => {
if (section.name === SchemaName.dataElement) {
const emptyIsSelected = value.filter((id) => id === 'NO_DATASET')
if (emptyIsSelected.length > 0) {
return `dataSetElements:empty`
}
return inFilter('dataSetElements.dataSet.id', value)
}
return defaultFilter('dataSet', value)
},
publicAccess: (value) => inFilter('sharing.public', value),
}

Expand Down

0 comments on commit dbfca87

Please sign in to comment.