Skip to content

Commit

Permalink
Moved handleScannerChange out of ScannerSelect, refactored ScannerSel…
Browse files Browse the repository at this point in the history
…ect into a function, and added some update logic.
  • Loading branch information
saberlynx committed Oct 9, 2019
1 parent 17fd0bf commit b62aed6
Showing 1 changed file with 78 additions and 66 deletions.
144 changes: 78 additions & 66 deletions gsa/src/web/pages/tasks/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import React from 'react';
import React, {useState} from 'react';

import _ from 'gmp/locale';

Expand Down Expand Up @@ -105,64 +105,21 @@ const get_scanner = (scanners, scanner_id) => {
});
};

class ScannerSelect extends React.Component {
constructor(...args) {
super(...args);
const ScannerSelect = props => {
const {changeTask, scannerId, scanners, onChange} = props;

this.handleScannerChange = this.handleScannerChange.bind(this);
}

handleScannerChange(value, name) {
const {
scanners,
scanConfigs,
onScanConfigChange,
onScannerChange,
} = this.props;
let config_id;

const scanner = get_scanner(scanners, value);
const scanner_type = isDefined(scanner) ? scanner.scannerType : undefined;

if (
scanner_type === OPENVAS_SCANNER_TYPE ||
scanner_type === GMP_SCANNER_TYPE
) {
config_id = selectSaveId(
scanConfigs[OPENVAS_SCAN_CONFIG_TYPE],
FULL_AND_FAST_SCAN_CONFIG_ID,
);
} else if (scanner_type === OSP_SCANNER_TYPE) {
config_id = selectSaveId(scanConfigs[OSP_SCAN_CONFIG_TYPE], UNSET_VALUE);
} else {
config_id = UNSET_VALUE;
}

log.debug('on scanner change', value, config_id, scanner);

if (isDefined(onScannerChange)) {
onScannerChange(value);
}
if (isDefined(onScanConfigChange)) {
onScanConfigChange(config_id);
}
}

render() {
const {changeTask, scannerId, scanners} = this.props;
return (
<FormGroup title={_('Scanner')}>
<Select
name="scanner_id"
value={scannerId}
disabled={!changeTask}
items={renderSelectItems(scanners)}
onChange={this.handleScannerChange}
/>
</FormGroup>
);
}
}
return (
<FormGroup title={_('Scanner')}>
<Select
name="scanner_id"
value={scannerId}
disabled={!changeTask}
items={renderSelectItems(scanners)}
onChange={onChange}
/>
</FormGroup>
);
};

ScannerSelect.propTypes = {
changeTask: PropTypes.bool.isRequired,
Expand All @@ -172,8 +129,7 @@ ScannerSelect.propTypes = {
}),
scannerId: PropTypes.id.isRequired,
scanners: PropTypes.array.isRequired,
onScanConfigChange: PropTypes.func.isRequired,
onScannerChange: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
};

const TaskDialog = ({
Expand All @@ -186,7 +142,7 @@ const TaskDialog = ({
auto_delete_data = AUTO_DELETE_KEEP_DEFAULT_VALUE,
capabilities,
comment = '',
config_id = FULL_AND_FAST_SCAN_CONFIG_ID,
config_id,
hosts_ordering = HOSTS_ORDERING_SEQUENTIAL,
in_assets = YES_VALUE,
max_checks = DEFAULT_MAX_CHECKS,
Expand Down Expand Up @@ -225,6 +181,57 @@ const TaskDialog = ({
const scanner = get_scanner(scanners, scanner_id);
const scanner_type = isDefined(scanner) ? scanner.scannerType : undefined;

const [configType, setConfigType] = useState('openvas');
const [prevConfigType, setPrevConfigType] = useState('openvas');

// eslint-disable-next-line no-shadow
const handleScannerChange = (value, name) => {
// eslint-disable-next-line no-shadow
const scanner = get_scanner(scanners, value);
// eslint-disable-next-line no-shadow
const scanner_type = isDefined(scanner) ? scanner.scannerType : undefined;

if (
scanner_type === OPENVAS_SCANNER_TYPE ||
scanner_type === OSP_SENSOR_SCANNER_TYPE ||
scanner_type === GMP_SCANNER_TYPE
) {
setConfigType('openvas');
} else if (scanner_type === OSP_SCANNER_TYPE) {
setConfigType('osp');
} else {
setConfigType('other');
}

log.debug('on scanner change', value, scanner);

if (isDefined(onScannerChange)) {
onScannerChange(value);
}

if (configType !== prevConfigType && isDefined(onScanConfigChange)) {
if (
scanner_type === OPENVAS_SCANNER_TYPE ||
scanner_type === GMP_SCANNER_TYPE ||
scanner_type === OSP_SENSOR_SCANNER_TYPE
) {
onScanConfigChange(
selectSaveId(
sorted_scan_configs[OPENVAS_SCAN_CONFIG_TYPE],
FULL_AND_FAST_SCAN_CONFIG_ID,
),
);
} else if (scanner_type === OSP_SCANNER_TYPE) {
onScanConfigChange(
selectSaveId(sorted_scan_configs[OSP_SCAN_CONFIG_TYPE], UNSET_VALUE),
);
} else {
onScanConfigChange(UNSET_VALUE);
}
}
setPrevConfigType(configType);
};

const tag_items = renderSelectItems(tags);

const target_items = renderSelectItems(targets);
Expand Down Expand Up @@ -260,6 +267,7 @@ const TaskDialog = ({
auto_delete,
auto_delete_data,
comment,
config_id,
hosts_ordering,
in_assets,
max_checks,
Expand Down Expand Up @@ -464,12 +472,10 @@ const TaskDialog = ({
}
>
<ScannerSelect
scanConfigs={sorted_scan_configs}
scanners={scanners}
scannerId={state.scanner_id}
changeTask={change_task}
onScanConfigChange={onScanConfigChange}
onScannerChange={onScannerChange}
onChange={handleScannerChange}
/>
</div>
{use_openvas_scan_config && (
Expand All @@ -489,7 +495,10 @@ const TaskDialog = ({
disabled={!change_task}
items={openvas_scan_config_items}
value={openvas_config_id}
onChange={onScanConfigChange}
onChange={value => {
onScanConfigChange(value);
setPrevConfigType(configType);
}}
/>
</div>
</FormGroup>
Expand Down Expand Up @@ -557,7 +566,10 @@ const TaskDialog = ({
name="config_id"
items={osp_scan_config_items}
value={osp_config_id}
onChange={onScanConfigChange}
onChange={value => {
onScanConfigChange(value);
setPrevConfigType(configType);
}}
/>
</FormGroup>
)}
Expand Down

0 comments on commit b62aed6

Please sign in to comment.