Skip to content

Commit

Permalink
fix(filters): dimension items selection (#278)
Browse files Browse the repository at this point in the history
Always set the filter items by replacing the existing one.
The logic depends on the component where the selection is happening
(via checkboxes, selects, ItemSelector...), the components know how to
make the list of selected items.
  • Loading branch information
edoardo authored Apr 11, 2019
1 parent df3d80f commit 8344c4a
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/components/ItemFilter/FilterSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { sGetActiveModalDimension } from '../../reducers/activeModalDimension';
import { sGetDimensions } from '../../reducers/dimensions';
import { sGetFiltersKeys } from '../../reducers/itemFilters';
import { sGetEditItemFiltersRoot } from '../../reducers/editItemFilters';
import { acAddItemFilter, acSetItemFilters } from '../../actions/itemFilters';
import { acAddItemFilter, acRemoveItemFilter } from '../../actions/itemFilters';
import {
acRemoveEditItemFilter,
acSetEditItemFilters,
Expand Down Expand Up @@ -55,22 +55,15 @@ class FilterSelector extends Component {
};

onSelectItems = ({ dimensionType: dimensionId, value: items }) => {
const oldList = this.props.selectedItems[dimensionId] || [];
const newList = [
...oldList,
...items.filter(item => !oldList.find(i => i.id === item.id)),
];

this.props.setEditItemFilters({
...this.props.selectedItems,
[dimensionId]: newList,
[dimensionId]: items,
});
};

onDeselectItems = ({ dimensionType: dimensionId, value: idsToRemove }) => {
const newList = this.props.selectedItems[dimensionId].filter(
item => !idsToRemove.includes(item.id)
);
const oldList = this.props.selectedItems[dimensionId] || [];
const newList = oldList.filter(item => !idsToRemove.includes(item.id));

if (newList.length) {
this.props.setEditItemFilters({
Expand All @@ -83,8 +76,10 @@ class FilterSelector extends Component {
};

onReorderItems = ({ dimensionType: dimensionId, value: ids }) => {
const items = this.props.selectedItems[dimensionId];
const reorderedList = ids.map(id => items.find(item => item.id === id));
const oldList = this.props.selectedItems[dimensionId] || [];
const reorderedList = ids.map(id =>
oldList.find(item => item.id === id)
);

this.props.setEditItemFilters({
...this.props.selectedItems,
Expand All @@ -93,10 +88,16 @@ class FilterSelector extends Component {
};

saveFilter = id => {
this.props.addItemFilter({
id,
value: [...this.props.selectedItems[id]],
});
const filterItems = this.props.selectedItems[id];

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

this.closeDialog();
};
Expand Down Expand Up @@ -160,7 +161,7 @@ export default connect(
clearActiveModalDimension: acClearActiveModalDimension,
setActiveModalDimension: acSetActiveModalDimension,
addItemFilter: acAddItemFilter,
setItemFilters: acSetItemFilters,
removeItemFilter: acRemoveItemFilter,
removeEditItemFilter: acRemoveEditItemFilter,
setEditItemFilters: acSetEditItemFilters,
}
Expand Down

0 comments on commit 8344c4a

Please sign in to comment.