Skip to content

Commit bcbacee

Browse files
authored
(MIDRC-666): add guppy defaultFilter functionality (#1508)
* MIDRC-666: add guppy defaultFilter functionality
1 parent a149dcd commit bcbacee

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

docs/multi_tab_explorer.md

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ An example of this new `explorerConfig` is (some contents are omitted for concis
3232
"asTextAggFields": [
3333
"consortium_id",
3434
...
35+
],
36+
"defaultFilters": [
37+
{
38+
"field": "redacted", "values": ["No"]
39+
}
3540
]
3641
}
3742
]

docs/portal_config.md

+6
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ Below is an example, with inline comments describing what each JSON block config
395395
],
396396
"asTextAggFields": [ // optional; GraphQL fields that would be aggregated as text fields. Only meaningful to numeric fields that HAS NOT been specified in the "charts" section before, there is no behavior differences if used on text fields
397397
"consortium_id"
398+
],
399+
"defaultFilters": [ // optional; select default filters on page load
400+
{
401+
"field": "redacted", // field name
402+
"values": ["No"] // selected values on page load
403+
}
398404
]
399405
}
400406
]

src/GuppyDataExplorer/GuppyDataExplorer.jsx

+26-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class GuppyDataExplorer extends React.Component {
2121
constructor(props) {
2222
super(props);
2323
let initialFilter = {};
24+
let defaultFilters = {};
25+
let hasInitialFilter = false;
2426

2527
if (isEnabled('explorerStoreFilterInURL')) {
2628
const stateFromURL = getQueryParameter('filters');
@@ -30,6 +32,7 @@ class GuppyDataExplorer extends React.Component {
3032
const isValidJSON = IsValidJSONString(decodedFilter);
3133
if (isValidJSON) {
3234
initialFilter = JSON.parse(decodedFilter).filter;
35+
hasInitialFilter = true;
3336
}
3437
} catch (err) {
3538
// eslint-disable-next-line no-console
@@ -38,10 +41,32 @@ class GuppyDataExplorer extends React.Component {
3841
}
3942
}
4043

44+
if (!hasInitialFilter && this.props.filterConfig.tabs.length > 0) {
45+
const defaultFilterArr = this.props.filterConfig.tabs.reduce((accumulator, currentValue) => {
46+
const df = currentValue.defaultFilters;
47+
if (df && df.length > 0) {
48+
return [...accumulator, ...df];
49+
}
50+
return accumulator;
51+
}, []);
52+
53+
defaultFilters = defaultFilterArr.reduce((tabAccumulator, tabDefaultFilter) => {
54+
if (tabDefaultFilter.field && tabDefaultFilter.values.length > 0) {
55+
// if it already exists add values to it
56+
if (tabAccumulator[tabDefaultFilter.field]) {
57+
tabAccumulator[tabDefaultFilter.field].selectedValues.push(...tabDefaultFilter.values);
58+
return tabAccumulator;
59+
}
60+
return { ...tabAccumulator, [tabDefaultFilter.field]: { selectedValues: tabDefaultFilter.values } };
61+
}
62+
return tabAccumulator;
63+
}, {});
64+
}
65+
4166
this.state = {
4267
aggsData: {},
4368
filter: {},
44-
initialFilterFromURL: initialFilter,
69+
initialFilterFromURL: hasInitialFilter ? initialFilter : defaultFilters,
4570
encodableExplorerStateForURL: { filter: initialFilter },
4671
};
4772
}

src/GuppyDataExplorer/configTypeDef.js

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const FilterConfigType = PropTypes.shape({
2323
tabs: PropTypes.arrayOf(PropTypes.shape({
2424
title: PropTypes.string,
2525
fields: PropTypes.arrayOf(PropTypes.string),
26+
defaultFilters: PropTypes.arrayOf(PropTypes.shape({
27+
field: PropTypes.string,
28+
values: PropTypes.arrayOf(PropTypes.string),
29+
})),
2630
})),
2731
});
2832

0 commit comments

Comments
 (0)