Skip to content

Commit 64c2444

Browse files
committed
FIX Wizard create/update THEN/AND/OR rule
1 parent 36a7531 commit 64c2444

File tree

1 file changed

+62
-73
lines changed

1 file changed

+62
-73
lines changed

src/web/wizard/components/conditions/OrCondition.jsx

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
* <http://www.mongodb.com/licensing/server-side-public-license>.
1616
*/
1717

18-
19-
import PropTypes from 'prop-types';
20-
import React from 'react';
18+
import React, {useEffect, useState} from 'react';
2119
import ObjectUtils from 'util/ObjectUtils';
2220
import { FormattedMessage } from 'react-intl';
2321
import FieldsInput from 'wizard/components/inputs/FieldsInput';
@@ -28,106 +26,97 @@ import SearchQueryInput from 'wizard/components/inputs/SearchQueryInput';
2826
import GroupByInput from 'wizard/components/inputs/GroupByInput';
2927
import { Row, Col } from 'components/bootstrap';
3028
import HighlightedDiv from 'wizard/components/containers/HighlightedDiv';
29+
import TimeHook from './TimeHook';
3130

3231
const STREAM = {
3332
matching_type: '',
3433
field_rule: [{field: '', type: '', value: ''}],
3534
};
3635

37-
class OrCondition extends React.Component {
36+
const OrCondition = ({alert, onUpdate}) => {
37+
38+
const [state, setState] = useState({ alert: ObjectUtils.clone(alert), time: 0, time_type: 0 });
3839

39-
static propTypes = {
40-
onUpdate: PropTypes.func,
41-
}
40+
useEffect(() => {
41+
const { time, time_type } = TimeHook.computeTime(alert);
42+
setState({ alert, time, time_type });
43+
}, []);
4244

43-
state = {
44-
alert: ObjectUtils.clone(this.props.alert),
45-
}
45+
const _handleChangeCondition = (field, value) => {
46+
const update = ObjectUtils.clone(state.alert);
4647

47-
_handleChangeCondition(field, value) {
48-
let update = ObjectUtils.clone(this.state.alert);
4948
if (field === "threshold" || field === "additional_threshold" || field === "time") {
5049
update.condition_parameters[field] = parseInt(value);
5150
} else {
5251
update.condition_parameters[field] = value;
5352
}
54-
this.setState({alert: update});
55-
this.props.onUpdate('condition_parameters', update.condition_parameters);
56-
}
5753

58-
_handleChangeStream(field, value) {
59-
let update = ObjectUtils.clone(this.state.alert);
54+
const { time, time_type } = TimeHook.computeTime(update);
55+
setState({ alert: update, time, time_type });
56+
onUpdate('condition_parameters', update.condition_parameters);
57+
};
58+
59+
const _handleChangeStream = (field, value) => {
60+
const update = ObjectUtils.clone(state.alert);
6061
update.stream[field] = value;
61-
this.setState({alert: update});
62-
this.props.onUpdate('stream', update.stream);
63-
}
6462

65-
_handleChangeSecondStream(field, value) {
66-
let update = ObjectUtils.clone(this.state.alert);
63+
const { time, time_type } = TimeHook.computeTime(update);
64+
setState({ alert: update, time, time_type });
65+
onUpdate('stream', update.stream);
66+
};
67+
68+
const _handleChangeSecondStream = (field, value) => {
69+
const update = ObjectUtils.clone(state.alert);
6770
if(update.second_stream === null){
6871
update.second_stream = STREAM;
6972
}
7073
update.second_stream[field] = value;
71-
this.setState({alert: update});
72-
this.props.onUpdate('second_stream', update.second_stream);
73-
}
7474

75-
_handleChangeAdditionalNbrCond(field, value) {
75+
const { time, time_type } = TimeHook.computeTime(update);
76+
setState({ alert: update, time, time_type });
77+
onUpdate('second_stream', update.second_stream);
78+
};
79+
80+
const _handleChangeAdditionalNbrCond = (field, value) => {
7681
if (field === "threshold") {
77-
this._handleChangeCondition("additional_threshold", value);
82+
_handleChangeCondition("additional_threshold", value);
7883
} else if (field === "threshold_type") {
79-
this._handleChangeCondition("additional_threshold_type", value);
84+
_handleChangeCondition("additional_threshold_type", value);
8085
} else {
81-
this._handleChangeCondition(field, value);
86+
_handleChangeCondition(field, value);
8287
}
83-
}
84-
85-
render() {
86-
let time;
87-
let time_type;
88-
if (this.props.alert.condition_parameters.time >= 1440) {
89-
time = this.props.alert.condition_parameters.time / 1440;
90-
time_type = 1440;
91-
} else if (this.props.alert.condition_parameters.time >= 60) {
92-
time = this.props.alert.condition_parameters.time / 60;
93-
time_type = 60;
94-
} else {
95-
time = this.props.alert.condition_parameters.time;
96-
time_type = 1;
97-
}
98-
99-
return (
100-
<>
101-
<HighlightedDiv>
102-
<SearchQueryInput onUpdate={this._handleChangeCondition} search_query={this.props.alert.condition_parameters.search_query}/>
103-
<br/>
104-
<FieldsInput stream={this.props.alert.stream} onSaveStream={this._handleChangeStream} message={this.props.message}
105-
matchData={this.props.matchData}/>
106-
<br/>
107-
<NumberInput onUpdate={this._handleChangeCondition} threshold={this.state.alert.condition_parameters.threshold}
108-
threshold_type={this.state.alert.condition_parameters.threshold_type}/>
109-
</HighlightedDiv>
110-
<br/>
111-
<Row style={{marginBottom: '0px'}}><Col md={2}/><Col md={10}><label><FormattedMessage id="wizard.or" defaultMessage="OR"/></label></Col></Row>
112-
<br/>
113-
<HighlightedDiv>
114-
<SearchQueryInput onUpdate={this._handleChangeCondition} search_query={this.props.alert.condition_parameters.additional_search_query} fieldName='additional_search_query'/>
115-
<br/>
116-
<FieldsInput stream={this.props.alert.second_stream} onSaveStream={this._handleChangeSecondStream} message={this.props.message}/>
117-
<br/>
118-
<NumberInput onUpdate={this._handleChangeAdditionalNbrCond} threshold={this.state.alert.condition_parameters.additional_threshold}
119-
threshold_type={this.state.alert.condition_parameters.additional_threshold_type}/>
120-
</HighlightedDiv>
88+
};
89+
90+
return (
91+
<>
92+
<HighlightedDiv>
93+
<SearchQueryInput onUpdate={_handleChangeCondition} search_query={state.alert.condition_parameters.search_query}/>
12194
<br/>
122-
<TimeRangeInput onUpdate={this._handleChangeCondition} time={time.toString()} time_type={time_type.toString()}/>
95+
<FieldsInput stream={state.alert.stream} onSaveStream={_handleChangeStream} />
12396
<br/>
124-
<GroupByInput onUpdate={this._handleChangeCondition} grouping_fields={this.props.alert.condition_parameters.grouping_fields}/>
97+
<NumberInput onUpdate={_handleChangeCondition} threshold={state.alert.condition_parameters.threshold}
98+
threshold_type={state.alert.condition_parameters.threshold_type}/>
99+
</HighlightedDiv>
100+
<br/>
101+
<Row style={{marginBottom: '0px'}}><Col md={2}/><Col md={10}><label><FormattedMessage id="wizard.or" defaultMessage="OR"/></label></Col></Row>
102+
<br/>
103+
<HighlightedDiv>
104+
<SearchQueryInput onUpdate={_handleChangeCondition} search_query={state.alert.condition_parameters.additional_search_query} fieldName='additional_search_query'/>
125105
<br/>
126-
<Description onUpdate={this.props.onUpdate} description={this.props.alert.description}/>
106+
<FieldsInput stream={state.alert.second_stream} onSaveStream={_handleChangeSecondStream} />
127107
<br/>
128-
</>
129-
);
130-
}
108+
<NumberInput onUpdate={_handleChangeAdditionalNbrCond} threshold={state.alert.condition_parameters.additional_threshold}
109+
threshold_type={state.alert.condition_parameters.additional_threshold_type}/>
110+
</HighlightedDiv>
111+
<br/>
112+
<TimeRangeInput onUpdate={_handleChangeCondition} time={state.time.toString()} time_type={state.time_type.toString()}/>
113+
<br/>
114+
<GroupByInput onUpdate={_handleChangeCondition} grouping_fields={state.alert.condition_parameters.grouping_fields}/>
115+
<br/>
116+
<Description onUpdate={onUpdate} description={state.alert.description}/>
117+
<br/>
118+
</>
119+
);
131120
}
132121

133122
export default OrCondition;

0 commit comments

Comments
 (0)