Skip to content

Commit

Permalink
feat: add publicAccessFilter
Browse files Browse the repository at this point in the history
Birkbjo committed Jan 29, 2024
1 parent eb4ce54 commit a7e5d2f
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/sectionList/filters/DynamicFilters.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import {
DomainTypeSelectionFilter,
ValueTypeSelectionFilter,
} from './filterSelectors'
import { PublicAccessFilter } from './filterSelectors/PublicAccessFilter'
import { useFilterKeys } from './useFilterKeys'

type FilterKeyToComponentMap = Partial<Record<ConfigurableFilterKey, React.FC>>
@@ -17,6 +18,7 @@ const filterKeyToComponentMap: FilterKeyToComponentMap = {
domainType: DomainTypeSelectionFilter,
valueType: ValueTypeSelectionFilter,
aggregationType: AggregationTypeFilter,
publicAccess: PublicAccessFilter,
}

export const DynamicFilters = () => {
Original file line number Diff line number Diff line change
@@ -19,13 +19,18 @@ export const ConstantSelectionFilter = ({
filterable,
}: ConstantSelectionFilterProps) => {
const [filter, setFilter] = useSectionListFilter(filterKey)

const selected = Array.isArray(filter) ? filter[0] : filter
const isInOptions =
selected && constants[selected as keyof typeof constants]

return (
<SingleSelect
className={css.constantSelectionFilter}
onChange={({ selected }: SelectOnChangeObject) => {
setFilter(selected ? [selected] : undefined)
}}
selected={Array.isArray(filter) ? filter[0] : filter}
selected={isInOptions ? selected : undefined}
placeholder={label}
dense
prefix={label}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import i18n from '@dhis2/d2-i18n'
import React from 'react'
import { ConstantSelectionFilter } from './ConstantSelectionFilter'

const constants = {
'rw------': 'Public can edit',
'r-------': 'Public can view',
'--------': 'Public cannot access',
}

export const PublicAccessFilter = () => {
return (
<ConstantSelectionFilter
label={i18n.t('Public access')}
filterKey="publicAccess"
constants={constants}
/>
)
}
6 changes: 5 additions & 1 deletion src/lib/sectionList/filters/filterConfig.tsx
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { StringParam } from 'use-query-params'
import { z } from 'zod'
import { DataElement } from '../../../types/generated'
import { IDENTIFIABLE_KEY } from '../../constants'
import { isValidUid } from '../../models'
import { isValidUid, parsePublicAccessString } from '../../models'
import { CustomDelimitedArrayParam } from './customParams'

const zodArrayIds = z.array(z.string().refine((val) => isValidUid(val)))
@@ -15,6 +15,9 @@ export const filterParamsSchema = z
categoryCombo: zodArrayIds,
dataSet: zodArrayIds,
domainType: z.array(z.nativeEnum(DataElement.domainType)),
publicAccess: z.array(
z.string().refine((val) => parsePublicAccessString(val) !== null)
),
valueType: z.array(z.nativeEnum(DataElement.valueType)),
})
.partial()
@@ -28,6 +31,7 @@ export const filterQueryParamType = {
valueType: CustomDelimitedArrayParam,
dataSet: CustomDelimitedArrayParam,
categoryCombo: CustomDelimitedArrayParam,
publicAccess: CustomDelimitedArrayParam,
} as const satisfies QueryParamsConfigMap

export const validFilterKeys = Object.keys(filterQueryParamType)
10 changes: 8 additions & 2 deletions src/lib/sectionList/filters/parseFiltersToQueryParams.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@ const defaultFilter = (key: FilterKey, value: AllValues): string => {
return `${key}:${operator}:${valuesString}`
}

const inFilter = (filterPath: string, value: string[]) =>
`${filterPath}:in:[${value.join(',')}]`

const getQueryParamForFilter = (
key: FilterKey,
value: AllValues,
@@ -24,11 +27,14 @@ const getQueryParamForFilter = (
if (key === 'dataSet') {
const v = value as string[]
if (section?.name === SECTIONS_MAP.dataElement.name) {
return `dataSetElements.dataSet.id:in:[${v.join(',')}]`
return inFilter('dataSetElements.dataSet.id', v)
}
}
if (key === 'categoryCombo') {
return `categoryCombo.id:in:[${(value as string[]).join(',')}]`
return inFilter('categoryCombo.id', value as string[])
}
if (key === 'publicAccess') {
return inFilter('sharing.public', value as string[])
}
return defaultFilter(key, value)
}
6 changes: 5 additions & 1 deletion src/lib/sectionList/listViews/sectionListViewsConfig.ts
Original file line number Diff line number Diff line change
@@ -45,6 +45,10 @@ const FILTERS = {
label: i18n.t('Category combo'),
},
dataSet: { filterKey: 'dataSet', label: i18n.t('Data set') },
publicAccess: {
filterKey: 'publicAccess',
label: i18n.t('Public access'),
},
} satisfies Record<string, FilterDescriptor>

// This is the default views, and can be overriden per section in modelListViewsConfig below
@@ -98,7 +102,7 @@ export const modelListViewsConfig = {
FILTERS.dataSet,
'categoryCombo',
],
available: ['categoryCombo'],
available: ['categoryCombo', FILTERS.publicAccess],
},
},
} satisfies SectionListViewConfig

0 comments on commit a7e5d2f

Please sign in to comment.