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: parse the ou filter correctly for all ou types #2691

Merged
merged 7 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 1 addition & 15 deletions src/components/Item/AppItem/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@ import { Divider, colors, spacers, IconQuestion24 } from '@dhis2/ui'
import PropTypes from 'prop-types'
import React from 'react'
import { connect } from 'react-redux'
import { FILTER_ORG_UNIT } from '../../../actions/itemFilters.js'
import { EDIT, isEditMode } from '../../../modules/dashboardModes.js'
import {
sGetItemFiltersRoot,
DEFAULT_STATE_ITEM_FILTERS,
} from '../../../reducers/itemFilters.js'
import ItemHeader from '../ItemHeader/ItemHeader.js'

const getIframeSrc = (appDetails, item, itemFilters) => {
let iframeSrc = `${appDetails.launchUrl}?dashboardItemId=${item.id}`

if (itemFilters[FILTER_ORG_UNIT] && itemFilters[FILTER_ORG_UNIT].length) {
const ouIds = itemFilters[FILTER_ORG_UNIT].map(
(ouFilter) => ouFilter.path.split('/').slice(-1)[0]
)

iframeSrc += `&userOrgUnit=${ouIds.join(',')}`
}

return iframeSrc
}
import { getIframeSrc } from './getIframeSrc.js'

const AppItem = ({ dashboardMode, item, itemFilters }) => {
const { d2 } = useD2()
Expand Down
124 changes: 124 additions & 0 deletions src/components/Item/AppItem/__tests__/getIframeSrc.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { getIframeSrc } from '../getIframeSrc.js'

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

describe('getIframeSrc', () => {
it('no ou filter', () => {
const ouFilter = []

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}`
)
})

it('should return the correct iframe src', () => {
const ouFilter = [
{
id: 'fdc6uOvgoji',
path: '/ImspTQPwCqd/fdc6uOvgoji',
name: 'Bombali',
},
{
id: 'lc3eMKXaEfw',
path: '/ImspTQPwCqd/lc3eMKXaEfw',
name: 'Bonthe',
},
]

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}&userOrgUnit=fdc6uOvgoji,lc3eMKXaEfw`
)
})

it('org unit group in filter', () => {
const ouFilter = [
{
id: 'OU_GROUP-b0EsAxm8Nge',
name: 'Western Area',
},
{
id: 'lc3eMKXaEfw',
path: '/ImspTQPwCqd/lc3eMKXaEfw',
name: 'Bonthe',
},
]

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}&userOrgUnit=OU_GROUP-b0EsAxm8Nge,lc3eMKXaEfw`
)
})

it('org unit level in filter', () => {
const ouFilter = [
{
id: 'LEVEL-m9lBJogzE95',
name: 'Facility',
},
{
id: 'fdc6uOvgoji',
path: '/ImspTQPwCqd/fdc6uOvgoji',
name: 'Bombali',
},
]

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}&userOrgUnit=LEVEL-m9lBJogzE95,fdc6uOvgoji`
)
})

it('user org unit in filter', () => {
const ouFilter = [
{
id: 'USER_ORGUNIT',
displayName: 'User organisation unit',
},
]

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}&userOrgUnit=USER_ORGUNIT`
)
})

it('all user org units in filter', () => {
const ouFilter = [
{
id: 'USER_ORGUNIT_CHILDREN',
displayName: 'User sub-units',
},
{
id: 'USER_ORGUNIT_GRANDCHILDREN',
displayName: 'User sub-x2-units',
},
{
id: 'USER_ORGUNIT',
displayName: 'User organisation unit',
},
]

const appDetails = { launchUrl: 'debug/dev' }
const dashboardItem = { id: 'rainbowdashitem' }

const src = getIframeSrc(appDetails, dashboardItem, { ou: ouFilter })
expect(src).toEqual(
`${appDetails.launchUrl}?dashboardItemId=${dashboardItem.id}&userOrgUnit=USER_ORGUNIT_CHILDREN,USER_ORGUNIT_GRANDCHILDREN,USER_ORGUNIT`
)
})
})
17 changes: 17 additions & 0 deletions src/components/Item/AppItem/getIframeSrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FILTER_ORG_UNIT } from '../../../actions/itemFilters.js'

export const getIframeSrc = (appDetails, item, itemFilters) => {
let iframeSrc = `${appDetails.launchUrl}?dashboardItemId=${item.id}`

console.log('jj itemfilters', itemFilters)
jenniferarnesen marked this conversation as resolved.
Show resolved Hide resolved

if (itemFilters[FILTER_ORG_UNIT] && itemFilters[FILTER_ORG_UNIT].length) {
const ouIds = itemFilters[FILTER_ORG_UNIT].map(({ id, path }) =>
jenniferarnesen marked this conversation as resolved.
Show resolved Hide resolved
path ? path.split('/').slice(-1)[0] : id
)

iframeSrc += `&userOrgUnit=${ouIds.join(',')}`
}

return iframeSrc
}