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: dashboard filter - filter dialog incorrectly shows filter as selected even though it was removed [DHIS2-9560] #1074

Merged
merged 17 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2efb2d9
fix: get title and desc from the printDashboard redux prop
jenniferarnesen Sep 10, 2020
e1f1f8e
Merge branch 'master' into fix/wrong-title-in-print-preview
jenniferarnesen Sep 10, 2020
781806e
Merge branch 'master' into fix/wrong-title-in-print-preview
jenniferarnesen Sep 16, 2020
f123463
fix: revert refactor and save it for later
jenniferarnesen Sep 17, 2020
256ac8b
fix: move item handling to FilterDialog so initial list setting works
jenniferarnesen Sep 18, 2020
2e9b30c
fix: remove unnecessary code
jenniferarnesen Sep 18, 2020
437bbb9
fix: remove orObject from MSTP
jenniferarnesen Sep 18, 2020
63955c8
fix: remove prop that was basically duplicate causing unneeded rerenders
jenniferarnesen Sep 18, 2020
c94d7fb
fix: remove orObject from MSTP
jenniferarnesen Sep 18, 2020
68ce288
Merge branch 'fix/reduce-rennders-caused-by-state-change' into fix/cl…
jenniferarnesen Sep 21, 2020
39ca895
Merge branch 'fix/wrong-title-in-print-preview' into fix/clear-item-f…
jenniferarnesen Sep 21, 2020
ab2d13d
Merge branch 'master' into fix/clear-item-filter-in-dialog
jenniferarnesen Sep 21, 2020
e2677bb
Merge branch 'master' into fix/clear-item-filter-in-dialog
jenniferarnesen Sep 24, 2020
795f476
fix: use better spacing in item title when the title wraps to multipl…
jenniferarnesen Sep 24, 2020
4db605a
Merge branch 'fix/clear-item-filter-in-dialog' of github.com:dhis2/da…
jenniferarnesen Sep 24, 2020
6b5b040
Merge branch 'master' into fix/clear-item-filter-in-dialog
jenniferarnesen Sep 24, 2020
b467a2f
fix: use better spacing in item title when the title wraps to multipl…
jenniferarnesen Sep 24, 2020
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
149 changes: 97 additions & 52 deletions src/components/ItemFilter/FilterDialog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react'
import React, { useState } from 'react'
import PropTypes from 'prop-types'

import { connect } from 'react-redux'
import i18n from '@dhis2/d2-i18n'
import {
Button,
Modal,
Expand All @@ -9,9 +10,6 @@ import {
ModalActions,
ButtonStrip,
} from '@dhis2/ui'

import i18n from '@dhis2/d2-i18n'

import {
PeriodDimension,
DynamicDimension,
Expand All @@ -20,21 +18,73 @@ import {
DIMENSION_ID_ORGUNIT,
} from '@dhis2/analytics'

class FilterDialog extends Component {
onConfirm = id => () => this.props.onConfirm(id)
import { acAddItemFilter, acRemoveItemFilter } from '../../actions/itemFilters'
import { sGetItemFiltersRoot } from '../../reducers/itemFilters'

const FilterDialog = (
{
displayNameProperty,
dimension,
initiallySelectedItems,
addItemFilter,
removeItemFilter,
onClose,
},
context
) => {
const [filters, setFilters] = useState(initiallySelectedItems)

renderDialogContent() {
const { displayNameProperty, dimension, selectedItems } = this.props
const dialogId = dimension.id
const onSelectItems = ({ dimensionId, items }) => {
setFilters({ [dimensionId]: items })
}

const onDeselectItems = ({ dimensionId, itemIdsToRemove }) => {
const oldList = filters[dimensionId] || []
const newList = oldList.filter(
item => !itemIdsToRemove.includes(item.id)
)

const list = newList.length ? newList : []
jenniferarnesen marked this conversation as resolved.
Show resolved Hide resolved

setFilters({ ...filters, [dimensionId]: list })
}

const onReorderItems = ({ dimensionId, itemIds }) => {
const oldList = filters[dimensionId] || []
const reorderedList = itemIds.map(id =>
oldList.find(item => item.id === id)
)

setFilters({ ...filters, [dimensionId]: reorderedList })
}

const saveFilter = () => {
const id = dimension.id
const filterItems = filters[id]

if (filterItems && filterItems.length) {
addItemFilter({
id,
value: [...filterItems],
})
} else {
removeItemFilter(id)
}

onClose(id)
}

const renderDialogContent = () => {
const commonProps = {
d2: this.context.d2,
onSelect: this.props.onSelect,
onDeselect: this.props.onDeselect,
onReorder: this.props.onReorder,
d2: context.d2,
onSelect: onSelectItems,
onDeselect: onDeselectItems,
onReorder: onReorderItems,
}

switch (dialogId) {
const selectedItems = filters[dimension.id] || []

switch (dimension.id) {
case DIMENSION_ID_PERIOD: {
return (
<PeriodDimension
Expand All @@ -55,59 +105,54 @@ class FilterDialog extends Component {
return (
<DynamicDimension
selectedItems={selectedItems}
dimensionId={dialogId}
dimensionId={dimension.id}
onSelect={commonProps.onSelect}
context={commonProps.d2}
/>
)
}
}

render() {
const { dimension, onClose } = this.props
const dialogId = dimension.id

return (
<>
{dialogId && (
<Modal onClose={onClose} position="top" large>
<ModalTitle>{dimension.name}</ModalTitle>
<ModalContent>
{this.renderDialogContent()}
</ModalContent>
<ModalActions>
<ButtonStrip>
<Button secondary onClick={onClose}>
{i18n.t('Cancel')}
</Button>
<Button
primary
onClick={this.onConfirm(dialogId)}
>
{i18n.t('Confirm')}
</Button>
</ButtonStrip>
</ModalActions>
</Modal>
)}
</>
)
}
return (
<>
{dimension.id && (
<Modal onClose={onClose} position="top" large>
<ModalTitle>{dimension.name}</ModalTitle>
<ModalContent>{renderDialogContent()}</ModalContent>
<ModalActions>
<ButtonStrip>
<Button secondary onClick={onClose}>
{i18n.t('Cancel')}
</Button>
<Button primary onClick={saveFilter}>
{i18n.t('Confirm')}
</Button>
</ButtonStrip>
</ModalActions>
</Modal>
)}
</>
)
}

FilterDialog.propTypes = {
addItemFilter: PropTypes.func,
dimension: PropTypes.object,
displayNameProperty: PropTypes.string,
selectedItems: PropTypes.array,
initiallySelectedItems: PropTypes.object,
removeItemFilter: PropTypes.func,
onClose: PropTypes.func,
onConfirm: PropTypes.func,
onDeselect: PropTypes.func,
onReorder: PropTypes.func,
onSelect: PropTypes.func,
}

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

export default FilterDialog
const mapStateToProps = state => ({
initiallySelectedItems: sGetItemFiltersRoot(state),
})

export default connect(mapStateToProps, {
addItemFilter: acAddItemFilter,
removeItemFilter: acRemoveItemFilter,
})(FilterDialog)
67 changes: 6 additions & 61 deletions src/components/ItemFilter/FilterSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useRef } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown'
import isEmpty from 'lodash/isEmpty'

import i18n from '@dhis2/d2-i18n'
import { DimensionsPanel } from '@dhis2/analytics'
Expand All @@ -12,26 +13,20 @@ import FilterDialog from './FilterDialog'
import { sGetSettingsDisplayNameProperty } from '../../reducers/settings'
import { sGetActiveModalDimension } from '../../reducers/activeModalDimension'
import { sGetDimensions } from '../../reducers/dimensions'
import {
sGetItemFiltersRoot,
sGetFiltersKeys,
} from '../../reducers/itemFilters'
import { acAddItemFilter, acRemoveItemFilter } from '../../actions/itemFilters'
import { sGetFiltersKeys } from '../../reducers/itemFilters'

import {
acClearActiveModalDimension,
acSetActiveModalDimension,
} from '../../actions/activeModalDimension'

const FilterSelector = props => {
const [showPopover, setShowPopover] = useState(false)
const [filters, setFilters] = useState(props.initiallySelectedItems)

const ref = useRef(null)

const closePanel = () => setShowPopover(false)

const onCloseDialog = () => {
closePanel()
setShowPopover(false)

props.clearActiveModalDimension()
}
Expand All @@ -42,45 +37,6 @@ const FilterSelector = props => {
)
}

const onSelectItems = ({ dimensionId, items }) => {
setFilters({ ...filters, [dimensionId]: items })
}

const onDeselectItems = ({ dimensionId, itemIdsToRemove }) => {
const oldList = filters[dimensionId] || []
const newList = oldList.filter(
item => !itemIdsToRemove.includes(item.id)
)

const list = newList.length ? newList : []

setFilters({ ...filters, [dimensionId]: list })
}

const onReorderItems = ({ dimensionId, itemIds }) => {
const oldList = filters[dimensionId] || []
const reorderedList = itemIds.map(id =>
oldList.find(item => item.id === id)
)

setFilters({ ...filters, [dimensionId]: reorderedList })
}

const saveFilter = id => {
const filterItems = filters[id]

if (filterItems && filterItems.length) {
props.addItemFilter({
id,
value: [...filterItems],
})
} else {
props.removeItemFilter(id)
}

onCloseDialog()
}

return (
<>
<span ref={ref}>
Expand All @@ -91,7 +47,7 @@ const FilterSelector = props => {
</span>
{showPopover && (
<Popover
onClickOutside={closePanel}
onClickOutside={onCloseDialog}
reference={ref}
arrow={true}
placement="bottom-start"
Expand All @@ -104,16 +60,11 @@ const FilterSelector = props => {
/>
</Popover>
)}
{props.dimension ? (
{!isEmpty(props.dimension) ? (
<FilterDialog
displayNameProperty={props.displayNameProperty}
dimension={props.dimension}
selectedItems={filters[props.dimension.id] || []}
onSelect={onSelectItems}
onDeselect={onDeselectItems}
onReorder={onReorderItems}
onClose={onCloseDialog}
onConfirm={saveFilter}
/>
) : null}
</>
Expand All @@ -124,25 +75,19 @@ const mapStateToProps = state => ({
displayNameProperty: sGetSettingsDisplayNameProperty(state),
dimension: sGetActiveModalDimension(state),
dimensions: sGetDimensions(state),
initiallySelectedItems: sGetItemFiltersRoot(state),
selectedDimensions: sGetFiltersKeys(state),
})

FilterSelector.propTypes = {
addItemFilter: PropTypes.func,
clearActiveModalDimension: PropTypes.func,
dimension: PropTypes.object,
dimensions: PropTypes.array,
displayNameProperty: PropTypes.string,
initiallySelectedItems: PropTypes.object,
removeItemFilter: PropTypes.func,
selectedDimensions: PropTypes.array,
setActiveModalDimension: PropTypes.func,
}

export default connect(mapStateToProps, {
clearActiveModalDimension: acClearActiveModalDimension,
setActiveModalDimension: acSetActiveModalDimension,
addItemFilter: acAddItemFilter,
removeItemFilter: acRemoveItemFilter,
})(FilterSelector)