Skip to content

Commit

Permalink
use notification service from core
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Oct 30, 2019
1 parent e55805a commit 04e3001
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { EuiConfirmModal, EuiOverlayMask, EuiCallOut, EuiCheckbox, EuiBadge } fr
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import React, { Fragment, useState } from 'react';
import { toastNotifications } from 'ui/notify';
import { deleteTemplates } from '../services/api';
import { notificationService } from '../services/notification';
import { Template } from '../../../common/types';

export const TemplateDeleteModal = ({
Expand Down Expand Up @@ -51,7 +51,7 @@ export const TemplateDeleteModal = ({
);

callback({ hasDeletedTemplates });
toastNotifications.addSuccess(successMessage);
notificationService.showSuccessToast(successMessage);
}

if (error || (errors && errors.length)) {
Expand All @@ -71,7 +71,7 @@ export const TemplateDeleteModal = ({
defaultMessage: "Error deleting template '{name}'",
values: { name: (errors && errors[0].name) || templatesToDelete[0] },
});
toastNotifications.addDanger(errorMessage);
notificationService.showDangerToast(errorMessage);
}
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { NotificationsStart } from '../../../../../../../src/core/public';

class NotificationService {
public init(notifications: NotificationsStart): void {
this.addToasts = (title: string, type: 'danger' | 'warning' | 'success', text?: string) => {
notifications.toasts.add({
title,
color: type,
text,
});
};
}

public showDangerToast(title: string, text?: string) {
this.addToasts(title, 'danger', text);
}

public showWarningToast(title: string, text?: string) {
this.addToasts(title, 'warning', text);
}

public showSuccessToast(title: string, text?: string) {
this.addToasts(title, 'success', text);
}
}

export const notificationService = new NotificationService();
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { toastNotifications } from 'ui/notify';

import { clearCacheIndices as request } from '../../services';
import { notificationService } from '../../services/notification';

import { clearRowStatus, reloadIndices } from '../actions';

export const clearCacheIndicesStart = createAction(
Expand All @@ -18,11 +20,11 @@ export const clearCacheIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.clearCacheIndicesAction.successMessage', {
defaultMessage: 'Successfully cleared cache: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { closeIndices as request } from '../../services';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';
import { clearRowStatus, reloadIndices } from '../actions';

export const closeIndicesStart = createAction(
Expand All @@ -18,11 +18,11 @@ export const closeIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.closeIndicesAction.successfullyClosedIndicesMessage', {
defaultMessage: 'Successfully closed: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { deleteIndices as request } from '../../services';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';
import { clearRowStatus } from '../actions';

export const deleteIndicesSuccess = createAction(
Expand All @@ -17,10 +17,10 @@ export const deleteIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.deleteIndicesAction.successfullyDeletedIndicesMessage', {
defaultMessage: 'Successfully deleted: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

import { i18n } from '@kbn/i18n';
import { loadIndexSettings as request } from '../../services';
import { notificationService } from '../../services/notification';
import { loadIndexDataSuccess } from './load_index_data';
import { toastNotifications } from 'ui/notify';

export const editIndexSettings = ({ indexName }) => async (dispatch) => {
let indexSettings;
try {
indexSettings = await request(indexName);
} catch (error) {
return toastNotifications.addDanger(error.data.message);
return notificationService.showDangerToast(error.data.message);
}
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.editIndexSettingsAction.successfullySavedSettingsForIndicesMessage', {
defaultMessage: 'Successfully saved settings for {indexName}',
values: { indexName }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
*/

import { reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const performExtensionAction = ({ requestMethod, indexNames, successMessage }) => async (dispatch) => {
try {
await requestMethod(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return;
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(successMessage);
notificationService.showSuccessToast(successMessage);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { flushIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const flushIndicesStart = createAction(
'INDEX_MANAGEMENT_FLUSH_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const flushIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.flushIndicesAction.successfullyFlushedIndicesMessage', {
defaultMessage: 'Successfully flushed: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { forcemergeIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const forcemergeIndicesStart = createAction(
'INDEX_MANAGEMENT_FORCEMERGE_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const forcemergeIndices = ({ indexNames, maxNumSegments }) => async (disp
try {
await request(indexNames, maxNumSegments);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.forceMergeIndicesAction.successfullyForceMergedIndicesMessage', {
defaultMessage: 'Successfully force merged: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { freezeIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const freezeIndicesStart = createAction(
'INDEX_MANAGEMENT_FREEZE_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const freezeIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.freezeIndicesAction.successfullyFrozeIndicesMessage', {
defaultMessage: 'Successfully froze: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { loadIndexData as request } from '../../services';
import { createAction } from 'redux-actions';
import { toastNotifications } from 'ui/notify';
import { loadIndexData as request } from '../../services';
import { notificationService } from '../../services/notification';

export const loadIndexDataSuccess = createAction('INDEX_MANAGEMENT_LOAD_INDEX_DATA_SUCCESS');

Expand All @@ -15,7 +15,7 @@ export const loadIndexData = ({ indexName, dataType }) => async (dispatch) => {
try {
data = await request(dataType, indexName);
} catch (error) {
return toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
}
dispatch(loadIndexDataSuccess({ data, indexName }));
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { openIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const openIndicesStart = createAction(
'INDEX_MANAGEMENT_OPEN_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const openIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.openIndicesAction.successfullyOpenedIndicesMessage', {
defaultMessage: 'Successfully opened: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n';

import { refreshIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const refreshIndicesStart = createAction(
'INDEX_MANAGEMENT_REFRESH_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const refreshIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.refreshIndicesAction.successfullyRefreshedIndicesMessage', {
defaultMessage: 'Successfully refreshed: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n';
import { getIndexNamesForCurrentPage } from '../selectors';
import { reloadIndices as request } from '../../services';
import { loadIndices } from './load_indices';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const reloadIndicesSuccess = createAction('INDEX_MANAGEMENT_RELOAD_INDICES_SUCCESS');
export const reloadIndices = (indexNames) => async (dispatch, getState) => {
Expand All @@ -24,12 +24,13 @@ export const reloadIndices = (indexNames) => async (dispatch, getState) => {
if (error.status === 404 || error.status === 403) {
return dispatch(loadIndices());
}
return toastNotifications.addDanger(error.data.message);
return notificationService.showDangerToast(error.data.message);

}
if (indices && indices.length > 0) {
return dispatch(reloadIndicesSuccess({ indices }));
} else {
return toastNotifications.addWarning(
return notificationService.showWarningToast(
i18n.translate('xpack.idxMgmt.reloadIndicesAction.indicesPageRefreshFailureMessage', {
defaultMessage: 'Failed to refresh current page of indices.',
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { unfreezeIndices as request } from '../../services';
import { clearRowStatus, reloadIndices } from '../actions';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const unfreezeIndicesStart = createAction(
'INDEX_MANAGEMENT_UNFREEZE_INDICES_START'
Expand All @@ -19,11 +19,11 @@ export const unfreezeIndices = ({ indexNames }) => async (dispatch) => {
try {
await request(indexNames);
} catch (error) {
toastNotifications.addDanger(error.data.message);
notificationService.showDangerToast(error.data.message);
return dispatch(clearRowStatus({ indexNames }));
}
dispatch(reloadIndices(indexNames));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.unfreezeIndicesAction.successfullyUnfrozeIndicesMessage', {
defaultMessage: 'Successfully unfroze: [{indexNames}]',
values: { indexNames: indexNames.join(', ') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAction } from 'redux-actions';
import { i18n } from '@kbn/i18n';
import { updateIndexSettings as request } from '../../services';
import { reloadIndices } from './reload_indices';
import { toastNotifications } from 'ui/notify';
import { notificationService } from '../../services/notification';

export const updateIndexSettingsSuccess = createAction(
'INDEX_MANAGEMENT_UPDATE_INDEX_SETTINGS_SUCCESS'
Expand All @@ -34,7 +34,7 @@ export const updateIndexSettings = ({
}
dispatch(updateIndexSettingsSuccess());
dispatch(reloadIndices([ indexName ]));
toastNotifications.addSuccess(
notificationService.showSuccessToast(
i18n.translate('xpack.idxMgmt.updateIndexSettingsAction.settingsSuccessUpdateMessage', {
defaultMessage: 'Successfully updated settings for index {indexName}',
values: { indexName }
Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/index_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import { LegacyStart } from './legacy';
import { httpService } from './app/services/http';
import { breadcrumbService } from './app/services/set_breadcrumbs';
import { documentationService } from './app/services/documentation';
import { notificationService } from './app/services/notification';

export class IndexMgmtPlugin {
public start(core: CoreStart, plugins: {}, __LEGACY: LegacyStart) {
const { management } = __LEGACY;
const { http, chrome, docLinks } = core;
const { http, chrome, docLinks, notifications } = core;

// Initialize services
httpService.init(http);
breadcrumbService.init(chrome, management.constants.BREADCRUMB);
documentationService.init(docLinks);
notificationService.init(notifications);

// Register management section and Angular route
registerManagementSection(management.getSection('elasticsearch'));
Expand Down

0 comments on commit 04e3001

Please sign in to comment.