@@ -60,7 +60,34 @@ class ExplorerFilter extends React.Component {
60
60
return null ;
61
61
}
62
62
63
- componentDidUpdate ( ) {
63
+ /**
64
+ * Little helper function to apply accessibility filters to aggregation histogram results
65
+ * @param {* } accessibleValues list of values that user has access to
66
+ * @param {* } histogram raw histogram array
67
+ * @returns modified histogram after applying accessibility filters
68
+ */
69
+ applyAccessibleFilter = ( accessibleValues , histogram ) => {
70
+ if ( ! Array . isArray ( histogram ) ) {
71
+ throw new Error ( 'Invalid histogram format' ) ;
72
+ }
73
+ return histogram . filter ( ( { key } ) => {
74
+ const accessible = accessibleValues . includes ( key ) ;
75
+ switch ( this . state . selectedAccessFilter ) {
76
+ case 'all-data' :
77
+ return true ; // always show all items if 'all-data'
78
+ case 'with-access' :
79
+ return accessible ; // only show accessible items if 'with-access'
80
+ case 'without-access' :
81
+ return ! accessible ; // only show unaccessible items if 'without-access'
82
+ default :
83
+ throw new Error ( 'Invalid access filter option' ) ;
84
+ }
85
+ } )
86
+ . map ( ( { key, count } ) => ( {
87
+ key,
88
+ count,
89
+ accessible : accessibleValues . includes ( key ) ,
90
+ } ) ) ;
64
91
}
65
92
66
93
/**
@@ -87,26 +114,12 @@ class ExplorerFilter extends React.Component {
87
114
// if the field is in accessibleFieldObject, add "accessible=false"
88
115
// to those items which are unaccessible
89
116
const accessibleValues = this . props . accessibleFieldObject [ field ] ;
90
- const newHistogram = aggsData [ field ] . histogram
91
- . filter ( ( { key } ) => {
92
- const accessible = accessibleValues . includes ( key ) ;
93
- switch ( this . state . selectedAccessFilter ) {
94
- case 'all-data' :
95
- return true ; // always show all items if 'all-data'
96
- case 'with-access' :
97
- return accessible ; // only show accessible items if 'with-access'
98
- case 'without-access' :
99
- return ! accessible ; // only show unaccessible items if 'without-access'
100
- default :
101
- throw new Error ( 'Invalid access filter option' ) ;
102
- }
103
- } )
104
- . map ( ( { key, count } ) => ( {
105
- key,
106
- count,
107
- accessible : accessibleValues . includes ( key ) ,
108
- } ) ) ;
117
+ const newHistogram = this . applyAccessibleFilter ( accessibleValues , aggsData [ field ] . histogram ) ;
109
118
res [ field ] = { histogram : newHistogram } ;
119
+ if ( Array . isArray ( aggsData [ field ] . asTextHistogram ) ) {
120
+ const newAsTextHistogram = this . applyAccessibleFilter ( accessibleValues , aggsData [ field ] . asTextHistogram ) ;
121
+ res [ field ] = { ...res [ field ] , asTextHistogram : newAsTextHistogram } ;
122
+ }
110
123
return res ;
111
124
} , { } ) ;
112
125
return newAggsData ;
0 commit comments