Alerting: Add delete to rule edit (#43673)

This commit is contained in:
Peter Holmberg 2022-01-11 09:46:21 +01:00 committed by GitHub
parent 3ca3b7f432
commit fc037e1798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import React, { FC, useMemo } from 'react'; import React, { FC, useMemo, useState } from 'react';
import { GrafanaTheme2, AppEvents } from '@grafana/data'; import { GrafanaTheme2, AppEvents } from '@grafana/data';
import { PageToolbar, Button, useStyles2, CustomScrollbar, Spinner } from '@grafana/ui'; import { PageToolbar, Button, useStyles2, CustomScrollbar, Spinner, ConfirmModal } from '@grafana/ui';
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { AlertTypeStep } from './AlertTypeStep'; import { AlertTypeStep } from './AlertTypeStep';
@ -11,7 +11,7 @@ import { useForm, FormProvider } from 'react-hook-form';
import { RuleFormType, RuleFormValues } from '../../types/rule-form'; import { RuleFormType, RuleFormValues } from '../../types/rule-form';
import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector'; import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector';
import { initialAsyncRequestState } from '../../utils/redux'; import { initialAsyncRequestState } from '../../utils/redux';
import { saveRuleFormAction } from '../../state/actions'; import { deleteRuleAction, saveRuleFormAction } from '../../state/actions';
import { RuleWithLocation } from 'app/types/unified-alerting'; import { RuleWithLocation } from 'app/types/unified-alerting';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { useCleanup } from 'app/core/hooks/useCleanup'; import { useCleanup } from 'app/core/hooks/useCleanup';
@ -22,6 +22,7 @@ import { useQueryParams } from 'app/core/hooks/useQueryParams';
import { appEvents } from 'app/core/core'; import { appEvents } from 'app/core/core';
import { CloudConditionsStep } from './CloudConditionsStep'; import { CloudConditionsStep } from './CloudConditionsStep';
import { GrafanaConditionsStep } from './GrafanaConditionsStep'; import { GrafanaConditionsStep } from './GrafanaConditionsStep';
import * as ruleId from '../../utils/rule-id';
type Props = { type Props = {
existing?: RuleWithLocation; existing?: RuleWithLocation;
@ -33,6 +34,7 @@ export const AlertRuleForm: FC<Props> = ({ existing }) => {
const [queryParams] = useQueryParams(); const [queryParams] = useQueryParams();
const returnTo: string = (queryParams['returnTo'] as string | undefined) ?? '/alerting/list'; const returnTo: string = (queryParams['returnTo'] as string | undefined) ?? '/alerting/list';
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false);
const defaultValues: RuleFormValues = useMemo(() => { const defaultValues: RuleFormValues = useMemo(() => {
if (existing) { if (existing) {
@ -82,6 +84,19 @@ export const AlertRuleForm: FC<Props> = ({ existing }) => {
); );
}; };
const deleteRule = () => {
if (existing) {
const identifier = ruleId.fromRulerRule(
existing.ruleSourceName,
existing.namespace,
existing.group.name,
existing.rule
);
dispatch(deleteRuleAction(identifier, { navigateTo: '/alerting/list' }));
}
};
const onInvalid = () => { const onInvalid = () => {
appEvents.emit(AppEvents.alertError, ['There are errors in the form. Please correct them and try again!']); appEvents.emit(AppEvents.alertError, ['There are errors in the form. Please correct them and try again!']);
}; };
@ -95,6 +110,11 @@ export const AlertRuleForm: FC<Props> = ({ existing }) => {
Cancel Cancel
</Button> </Button>
</Link> </Link>
{existing ? (
<Button variant="destructive" type="button" onClick={() => setShowDeleteModal(true)}>
Delete
</Button>
) : null}
<Button <Button
variant="primary" variant="primary"
type="button" type="button"
@ -129,6 +149,17 @@ export const AlertRuleForm: FC<Props> = ({ existing }) => {
</CustomScrollbar> </CustomScrollbar>
</div> </div>
</form> </form>
{showDeleteModal ? (
<ConfirmModal
isOpen={true}
title="Delete rule"
body="Deleting this rule will permanently remove it. Are you sure you want to delete this rule?"
confirmText="Yes, delete"
icon="exclamation-triangle"
onConfirm={deleteRule}
onDismiss={() => setShowDeleteModal(false)}
/>
) : null}
</FormProvider> </FormProvider>
); );
}; };