From 51f5e1af39c7adb20adf32656e4381f4e4541e2b Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Tue, 23 Jan 2024 09:23:59 +0100 Subject: [PATCH] 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 --- .../query-and-alert-condition/QueryAndExpressionsStep.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx index 9360b934ad7..0aff0232535 100644 --- a/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/query-and-alert-condition/QueryAndExpressionsStep.tsx @@ -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(