Skip to content

Commit 475b460

Browse files
leSamokpatticha
authored and
kpatticha
committed
refactor: Refactor Remediation component
1 parent 038023a commit 475b460

File tree

1 file changed

+38
-56
lines changed

1 file changed

+38
-56
lines changed

src/Components/SmartComponents/Remediation/Remediation.js

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,30 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import propTypes from 'prop-types';
33
import { addNotification } from '@redhat-cloud-services/frontend-components-notifications/redux';
44
import RemediationButton from '@redhat-cloud-services/frontend-components-remediations/RemediationButton';
5-
import { AnsibeTowerIcon } from '@patternfly/react-icons';
5+
import { AnsibeTowerIcon } from '@patternfly/react-icons';
66
import routerParams from '@redhat-cloud-services/frontend-components-utilities/RouterParams';
77
import { connect } from 'react-redux';
88
import messages from '../../../Messages';
99
import { FormattedMessage } from 'react-intl';
1010
import { mergeObjectPropertyBy } from '../../../Helpers/MiscHelper';
11-
class Remediation extends Component {
12-
static propTypes = {
13-
cves: propTypes.oneOfType([
14-
propTypes.array,
15-
propTypes.string
16-
]),
17-
systems: propTypes.oneOfType([
18-
propTypes.array,
19-
propTypes.string
20-
]),
21-
addNotification: propTypes.func,
22-
manyRules: propTypes.bool
23-
};
24-
25-
shouldComponentUpdate(nextProps) {
26-
if (nextProps.cves !== this.props.cves) {
27-
return true;
28-
}
29-
30-
if (nextProps.systems !== this.props.systems) {
31-
return true;
32-
}
3311

34-
return false;
35-
}
12+
const Remediation = ({ cves, systems, manyRules, addNotification: dispatchNotification }) => {
13+
const baseIssueTemplate = (cve, system) => ({
14+
id: `vulnerabilities:${cve}`,
15+
description: cve,
16+
systems: [system]
17+
});
3618

37-
baseIssueTemplate = (cve, system) => (
38-
{
39-
id: `vulnerabilities:${cve}`,
40-
description: cve,
41-
systems: [system]
42-
}
43-
)
44-
45-
remediationProvider = (cvesProvider = [], systemsProvider = [], manyRules = false) => {
19+
const remediationProvider = (cvesProvider = [], systemsProvider = [], manyRules = false) => {
4620
let cves = [].concat(cvesProvider);
4721
let systems = [].concat(systemsProvider);
4822
let issues = [];
4923

5024
// CVE Details page where cves === 1 and systems > 1...N and cves linked rules > 1...N
5125
if (manyRules && cves.length === 1) {
52-
5326
issues = systems.reduce((prev, { id: systemID, cve: cveID, rule }) => {
54-
let issue = this.baseIssueTemplate(cveID, systemID);
27+
let issue = baseIssueTemplate(cveID, systemID);
5528

5629
if (rule) {
5730
issue.id = `${issue.id}:${rule?.rule?.rule_id}`;
@@ -61,44 +34,53 @@ class Remediation extends Component {
6134
}, []);
6235

6336
issues = mergeObjectPropertyBy(issues, 'systems');
64-
6537
}
6638

6739
// System cves where systems === 1 & cves > 1..N and cves linked rules === 1
6840
if (!manyRules && systems?.length === 1) {
6941
const [systemID] = systems;
7042

7143
issues = cves.reduce((acc, { id: cveID, rules }) => {
72-
let issue = this.baseIssueTemplate(cveID, systemID);
44+
let issue = baseIssueTemplate(cveID, systemID);
7345

7446
if (rules?.rule_id) {
7547
issue.id = `${issue.id}:${rules.rule_id}`;
7648
}
7749

7850
return [...acc, issue];
7951
}, []);
80-
8152
}
8253

8354
return cves.length && systems.length ? { issues } : false;
8455
};
8556

86-
render() {
87-
const { cves, systems, manyRules, addNotification: dispatchNotification } = this.props;
88-
return (
89-
<div>
90-
<RemediationButton
91-
dataProvider={() => this.remediationProvider(cves, systems, manyRules)}
92-
isDisabled={cves.length === 0 || systems.length === 0}
93-
onRemediationCreated={result => dispatchNotification(result.getNotification())}
94-
>
95-
<AnsibeTowerIcon size="sm" color={'var(--pf-global--BackgroundColor--100)'} />
96-
&nbsp;{<FormattedMessage {...messages.remediateLabel} />}
97-
</RemediationButton>
98-
</div>
99-
);
100-
}
101-
}
57+
return (
58+
<div>
59+
<RemediationButton
60+
dataProvider={() => remediationProvider(cves, systems, manyRules)}
61+
isDisabled={cves.length === 0 || systems.length === 0}
62+
onRemediationCreated={result => dispatchNotification(result.getNotification())}
63+
>
64+
<AnsibeTowerIcon size="sm" color={'var(--pf-global--BackgroundColor--100)'} />
65+
&nbsp;
66+
<FormattedMessage {...messages.remediateLabel} />
67+
</RemediationButton>
68+
</div>
69+
);
70+
};
71+
72+
Remediation.propTypes = {
73+
cves: propTypes.oneOfType([
74+
propTypes.array,
75+
propTypes.string
76+
]),
77+
systems: propTypes.oneOfType([
78+
propTypes.array,
79+
propTypes.string
80+
]),
81+
addNotification: propTypes.func,
82+
manyRules: propTypes.bool
83+
};
10284

10385
export default connect(
10486
null,

0 commit comments

Comments
 (0)