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 scope bug #13695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,96 @@ describe('getFilterScopeFromNodesTree', () => {
immune: [105, 103, 102, 101, 108, 104],
});
});

it('exclude nested sub-tab', () => {
// another layout for test:
// - filter_109
// - chart_106
// - Tab 1
// - Nested_Tab1
// - chart_101
// - chart_102
// - Nested_Tab2
// - chart_103
// - chart_104
const nodes3 = [
{
label: 'All dashboard',
type: 'ROOT',
value: 'ROOT_ID',
children: [
{
label: 'Time Filter',
showCheckbox: true,
type: 'CHART',
value: 109,
},
{
label: "World's Pop Growth",
showCheckbox: true,
type: 'CHART',
value: 106,
},
{
label: 'Row Tab 1',
type: 'TAB',
value: 'TAB-w5Fp904Rs',
children: [
{
label: 'Nested Tab 1',
type: 'TAB',
value: 'TAB-E4mJaZ-uQM',
children: [
{
value: 104,
label: 'Rural Breakdown',
type: 'CHART',
showCheckbox: true,
},
{
value: 103,
label: '% Rural',
type: 'CHART',
showCheckbox: true,
},
],
},
{
value: 'TAB-rLYu-Cryu',
label: 'Nested Tab 2',
type: 'TAB',
children: [
{
value: 102,
label: 'Most Populated Countries',
type: 'CHART',
showCheckbox: true,
},
{
value: 101,
label: "World's Population",
type: 'CHART',
showCheckbox: true,
},
],
},
],
},
],
},
];

const checkedChartIds = [103, 104, 106];
expect(
getFilterScopeFromNodesTree({
filterKey: '109___time_range',
nodes: nodes3,
checkedChartIds,
}),
).toEqual({
scope: ['ROOT_ID'],
immune: [102, 101],
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,29 @@ import { flatMap, isEmpty } from 'lodash';
import { CHART_TYPE, TAB_TYPE } from './componentTypes';
import { getChartIdAndColumnFromFilterKey } from './getDashboardFilterKey';

function getImmuneChartIdsFromTabsNotInScope({ tabs = [], tabsInScope = [] }) {
const chartsNotInScope = [];
tabs.forEach(({ value: tab, children: tabChildren }) => {
if (tabChildren && !tabsInScope.includes(tab)) {
tabChildren.forEach(({ value: subTab, children: subTabChildren }) => {
if (subTabChildren && !tabsInScope.includes(subTab)) {
chartsNotInScope.push(
...subTabChildren.filter(({ type }) => type === CHART_TYPE),
);
}
});
}
});

// return chartId only
return chartsNotInScope.map(({ value }) => value);
Comment on lines +26 to +40
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this could probably be done more succinctly with a reduce? I can try to work my way through it as a suggestion if you'd like

Copy link
Author

@graceguo-supercat graceguo-supercat Mar 19, 2021

Choose a reason for hiding this comment

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

I would use reduce a few month ago...Recently @ktmud gave me a code review comment and says reduce is hard to read (for many ppl)...like this thread 🤣
https://twitter.com/jaffathecake/status/1213077702300852224?lang=en

So now i try to avoid using reduce. Sorry i can't find original PR :)

Copy link
Member

Choose a reason for hiding this comment

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

lol TIL!

I do wonder what would happen if we all used reduce more often; maybe it would be as easy to read as map! But this works :D

}
function getTabChildrenScope({
tabScopes,
parentNodeValue,
forceAggregate = false,
hasChartSiblings = false,
tabChildren = [],
immuneChartSiblings = [],
}) {
// if all sub-tabs are in scope, or forceAggregate = true
Expand All @@ -38,9 +56,26 @@ function getTabChildrenScope({
([key, { scope }]) => scope && scope.length && key === scope[0],
))
) {
// get all charts from tabChildren that is not in scope
const immuneChartIdsFromTabsNotInScope = getImmuneChartIdsFromTabsNotInScope(
{
tabs: tabChildren,
tabsInScope: flatMap(tabScopes, ({ scope }) => scope),
},
);
const immuneChartIdsFromTabsInScope = flatMap(
Object.values(tabScopes),
({ immune }) => immune,
);
const immuneCharts = [
...new Set([
...immuneChartIdsFromTabsNotInScope,
...immuneChartIdsFromTabsInScope,
]),
];
return {
scope: [parentNodeValue],
immune: flatMap(Object.values(tabScopes), ({ immune }) => immune),
immune: immuneCharts,
};
}

Expand Down Expand Up @@ -96,6 +131,7 @@ function traverse({ currentNode = {}, filterId, checkedChartIds = [] }) {
tabScopes,
parentNodeValue: currentValue,
forceAggregate: true,
tabChildren,
});
return {
scope,
Expand All @@ -109,6 +145,7 @@ function traverse({ currentNode = {}, filterId, checkedChartIds = [] }) {
tabScopes,
parentNodeValue: currentValue,
hasChartSiblings: !isEmpty(chartChildren),
tabChildren,
immuneChartSiblings: chartsImmune,
});
}
Expand Down