Skip to content

Commit e69765c

Browse files
committed
Change: Convert SeverityClassDisplay into a function component
Refactor SeverityClassDisplay into a function component and use new filterValueToFilterTerms utility function internally to convert severity class borders into filter terms.
1 parent ed51372 commit e69765c

File tree

1 file changed

+35
-57
lines changed

1 file changed

+35
-57
lines changed

src/web/components/dashboard/display/severity/SeverityClassDisplay.jsx

+35-57
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,26 @@
44
*/
55

66
import Filter from 'gmp/models/filter';
7-
import FilterTerm from 'gmp/models/filter/filterterm';
87
import {isDefined} from 'gmp/utils/identity';
98
import React from 'react';
109
import DonutChart from 'web/components/chart/Donut';
1110
import DataDisplay from 'web/components/dashboard/display/DataDisplay';
1211
import {renderDonutChartIcons} from 'web/components/dashboard/display/DataDisplayIcons';
1312
import transformSeverityData from 'web/components/dashboard/display/severity/SeverityClassTransform';
13+
import {filterValueToFilterTerms} from 'web/components/dashboard/display/severity/utils';
1414
import PropTypes from 'web/utils/PropTypes';
1515

16-
class SeverityClassDisplay extends React.Component {
17-
constructor(...args) {
18-
super(...args);
19-
20-
this.handleDataClick = this.handleDataClick.bind(this);
21-
}
22-
23-
handleDataClick(data) {
24-
const {onFilterChanged, filter} = this.props;
16+
const SeverityClassDisplay = ({onFilterChanged, filter, ...props}) => {
17+
const handleDataClick = data => {
2518
const {filterValue} = data;
2619

2720
let severityFilter;
2821
if (!isDefined(onFilterChanged)) {
2922
return;
3023
}
3124

32-
const {start, end} = filterValue;
33-
if (start > 0 && end < 10) {
34-
const startTerm = FilterTerm.fromString(`severity>${start}`);
35-
const endTerm = FilterTerm.fromString(`severity<${end}`);
36-
25+
const [startTerm, endTerm] = filterValueToFilterTerms(filterValue);
26+
if (isDefined(endTerm)) {
3727
if (
3828
isDefined(filter) &&
3929
filter.hasTerm(startTerm) &&
@@ -44,58 +34,46 @@ class SeverityClassDisplay extends React.Component {
4434

4535
severityFilter = Filter.fromTerm(startTerm).and(Filter.fromTerm(endTerm));
4636
} else {
47-
let severityTerm;
48-
if (start > 0) {
49-
severityTerm = FilterTerm.fromString(`severity>${start}`);
50-
} else {
51-
severityTerm = FilterTerm.fromString(`severity=${start}`);
52-
}
53-
54-
if (isDefined(filter) && filter.hasTerm(severityTerm)) {
37+
if (isDefined(filter) && filter.hasTerm(startTerm)) {
5538
return;
5639
}
5740

58-
severityFilter = Filter.fromTerm(severityTerm);
41+
severityFilter = Filter.fromTerm(startTerm);
5942
}
6043

6144
const newFilter = isDefined(filter)
6245
? filter.copy().and(severityFilter)
6346
: severityFilter;
6447

6548
onFilterChanged(newFilter);
66-
}
67-
68-
render() {
69-
const {onFilterChanged, ...props} = this.props;
70-
return (
71-
<DataDisplay
72-
{...props}
73-
dataTransform={transformSeverityData}
74-
icons={renderDonutChartIcons}
75-
initialState={{
76-
show3d: true,
77-
}}
78-
>
79-
{({width, height, data, svgRef, state}) => (
80-
<DonutChart
81-
data={data}
82-
height={height}
83-
show3d={state.show3d}
84-
showLegend={state.showLegend}
85-
svgRef={svgRef}
86-
width={width}
87-
onDataClick={
88-
isDefined(onFilterChanged) ? this.handleDataClick : undefined
89-
}
90-
onLegendItemClick={
91-
isDefined(onFilterChanged) ? this.handleDataClick : undefined
92-
}
93-
/>
94-
)}
95-
</DataDisplay>
96-
);
97-
}
98-
}
49+
};
50+
return (
51+
<DataDisplay
52+
{...props}
53+
dataTransform={transformSeverityData}
54+
filter={filter}
55+
icons={renderDonutChartIcons}
56+
initialState={{
57+
show3d: true,
58+
}}
59+
>
60+
{({width, height, data, svgRef, state}) => (
61+
<DonutChart
62+
data={data}
63+
height={height}
64+
show3d={state.show3d}
65+
showLegend={state.showLegend}
66+
svgRef={svgRef}
67+
width={width}
68+
onDataClick={isDefined(onFilterChanged) ? handleDataClick : undefined}
69+
onLegendItemClick={
70+
isDefined(onFilterChanged) ? handleDataClick : undefined
71+
}
72+
/>
73+
)}
74+
</DataDisplay>
75+
);
76+
};
9977

10078
SeverityClassDisplay.propTypes = {
10179
filter: PropTypes.filter,

0 commit comments

Comments
 (0)