Skip to content

Commit

Permalink
refactor: more robust publicAccess filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Feb 28, 2024
1 parent 432ac43 commit 39762f6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ type ConstantSelectionFilterProps = {
constants: Record<string, string>
filterKey: FilterKey
filterable?: boolean
formatFilter?: (filter: string | undefined) => string | undefined
}

export const ConstantSelectionFilter = ({
constants,
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]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
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',
'--------': 'Public cannot access',
}

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 (
<ConstantSelectionFilter
label={i18n.t('Public access')}
filterKey="publicAccess"
constants={constants}
formatFilter={formatFilter}
/>
)
}
3 changes: 3 additions & 0 deletions src/components/sectionList/filters/useFilterKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/lib/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { isValidUid } from './uid'
export { parsePublicAccessString } from './parsePublicAccess'
export {
parsePublicAccessString,
formatPublicAccess,
} from './parsePublicAccess'
export { getIn, stringToPathArray, getFieldFilterFromPath } from './path'
16 changes: 15 additions & 1 deletion src/lib/models/parsePublicAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '----'
}

0 comments on commit 39762f6

Please sign in to comment.