diff --git a/frontend/src/constants/query.ts b/frontend/src/constants/query.ts index 3759fb26b6..9b731ca089 100644 --- a/frontend/src/constants/query.ts +++ b/frontend/src/constants/query.ts @@ -31,4 +31,5 @@ export enum QueryParams { pagination = 'pagination', relativeTime = 'relativeTime', alertType = 'alertType', + ruleId = 'ruleId', } diff --git a/frontend/src/container/FormAlertRules/index.tsx b/frontend/src/container/FormAlertRules/index.tsx index 24edb957aa..3514f6c7ea 100644 --- a/frontend/src/container/FormAlertRules/index.tsx +++ b/frontend/src/container/FormAlertRules/index.tsx @@ -157,8 +157,12 @@ function FormAlertRules({ }, [alertDef, currentQuery?.queryType, queryOptions]); const onCancelHandler = useCallback(() => { - history.replace(ROUTES.LIST_ALL_ALERT); - }, []); + urlQuery.delete(QueryParams.compositeQuery); + urlQuery.delete(QueryParams.panelTypes); + urlQuery.delete(QueryParams.ruleId); + urlQuery.delete(QueryParams.relativeTime); + history.replace(`${ROUTES.LIST_ALL_ALERT}?${urlQuery.toString()}`); + }, [urlQuery]); // onQueryCategoryChange handles changes to query category // in state as well as sets additional defaults @@ -343,8 +347,13 @@ function FormAlertRules({ // invalidate rule in cache ruleCache.invalidateQueries(['ruleId', ruleId]); + // eslint-disable-next-line sonarjs/no-identical-functions setTimeout(() => { - history.replace(ROUTES.LIST_ALL_ALERT); + urlQuery.delete(QueryParams.compositeQuery); + urlQuery.delete(QueryParams.panelTypes); + urlQuery.delete(QueryParams.ruleId); + urlQuery.delete(QueryParams.relativeTime); + history.replace(`${ROUTES.LIST_ALL_ALERT}?${urlQuery.toString()}`); }, 2000); } else { notifications.error({ @@ -360,12 +369,13 @@ function FormAlertRules({ } setLoading(false); }, [ - t, isFormValid, - ruleId, - ruleCache, memoizedPreparePostData, + ruleId, notifications, + t, + ruleCache, + urlQuery, ]); const onSaveHandler = useCallback(async () => { diff --git a/frontend/src/container/ListAlertRules/ListAlert.tsx b/frontend/src/container/ListAlertRules/ListAlert.tsx index 39465e28a8..37a56ba921 100644 --- a/frontend/src/container/ListAlertRules/ListAlert.tsx +++ b/frontend/src/container/ListAlertRules/ListAlert.tsx @@ -117,14 +117,19 @@ function ListAlert({ allAlertRules, refetch }: ListAlertProps): JSX.Element { .refetch() .then(() => { const compositeQuery = mapQueryDataFromApi(record.condition.compositeQuery); - - history.push( - `${ROUTES.EDIT_ALERTS}?ruleId=${record.id.toString()}&${ - QueryParams.compositeQuery - }=${encodeURIComponent(JSON.stringify(compositeQuery))}&panelTypes=${ - record.condition.compositeQuery.panelType - }`, + params.set( + QueryParams.compositeQuery, + encodeURIComponent(JSON.stringify(compositeQuery)), ); + + params.set( + QueryParams.panelTypes, + record.condition.compositeQuery.panelType, + ); + + params.set(QueryParams.ruleId, record.id.toString()); + + history.push(`${ROUTES.EDIT_ALERTS}?${params.toString()}`); }) .catch(handleError); }; @@ -151,7 +156,8 @@ function ListAlert({ allAlertRules, refetch }: ListAlertProps): JSX.Element { setData(refetchData.payload || []); setTimeout(() => { const clonedAlert = refetchData.payload[refetchData.payload.length - 1]; - history.push(`${ROUTES.EDIT_ALERTS}?ruleId=${clonedAlert.id}`); + params.set(QueryParams.ruleId, String(clonedAlert.id)); + history.push(`${ROUTES.EDIT_ALERTS}?${params.toString()}`); }, 2000); } if (status === 'error') {