Skip to content

Commit

Permalink
show success toast when detector is updated (opensearch-project#224) (o…
Browse files Browse the repository at this point in the history
…pensearch-project#235)

Signed-off-by: Amardeepsingh Siglani <[email protected]>

Signed-off-by: Amardeepsingh Siglani <[email protected]>
(cherry picked from commit 8d835ce)

Co-authored-by: Amardeepsingh Siglani <[email protected]>
Signed-off-by: AWSHurneyt <[email protected]>
  • Loading branch information
2 people authored and AWSHurneyt committed Oct 12, 2023
1 parent d14a67e commit d142dfa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
14 changes: 8 additions & 6 deletions public/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export interface BrowserServices {
notificationsService: NotificationsService;
}

export interface RuleOptions {
name: string;
id: string;
severity: string;
tags: string[];
}

export interface RulesSharedState {
page: RulesPage;
rulesOptions: {
name: string;
id: string;
severity: string;
tags: string[];
}[];
rulesOptions: RuleOptions[];
}

export interface RulesPage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React, { Component } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { EuiButton, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { DetectorHit } from '../../../../../server/models/interfaces';
import { DetectorHit, RuleSource } from '../../../../../server/models/interfaces';
import { Detector } from '../../../../../models/interfaces';
import ConfigureAlerts from '../../../CreateDetector/components/ConfigureAlerts';
import {
Expand All @@ -15,10 +15,14 @@ import {
RuleService,
OpenSearchService,
} from '../../../../services';
import { RulesSharedState } from '../../../../models/interfaces';
import { RuleOptions } from '../../../../models/interfaces';
import { ROUTES, OS_NOTIFICATION_PLUGIN } from '../../../../utils/constants';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { errorNotificationToast, getPlugins } from '../../../../utils/helpers';
import {
errorNotificationToast,
getPlugins,
successNotificationToast,
} from '../../../../utils/helpers';

export interface UpdateAlertConditionsProps
extends RouteComponentProps<any, any, { detectorHit: DetectorHit }> {
Expand All @@ -32,7 +36,7 @@ export interface UpdateAlertConditionsProps
export interface UpdateAlertConditionsState {
detector: Detector;
rules: object;
rulesOptions: Pick<RulesSharedState, 'rulesOptions'>['rulesOptions'];
rulesOptions: RuleOptions[];
submitting: boolean;
plugins: string[];
}
Expand Down Expand Up @@ -83,8 +87,8 @@ export default class UpdateAlertConditions extends Component<
const prePackagedResponse = await ruleService.getRules(true, body);
const customResponse = await ruleService.getRules(false, body);

const allRules = {};
const rulesOptions = new Set();
const allRules: { [id: string]: RuleSource } = {};
const rulesOptions = new Set<RuleOptions>();

if (prePackagedResponse.ok) {
prePackagedResponse.response.hits.hits.forEach((hit) => {
Expand Down Expand Up @@ -127,7 +131,7 @@ export default class UpdateAlertConditions extends Component<
}

this.setState({ rules: allRules, rulesOptions: Array.from(rulesOptions) });
} catch (e) {
} catch (e: any) {
errorNotificationToast(this.props.notifications, 'retrieve', 'rules', e);
}
};
Expand Down Expand Up @@ -169,8 +173,10 @@ export default class UpdateAlertConditions extends Component<
'detector',
updateDetectorResponse.error
);
} else {
successNotificationToast(this.props.notifications, 'updated', 'detector');
}
} catch (e) {
} catch (e: any) {
errorNotificationToast(this.props.notifications, 'update', 'detector', e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DetectorHit, SearchDetectorsResponse } from '../../../../../server/mode
import { EMPTY_DEFAULT_DETECTOR, ROUTES } from '../../../../utils/constants';
import { ServerResponse } from '../../../../../server/models/types';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { errorNotificationToast } from '../../../../utils/helpers';
import { errorNotificationToast, successNotificationToast } from '../../../../utils/helpers';

export interface UpdateDetectorBasicDetailsProps
extends RouteComponentProps<any, any, { detectorHit: DetectorHit }> {
Expand Down Expand Up @@ -168,23 +168,18 @@ export const UpdateDetectorBasicDetails: React.FC<UpdateDetectorBasicDetailsProp
);

if (updateDetectorRes?.ok) {
props.history.replace({
pathname: `${ROUTES.DETECTOR_DETAILS}/${detectorId}`,
state: {
detectorHit: { ...detectorHit, _source: { ...detectorHit._source, ...detector } },
},
});
successNotificationToast(props.notifications, 'updated', 'detector');
} else {
errorNotificationToast(props.notifications, 'update', 'detector', updateDetectorRes?.error);
}

setSubmitting(false);
props.history.replace({
pathname: `${ROUTES.DETECTOR_DETAILS}/${detectorId}`,
state: {
detectorHit: { ...detectorHit, _source: { ...detectorHit._source, ...detector } },
},
});
setSubmitting(false);
};

updateDetector().catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { EMPTY_DEFAULT_DETECTOR, ROUTES } from '../../../../utils/constants';
import { DetectorsService } from '../../../../services';
import { ServerResponse } from '../../../../../server/models/types';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { errorNotificationToast } from '../../../../utils/helpers';
import { errorNotificationToast, successNotificationToast } from '../../../../utils/helpers';

export interface UpdateFieldMappingsProps
extends RouteComponentProps<any, any, { detectorHit: DetectorHit }> {
Expand Down Expand Up @@ -128,6 +128,8 @@ export default class UpdateFieldMappings extends Component<
'detector',
updateDetectorResponse.error
);
} else {
successNotificationToast(this.props.notifications, 'updated', 'detector');
}
} catch (error: any) {
errorNotificationToast(this.props.notifications, 'update', 'detector', error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { EMPTY_DEFAULT_DETECTOR, ROUTES } from '../../../../utils/constants';
import { ServicesContext } from '../../../../services';
import { ServerResponse } from '../../../../../server/models/types';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { errorNotificationToast } from '../../../../utils/helpers';
import { errorNotificationToast, successNotificationToast } from '../../../../utils/helpers';

export interface UpdateDetectorRulesProps
extends RouteComponentProps<
Expand Down Expand Up @@ -191,6 +191,8 @@ export const UpdateDetectorRules: React.FC<UpdateDetectorRulesProps> = (props) =

if (!updateDetectorRes.ok) {
errorNotificationToast(props.notifications, 'update', 'detector', updateDetectorRes.error);
} else {
successNotificationToast(props.notifications, 'updated', 'detector');
}

props.history.replace({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
EuiSpacer,
EuiTab,
EuiTabs,
EuiText,
EuiTitle,
EuiHealth,
} from '@elastic/eui';
Expand Down

0 comments on commit d142dfa

Please sign in to comment.