diff --git a/src/components/sectionList/filters/filterSelectors/ConstantSelectionFilter.tsx b/src/components/sectionList/filters/filterSelectors/ConstantSelectionFilter.tsx index 64647e71..b6932eef 100644 --- a/src/components/sectionList/filters/filterSelectors/ConstantSelectionFilter.tsx +++ b/src/components/sectionList/filters/filterSelectors/ConstantSelectionFilter.tsx @@ -10,6 +10,7 @@ type ConstantSelectionFilterProps = { constants: Record filterKey: FilterKey filterable?: boolean + formatFilter?: (filter: string | undefined) => string | undefined } export const ConstantSelectionFilter = ({ @@ -17,10 +18,15 @@ export const ConstantSelectionFilter = ({ filterKey, label, filterable, + formatFilter, }: ConstantSelectionFilterProps) => { const [filter, setFilter] = useSectionListFilter(filterKey) - const selected = Array.isArray(filter) ? filter[0] : filter + let selected = Array.isArray(filter) ? filter[0] : filter + if (formatFilter) { + selected = formatFilter(selected) + } + const isInOptions = selected && constants[selected as keyof typeof constants] diff --git a/src/components/sectionList/filters/filterSelectors/PublicAccessFilter.tsx b/src/components/sectionList/filters/filterSelectors/PublicAccessFilter.tsx index e5e0df45..f9111c93 100644 --- a/src/components/sectionList/filters/filterSelectors/PublicAccessFilter.tsx +++ b/src/components/sectionList/filters/filterSelectors/PublicAccessFilter.tsx @@ -1,7 +1,11 @@ import i18n from '@dhis2/d2-i18n' import React from 'react' +import { formatPublicAccess, parsePublicAccessString } from '../../../../lib' import { ConstantSelectionFilter } from './ConstantSelectionFilter' +// currently we only care about metadata access +// we may want to revist this and potentially rename to "publicMetadataAccess" +// and have another component for data access const constants = { 'rw------': 'Public can edit', 'r-------': 'Public can view', @@ -9,11 +13,27 @@ const constants = { } export const PublicAccessFilter = () => { + const formatFilter = (filter: string | undefined) => { + if (!filter) { + return undefined + } + const parsedPublicAccessString = parsePublicAccessString(filter) + if (!parsedPublicAccessString) { + return undefined + } + const withoutDataAccess = formatPublicAccess({ + metadata: parsedPublicAccessString.metadata, + data: { read: false, write: false }, + }) + return withoutDataAccess + } + return ( ) } diff --git a/src/components/sectionList/filters/useFilterKeys.tsx b/src/components/sectionList/filters/useFilterKeys.tsx index e19f165b..ab7425ed 100644 --- a/src/components/sectionList/filters/useFilterKeys.tsx +++ b/src/components/sectionList/filters/useFilterKeys.tsx @@ -6,6 +6,9 @@ import { } from '../../../lib' import { useModelListView } from '../listView' +/** + * Get the filterKeys for for which filters to show. + * This depends on the current "modelList" view, and selected filters with values in the url */ export const useFilterKeys = () => { const [filters] = useSectionListFilters() const { filters: viewFilters } = useModelListView() diff --git a/src/lib/models/index.ts b/src/lib/models/index.ts index 725e338c..b7d55ba4 100644 --- a/src/lib/models/index.ts +++ b/src/lib/models/index.ts @@ -1,3 +1,6 @@ export { isValidUid } from './uid' -export { parsePublicAccessString } from './parsePublicAccess' +export { + parsePublicAccessString, + formatPublicAccess, +} from './parsePublicAccess' export { getIn, stringToPathArray, getFieldFilterFromPath } from './path' diff --git a/src/lib/models/parsePublicAccess.ts b/src/lib/models/parsePublicAccess.ts index 44e7ea7b..968cae63 100644 --- a/src/lib/models/parsePublicAccess.ts +++ b/src/lib/models/parsePublicAccess.ts @@ -26,10 +26,24 @@ export const parsePublicAccessString = ( if (!matches) { return null } - const [_, metadata, data] = matches + const [, metadata, data] = matches return { metadata: parseAccessPart(metadata), data: parseAccessPart(data), } } + +const accessPartToString = (accessPart: PublicAccessPart): string => { + if (accessPart.write) { + return 'rw' + } + return accessPart.read ? 'r-' : '--' +} + +export const formatPublicAccess = (publicAccess: PublicAccess): string => { + const metadata = accessPartToString(publicAccess.metadata) + const data = accessPartToString(publicAccess.data) + + return metadata + data + '----' +}