Skip to content

Commit

Permalink
Added consumer and alertTypeId with enable change alert type button p…
Browse files Browse the repository at this point in the history
…rops
  • Loading branch information
YulNaumenko committed Feb 6, 2020
1 parent 1ad46d5 commit 2bd5bef
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
EuiFormRow,
EuiCallOut,
} from '@elastic/eui';
import { HttpSetup } from 'kibana/public';
import { HttpSetup, IUiSettingsClient, ToastsApi } from 'kibana/public';
import { COMPARATORS, builtInComparators } from '../../../../common/constants';
import {
getMatchingIndicesForThresholdAlertType,
Expand Down Expand Up @@ -68,6 +68,11 @@ interface IndexThresholdProps {
setAlertProperty: (key: string, value: any) => void;
errors: { [key: string]: string[] };
http: HttpSetup;
uiSettings?: IUiSettingsClient;
toastNotifications?: Pick<
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
}

export const IndexThresholdAlertTypeExpression: React.FunctionComponent<IndexThresholdProps> = ({
Expand All @@ -76,6 +81,8 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<IndexThr
setAlertProperty,
errors,
http,
uiSettings,
toastNotifications,
}) => {
const {
index,
Expand Down Expand Up @@ -433,12 +440,15 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<IndexThr
/>
</EuiFlexItem>
</EuiFlexGroup>
{canShowVizualization ? null : (
{canShowVizualization || !uiSettings ? null : (
<Fragment>
<ThresholdVisualization
alertParams={alertParams}
aggregationTypes={builtInAggregationTypes}
comparators={builtInComparators}
http={http}
toastNotifications={toastNotifications}
uiSettings={uiSettings}
/>
</Fragment>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, { Fragment, useEffect, useState } from 'react';
import { IUiSettingsClient } from 'kibana/public';
import { IUiSettingsClient, HttpSetup, ToastsApi } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import {
AnnotationDomainTypes,
Expand All @@ -26,7 +26,6 @@ import { EuiCallOut, EuiLoadingChart, EuiSpacer, EuiEmptyPrompt, EuiText } from
import { FormattedMessage } from '@kbn/i18n/react';
import { npStart } from 'ui/new_platform';
import { getThresholdAlertVisualizationData } from './lib/api';
import { useAppDependencies } from '../../../app_context';
import { AggregationType, Comparator } from '../../../../common/types';
import { IndexThresholdAlertParams } from '../types';

Expand Down Expand Up @@ -89,14 +88,22 @@ interface Props {
comparators: {
[key: string]: Comparator;
};
http: HttpSetup;
uiSettings: IUiSettingsClient;
toastNotifications?: Pick<
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
}

export const ThresholdVisualization: React.FunctionComponent<Props> = ({
alertParams,
aggregationTypes,
comparators,
http,
uiSettings,
toastNotifications,
}) => {
const { http, uiSettings, toastNotifications } = useAppDependencies();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<undefined | any>(undefined);
const [visualizationData, setVisualizationData] = useState<Record<string, any>>([]);
Expand Down Expand Up @@ -142,12 +149,14 @@ export const ThresholdVisualization: React.FunctionComponent<Props> = ({
})
);
} catch (e) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertAdd.unableToLoadVisualizationMessage',
{ defaultMessage: 'Unable to load visualization' }
),
});
if (toastNotifications) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertAdd.unableToLoadVisualizationMessage',
{ defaultMessage: 'Unable to load visualization' }
),
});
}
setError(e);
} finally {
setIsLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ interface ConnectorAddModalProps {
setAddModalVisibility: React.Dispatch<React.SetStateAction<boolean>>;
postSaveEventHandler?: (savedAction: ActionConnector) => void;
http: HttpSetup;
toastNotifications: Pick<
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
toastNotifications?: Pick<
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
}

export const ConnectorAddModal = ({
Expand Down Expand Up @@ -88,17 +88,19 @@ export const ConnectorAddModal = ({
const onActionConnectorSave = async (): Promise<ActionConnector | undefined> =>
await createActionConnector({ http, connector })
.then(savedConnector => {
toastNotifications.addSuccess(
i18n.translate(
'xpack.triggersActionsUI.sections.addModalConnectorForm.updateSuccessNotificationText',
{
defaultMessage: "Created '{connectorName}'",
values: {
connectorName: savedConnector.name,
},
}
)
);
if (toastNotifications) {
toastNotifications.addSuccess(
i18n.translate(
'xpack.triggersActionsUI.sections.addModalConnectorForm.updateSuccessNotificationText',
{
defaultMessage: "Created '{connectorName}'",
values: {
connectorName: savedConnector.name,
},
}
)
);
}
return savedConnector;
})
.catch(errorRes => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ describe('alert_add', () => {
}}
>
<AlertAdd
consumer={'alerting'}
http={deps.http}
actionTypeRegistry={deps.actionTypeRegistry}
alertTypeRegistry={deps.alertTypeRegistry}
toastNotifications={deps.toastNotifications}
uiSettings={deps.uiSettings}
/>
</AlertsContextProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
EuiPortal,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { HttpSetup, ToastsApi, IUiSettingsClient } from 'kibana/public';
import { useAlertsContext } from '../../context/alerts_context';
import { Alert, AlertAction, IErrorObject, AlertTypeModel, ActionTypeModel } from '../../../types';
import { AlertForm, validateBaseProperties } from './alert_form';
Expand All @@ -27,25 +27,33 @@ import { createAlert } from '../../lib/alert_api';
import { TypeRegistry } from '../../type_registry';

interface AlertAddProps {
consumer: string;
http: HttpSetup;
toastNotifications: Pick<
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
uiSettings?: IUiSettingsClient;
toastNotifications?: Pick<
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
alertTypeId?: string;
canChangeTrigger?: boolean;
}

export const AlertAdd = ({
consumer,
http,
toastNotifications,
alertTypeRegistry,
actionTypeRegistry,
uiSettings,
canChangeTrigger,
alertTypeId,
}: AlertAddProps) => {
const initialAlert = ({
params: {},
consumer: 'alerting',
alertTypeId: null,
consumer,
alertTypeId,
schedule: {
interval: '1m',
},
Expand Down Expand Up @@ -106,14 +114,16 @@ export const AlertAdd = ({
async function onSaveAlert(): Promise<Alert | undefined> {
try {
const newAlert = await createAlert({ http, alert });
toastNotifications.addSuccess(
i18n.translate('xpack.triggersActionsUI.sections.alertForm.saveSuccessNotificationText', {
defaultMessage: "Saved '{alertName}'",
values: {
alertName: newAlert.name,
},
})
);
if (toastNotifications) {
toastNotifications.addSuccess(
i18n.translate('xpack.triggersActionsUI.sections.alertForm.saveSuccessNotificationText', {
defaultMessage: "Saved '{alertName}'",
values: {
alertName: newAlert.name,
},
})
);
}
return newAlert;
} catch (errorRes) {
setServerError(errorRes);
Expand Down Expand Up @@ -146,10 +156,12 @@ export const AlertAdd = ({
actionTypeRegistry={actionTypeRegistry}
alertTypeRegistry={alertTypeRegistry}
toastNotifications={toastNotifications}
uiSettings={uiSettings}
alert={alert}
dispatch={dispatch}
errors={errors}
serverError={serverError}
canChangeTrigger={canChangeTrigger}
/>
</EuiFlyoutBody>
<EuiFlyoutFooter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('alert_form', () => {
actionTypeRegistry={deps.actionTypeRegistry}
alertTypeRegistry={deps.alertTypeRegistry}
toastNotifications={deps.toastNotifications}
uiSettings={deps.uiSettings}
/>
);
}
Expand Down Expand Up @@ -172,6 +173,7 @@ describe('alert_form', () => {
actionTypeRegistry={deps.actionTypeRegistry}
alertTypeRegistry={deps.alertTypeRegistry}
toastNotifications={deps.toastNotifications}
uiSettings={deps.uiSettings}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
EuiEmptyPrompt,
EuiButtonEmpty,
} from '@elastic/eui';
import { HttpSetup, ToastsApi } from 'kibana/public';
import { HttpSetup, ToastsApi, IUiSettingsClient } from 'kibana/public';
import { loadAlertTypes } from '../../lib/alert_api';
import { loadActionTypes, loadAllActions } from '../../lib/action_connector_api';
import { AlertReducerAction } from './alert_reducer';
Expand Down Expand Up @@ -82,19 +82,20 @@ export function validateBaseProperties(alertObject: Alert) {

interface AlertFormProps {
alert: Alert;
canChangeTrigger?: boolean; // to hide Change trigger button
dispatch: React.Dispatch<AlertReducerAction>;
errors: IErrorObject;
serverError: {
body: { message: string; error: string };
} | null;
http: HttpSetup;
toastNotifications: Pick<
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
uiSettings?: IUiSettingsClient;
toastNotifications?: Pick<
ToastsApi,
'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError'
>;
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
actionTypeRegistry: TypeRegistry<ActionTypeModel>;
canChangeTrigger?: boolean; // to hide Change trigger button
}

interface ActiveActionConnectorState {
Expand All @@ -112,6 +113,7 @@ export const AlertForm = ({
toastNotifications,
alertTypeRegistry,
actionTypeRegistry,
uiSettings,
}: AlertFormProps) => {
const [alertTypeModel, setAlertTypeModel] = useState<AlertTypeModel | null>(
alertTypeRegistry.get(alert.alertTypeId)
Expand Down Expand Up @@ -144,12 +146,14 @@ export const AlertForm = ({
}
setActionTypesIndex(index);
} catch (e) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadActionTypesMessage',
{ defaultMessage: 'Unable to load action types' }
),
});
if (toastNotifications) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadActionTypesMessage',
{ defaultMessage: 'Unable to load action types' }
),
});
}
} finally {
setIsLoadingActionTypes(false);
}
Expand All @@ -175,12 +179,14 @@ export const AlertForm = ({
}
setAlertTypesIndex(index);
} catch (e) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadAlertTypesMessage',
{ defaultMessage: 'Unable to load alert types' }
),
});
if (toastNotifications) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadAlertTypesMessage',
{ defaultMessage: 'Unable to load alert types' }
),
});
}
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -218,14 +224,16 @@ export const AlertForm = ({
const actionsResponse = await loadAllActions({ http });
setConnectors(actionsResponse.data);
} catch (e) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadActionsMessage',
{
defaultMessage: 'Unable to load connectors',
}
),
});
if (toastNotifications) {
toastNotifications.addDanger({
title: i18n.translate(
'xpack.triggersActionsUI.sections.alertForm.unableToLoadActionsMessage',
{
defaultMessage: 'Unable to load connectors',
}
),
});
}
}
}

Expand Down Expand Up @@ -624,6 +632,9 @@ export const AlertForm = ({
errors={errors}
setAlertParams={setAlertParams}
setAlertProperty={setAlertProperty}
http={http}
toastNotifications={toastNotifications}
uiSettings={uiSettings}
/>
) : null}
<EuiSpacer size="xl" />
Expand Down
Loading

0 comments on commit 2bd5bef

Please sign in to comment.