diff --git a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx index bd480ca8f74..dbbbbd686f7 100644 --- a/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/QueryEditor.tsx @@ -34,11 +34,13 @@ interface State { } export class QueryEditor extends PureComponent { private runner: AlertingQueryRunner; + private queries: GrafanaQuery[]; constructor(props: Props) { super(props); this.state = { panelDataByRefId: {} }; this.runner = new AlertingQueryRunner(); + this.queries = props.value ?? []; } componentDidMount() { @@ -52,29 +54,34 @@ export class QueryEditor extends PureComponent { } onRunQueries = () => { - const { value = [] } = this.props; - this.runner.run(value); + const { queries } = this; + this.runner.run(queries); }; onCancelQueries = () => { this.runner.cancel(); }; + onChangeQueries = (queries: GrafanaQuery[]) => { + this.queries = queries; + this.props.onChange(queries); + }; + onDuplicateQuery = (query: GrafanaQuery) => { - const { onChange, value = [] } = this.props; - onChange(addQuery(value, query)); + const { queries } = this; + this.onChangeQueries(addQuery(queries, query)); }; onNewAlertingQuery = () => { - const { onChange, value = [] } = this.props; + const { queries } = this; const defaultDataSource = getDatasourceSrv().getInstanceSettings('default'); if (!defaultDataSource) { return; } - onChange( - addQuery(value, { + this.onChangeQueries( + addQuery(queries, { datasourceUid: defaultDataSource.uid, model: { refId: '', @@ -85,10 +92,10 @@ export class QueryEditor extends PureComponent { }; onNewExpressionQuery = () => { - const { onChange, value = [] } = this.props; + const { queries } = this; - onChange( - addQuery(value, { + this.onChangeQueries( + addQuery(queries, { datasourceUid: ExpressionDatasourceUID, model: expressionDatasource.newQuery({ type: ExpressionQueryType.classic, @@ -166,7 +173,7 @@ export class QueryEditor extends PureComponent {