From ec479b8d8868d6ab20a227ee584113c1a37f0ad5 Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Tue, 18 Jul 2023 10:06:47 +0200 Subject: [PATCH] Alerting: Save and restore condition reference while switching type (#71629) Save and restore condition reference while switching type --- .../QueryAndExpressionsStep.tsx | 19 ++++++++++++++++--- 1 file changed, 16 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 1118e04c4fb..3fb8af2ae56 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 @@ -303,8 +303,9 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P [dispatch] ); - // we need to keep track of the previous expressions to be able to restore them when switching back to grafana managed + // we need to keep track of the previous expressions and condition reference to be able to restore them when switching back to grafana managed const [prevExpressions, setPrevExpressions] = useState([]); + const [prevCondition, setPrevCondition] = useState(null); const restoreExpressionsInQueries = useCallback(() => { addExpressionsInQueries(prevExpressions); @@ -314,14 +315,26 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P const typeInForm = getValues('type'); if (typeInForm === RuleFormType.cloudAlerting) { setValue('type', RuleFormType.grafana); - setPrevExpressions.length > 0 && restoreExpressionsInQueries(); + prevExpressions.length > 0 && restoreExpressionsInQueries(); + prevCondition && setValue('condition', prevCondition); } else { setValue('type', RuleFormType.cloudAlerting); const expressions = queries.filter((query) => query.datasourceUid === ExpressionDatasourceUID); setPrevExpressions(expressions); removeExpressionsInQueries(); + setPrevCondition(condition); } - }, [getValues, setValue, queries, removeExpressionsInQueries, restoreExpressionsInQueries, setPrevExpressions]); + }, [ + getValues, + setValue, + queries, + removeExpressionsInQueries, + restoreExpressionsInQueries, + setPrevExpressions, + prevExpressions, + prevCondition, + condition, + ]); return (