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: ou item filter on App items was crashing Dashboards [DHIS2-9725] #1183

Merged
merged 3 commits into from
Oct 19, 2020
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@
"devDependencies": {
"@dhis2/cli-app-scripts": "^5",
"@dhis2/cli-style": "^7.0.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"d2-manifest": "^1.0.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.5.0",
"immutability-helper": "^3.1.1",
"patch-package": "^6.2.2",
"postinstall-postinstall": "^2.1.0"
"postinstall-postinstall": "^2.1.0",
"redux-mock-store": "^1.5.4"
},
"jest": {
"setupFilesAfterEnv": [
Expand Down
2 changes: 0 additions & 2 deletions src/actions/itemFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
CLEAR_ITEM_FILTERS,
} from '../reducers/itemFilters'

export const FILTER_ORG_UNIT = 'ou'

// actions

export const acAddItemFilter = filter => ({
Expand Down
33 changes: 13 additions & 20 deletions src/components/Item/AppItem/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@ import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import NotInterestedIcon from '@material-ui/icons/NotInterested'

import { FILTER_ORG_UNIT } from '../../../actions/itemFilters'
import {
sGetItemFiltersRoot,
DEFAULT_STATE_ITEM_FILTERS,
} from '../../../reducers/itemFilters'
import ItemHeader from '../ItemHeader/ItemHeader'
import Line from '../../../widgets/Line'

import { sGetOuItemFilters } from '../../../reducers/itemFilters'

import { isEditMode } from '../../Dashboard/dashboardModes'

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

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

iframeSrc += `&userOrgUnit=${ouIds.join(',')}`
Expand All @@ -31,7 +24,7 @@ const getIframeSrc = (appDetails, item, itemFilters) => {
return iframeSrc
}

const AppItem = ({ dashboardMode, item, itemFilters }, context) => {
const AppItem = ({ dashboardMode, item, ouItemFilters }, context) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good.
It's been a while, but I think the original idea here was to pass all the filters in itemFilters and pass them to the iframe URL.
Apparently only the ou one was implemented, but I'm wondering if we should keep itemFilters for when other filters need to be passed in (like period).
Perhaps all filters should already be passed?
Probably not all AppItem widgets support all of them but wouldn't it be more correct to pass all (not sure about the URL query parameters naming)?

let appDetails

const appKey = item.appKey
Expand All @@ -53,7 +46,7 @@ const AppItem = ({ dashboardMode, item, itemFilters }, context) => {
<Line />
<iframe
title={appDetails.name}
src={getIframeSrc(appDetails, item, itemFilters)}
src={getIframeSrc(appDetails, item, ouItemFilters)}
className="dashboard-item-content"
style={{ border: 'none' }}
/>
Expand Down Expand Up @@ -84,19 +77,19 @@ const AppItem = ({ dashboardMode, item, itemFilters }, context) => {
AppItem.propTypes = {
dashboardMode: PropTypes.string,
item: PropTypes.object,
itemFilters: PropTypes.object,
ouItemFilters: PropTypes.array,
}

AppItem.contextTypes = {
d2: PropTypes.object,
}

const mapStateToProps = (state, ownProps) => {
const itemFilters = !isEditMode(ownProps.dashboardMode)
? sGetItemFiltersRoot(state)
: DEFAULT_STATE_ITEM_FILTERS
const ouItemFilters = !isEditMode(ownProps.dashboardMode)
? sGetOuItemFilters(state)
: undefined

return { itemFilters }
return { ouItemFilters }
}

export default connect(mapStateToProps)(AppItem)
117 changes: 117 additions & 0 deletions src/components/Item/AppItem/__tests__/Item.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React from 'react'
import PropTypes from 'prop-types'
import { render } from '@testing-library/react'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'

import Item from '../Item'

const mockStore = configureMockStore()

const item = {
appKey: 'scorecard',
id: 'rainbowdash',
shortened: false,
}

test('renders a valid App item in view mode', () => {
const store = {
itemFilters: {},
}
const { container } = render(
<Provider store={mockStore(store)}>
<D2Provider>
<Item item={item} dashboardMode={'view'} />
</D2Provider>
</Provider>
)
expect(container).toMatchSnapshot()
})

test('renders a valid App item with filter in view mode', () => {
const store = {
itemFilters: {
ou: [{ path: '/rainbow' }],
},
}

const { container } = render(
<Provider store={mockStore(store)}>
<D2Provider>
<Item item={item} dashboardMode={'view'} />
</D2Provider>
</Provider>
)
expect(container).toMatchSnapshot()
})

test('renders a valid App item with filter in edit mode', () => {
const store = {
itemFilters: {
ou: [{ path: '/rainbow' }],
},
}

const { container } = render(
<Provider store={mockStore(store)}>
<D2Provider>
<Item item={item} dashboardMode={'edit'} />
</D2Provider>
</Provider>
)
expect(container).toMatchSnapshot()
})

test('renders an invalid App item', () => {
const store = {
itemFilters: {
ou: [{ path: '/rainbow' }],
},
}

const invalidItem = {
appKey: 'unknownApp',
id: 'unknown',
shortened: false,
}

const { container } = render(
<Provider store={mockStore(store)}>
<D2Provider>
<Item item={invalidItem} dashboardMode={'edit'} />
</D2Provider>
</Provider>
)
expect(container).toMatchSnapshot()
})

// Mock context provider
class D2Provider extends React.Component {
getChildContext() {
return {
d2: {
system: {
installedApps: [
{
key: 'scorecard',
name: 'Scorecard',
launchUrl: 'launchurl',
},
],
},
},
}
}

render() {
return this.props.children
}
}

D2Provider.childContextTypes = {
d2: PropTypes.object,
}

D2Provider.propTypes = {
children: PropTypes.node,
}
124 changes: 124 additions & 0 deletions src/components/Item/AppItem/__tests__/__snapshots__/Item.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders a valid App item in view mode 1`] = `
<div>
<div
class="itemHeaderWrap"
>
<p
class="itemTitle"
>
Scorecard
</p>
</div>
<hr
style="background-color: rgb(243, 245, 247); height: 1px; margin: 0px 0px 5px 0px;"
/>
<iframe
class="dashboard-item-content"
src="launchurl?dashboardItemId=rainbowdash"
title="Scorecard"
/>
</div>
`;

exports[`renders a valid App item with filter in edit mode 1`] = `
<div>
<div
class="itemHeaderWrap"
>
<p
class="itemTitle"
>
Scorecard
</p>
<div
class="itemActionsWrap"
>
<button
class="deleteItemButton"
title="Delete item"
type="button"
>
<svg
aria-hidden="true"
class="MuiSvgIcon-root-1"
focusable="false"
role="presentation"
style="fill: #d32f2f;"
viewBox="0 0 24 24"
>
<path
d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"
/>
</svg>
</button>
</div>
</div>
<hr
style="background-color: rgb(243, 245, 247); height: 1px; margin: 0px 0px 5px 0px;"
/>
<iframe
class="dashboard-item-content"
src="launchurl?dashboardItemId=rainbowdash"
title="Scorecard"
/>
</div>
`;

exports[`renders a valid App item with filter in view mode 1`] = `
<div>
<div
class="itemHeaderWrap"
>
<p
class="itemTitle"
>
Scorecard
</p>
</div>
<hr
style="background-color: rgb(243, 245, 247); height: 1px; margin: 0px 0px 5px 0px;"
/>
<iframe
class="dashboard-item-content"
src="launchurl?dashboardItemId=rainbowdash&userOrgUnit=rainbow"
title="Scorecard"
/>
</div>
`;

exports[`renders an invalid App item 1`] = `
<div>
<div
class="itemHeaderWrap"
>
<p
class="itemTitle"
>
unknownApp app not found
</p>
</div>
<hr
style="background-color: rgb(243, 245, 247); height: 1px; margin: 0px 0px 5px 0px;"
/>
<div
class="dashboard-item-content"
style="display: flex; justify-content: center; align-items: center; height: 90%;"
>
<svg
aria-hidden="true"
class="MuiSvgIcon-root-10 MuiSvgIcon-colorDisabled-15"
disabled=""
focusable="false"
role="presentation"
style="width: 100px; height: 100px;"
viewBox="0 0 24 24"
>
<path
d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8 0-1.85.63-3.55 1.69-4.9L16.9 18.31C15.55 19.37 13.85 20 12 20zm6.31-3.1L7.1 5.69C8.45 4.63 10.15 4 12 4c4.42 0 8 3.58 8 8 0 1.85-.63 3.55-1.69 4.9z"
/>
</svg>
</div>
</div>
`;
4 changes: 4 additions & 0 deletions src/reducers/itemFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ export const sGetNamedItemFilters = createSelector(
return arr
}, [])
)

const FILTER_ORG_UNIT = 'ou'
export const sGetOuItemFilters = state =>
sGetItemFiltersRoot(state)[FILTER_ORG_UNIT]
Loading