[v10.3.x] Alerting: Fix preview getting the correct queries from the form (#81481)

Alerting: Fix preview getting the correct queries from the form (#80458)

* fix preview getting the correct queries from the form

* Remove setting queries in onChangeQueries handler as it only contains data queries and not expressions

* Keep setValue('queries') but also adding expressions

(cherry picked from commit 51f5e1af39)

Co-authored-by: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-03-14 12:50:06 +01:00
committed by GitHub
co-authored by Sonia Aguilar
parent a5dc97b4fe
commit 5fe20a365d
@@ -186,8 +186,9 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
// Invocation cycle => onChange -> dispatch(setDataQueries) -> onRunQueries -> setDataQueries Reducer
// As a workaround we update form values as soon as possible to avoid stale state
// This way we can access up to date queries in runQueriesPreview without waiting for re-render
setValue('queries', updatedQueries, { shouldValidate: false });
const previousQueries = getValues('queries');
const expressionQueries = previousQueries.filter((query) => isExpressionQuery(query.model));
setValue('queries', [...updatedQueries, ...expressionQueries], { shouldValidate: false });
updateExpressionAndDatasource(updatedQueries);
dispatch(setDataQueries(updatedQueries));
@@ -199,7 +200,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P
dispatch(rewireExpressions({ oldRefId, newRefId }));
}
},
[queries, setValue, updateExpressionAndDatasource]
[queries, updateExpressionAndDatasource, getValues, setValue]
);
const onChangeRecordingRulesQueries = useCallback(