Skip to content

Commit

Permalink
Replace ui/notify with npSetup.core.notifications (#47588)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Gospodarsky authored Oct 9, 2019
1 parent a063535 commit bee4212
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';
import { toastNotifications } from 'ui/notify';
import { NotificationsSetup } from 'kibana/public';
import { i18n } from '@kbn/i18n';
// @ts-ignore
import { ObjDefine } from './obj_define';
Expand Down Expand Up @@ -80,7 +80,8 @@ export class Field implements FieldType {
constructor(
indexPattern: IndexPattern,
spec: FieldSpec | Field,
shortDotsEnable: boolean = false
shortDotsEnable: boolean = false,
notifications: NotificationsSetup
) {
// unwrap old instances of Field
if (spec instanceof Field) spec = spec.$$spec;
Expand All @@ -106,7 +107,7 @@ export class Field implements FieldType {
defaultMessage: 'Field {name} in indexPattern {title} is using an unknown field type.',
});

toastNotifications.addDanger({
notifications.toasts.addDanger({
title,
text,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@
*/

import { IndexedArray } from 'ui/indexed_array';
import { NotificationsSetup } from 'kibana/public';
import { IndexPattern } from '../index_patterns';
import { Field, FieldSpec } from './field';

export class FieldList extends IndexedArray<Field> {
constructor(indexPattern: IndexPattern, specs: FieldSpec[], shortDotsEnable = false) {
constructor(
indexPattern: IndexPattern,
specs: FieldSpec[],
shortDotsEnable = false,
notifications: NotificationsSetup
) {
super({
index: ['name'],
group: ['type'],
initialSet: specs.map(function(field) {
return new Field(indexPattern, field, shortDotsEnable);
return new Field(indexPattern, field, shortDotsEnable, notifications);
}),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,27 @@ const apiClient = {
getFieldsForWildcard: jest.fn(),
};

const notifications = {
toasts: {
addDanger: jest.fn(),
addError: jest.fn(),
add: jest.fn(),
addWarning: jest.fn(),
addSuccess: jest.fn(),
remove: jest.fn(),
get$: jest.fn(),
},
};

// helper function to create index patterns
function create(id: string, payload?: any): Promise<IndexPattern> {
const indexPattern = new IndexPattern(
id,
(cfg: any) => config.get(cfg),
savedObjectsClient as any,
apiClient,
patternCache
patternCache,
notifications
);

setDocsourcePayload(id, payload);
Expand Down Expand Up @@ -374,7 +387,8 @@ describe('IndexPattern', () => {
(cfg: any) => config.get(cfg),
savedObjectsClient as any,
apiClient,
patternCache
patternCache,
notifications
);
await pattern.init();

Expand All @@ -386,7 +400,8 @@ describe('IndexPattern', () => {
(cfg: any) => config.get(cfg),
savedObjectsClient as any,
apiClient,
patternCache
patternCache,
notifications
);
await samePattern.init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { fieldFormats } from 'ui/registry/field_formats';
import { expandShorthand } from 'ui/utils/mapping_setup';
import { toastNotifications } from 'ui/notify';
import { findObjectByTitle } from 'ui/saved_objects';
import { SavedObjectsClientContract } from 'src/core/public';
import { NotificationsSetup, SavedObjectsClientContract } from 'src/core/public';
import { SavedObjectNotFound, DuplicateField } from '../../../../../../plugins/kibana_utils/public';

import { IndexPatternMissingIndices } from '../errors';
Expand Down Expand Up @@ -70,6 +70,7 @@ export class IndexPattern implements StaticIndexPattern {
public formatField: any;
public flattenHit: any;
public metaFields: string[];
public notifications: NotificationsSetup;

private version: string | undefined;
private savedObjectsClient: SavedObjectsClientContract;
Expand Down Expand Up @@ -107,19 +108,21 @@ export class IndexPattern implements StaticIndexPattern {
getConfig: any,
savedObjectsClient: SavedObjectsClientContract,
apiClient: IIndexPatternsApiClient,
patternCache: any
patternCache: any,
notifications: NotificationsSetup
) {
this.id = id;
this.savedObjectsClient = savedObjectsClient;
this.patternCache = patternCache;
// instead of storing config we rather store the getter only as np uiSettingsClient has circular references
// which cause problems when being consumed from angular
this.getConfig = getConfig;
this.notifications = notifications;

this.shortDotsEnable = this.getConfig('shortDots:enable');
this.metaFields = this.getConfig('metaFields');

this.fields = new FieldList(this, [], this.shortDotsEnable);
this.fields = new FieldList(this, [], this.shortDotsEnable, notifications);
this.fieldsFetcher = createFieldsFetcher(this, apiClient, this.getConfig('metaFields'));
this.flattenHit = flattenHitWrapper(this, this.getConfig('metaFields'));
this.formatHit = formatHitProvider(this, fieldFormats.getDefaultInstance('string'));
Expand All @@ -139,7 +142,7 @@ export class IndexPattern implements StaticIndexPattern {

private initFields(input?: any) {
const newValue = input || this.fields;
this.fields = new FieldList(this, newValue, this.shortDotsEnable);
this.fields = new FieldList(this, newValue, this.shortDotsEnable, this.notifications);
}

private isFieldRefreshRequired(): boolean {
Expand Down Expand Up @@ -274,16 +277,21 @@ export class IndexPattern implements StaticIndexPattern {
}

this.fields.push(
new Field(this, {
name,
script,
fieldType,
scripted: true,
lang,
aggregatable: true,
filterable: true,
searchable: true,
})
new Field(
this,
{
name,
script,
fieldType,
scripted: true,
lang,
aggregatable: true,
filterable: true,
searchable: true,
},
false,
this.notifications
)
);

await this.save();
Expand Down Expand Up @@ -373,7 +381,8 @@ export class IndexPattern implements StaticIndexPattern {
this.getConfig,
this.savedObjectsClient,
this.patternCache,
this.fieldsFetcher
this.fieldsFetcher,
this.notifications
);
await duplicatePattern.destroy();
}
Expand Down Expand Up @@ -426,7 +435,8 @@ export class IndexPattern implements StaticIndexPattern {
this.getConfig,
this.savedObjectsClient,
this.patternCache,
this.fieldsFetcher
this.fieldsFetcher,
this.notifications
);
return samePattern.init().then(() => {
// What keys changed from now and what the server returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SavedObjectsClientContract,
UiSettingsClientContract,
HttpServiceBase,
NotificationsSetup,
} from 'kibana/public';

jest.mock('../errors', () => ({
Expand Down Expand Up @@ -70,8 +71,9 @@ describe('IndexPatterns', () => {
const savedObjectsClient = {} as SavedObjectsClientContract;
const uiSettings = {} as UiSettingsClientContract;
const http = {} as HttpServiceBase;
const notifications = {} as NotificationsSetup;

indexPatterns = new IndexPatterns(uiSettings, savedObjectsClient, http);
indexPatterns = new IndexPatterns(uiSettings, savedObjectsClient, http, notifications);
});

test('does cache gets for the same id', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SimpleSavedObject,
UiSettingsClientContract,
HttpServiceBase,
NotificationsSetup,
} from 'src/core/public';
// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';
Expand All @@ -40,13 +41,16 @@ export class IndexPatterns {
private savedObjectsClient: SavedObjectsClientContract;
private savedObjectsCache?: Array<SimpleSavedObject<Record<string, any>>> | null;
private apiClient: IndexPatternsApiClient;
private notifications: NotificationsSetup;

constructor(
config: UiSettingsClientContract,
savedObjectsClient: SavedObjectsClientContract,
http: HttpServiceBase
http: HttpServiceBase,
notifications: NotificationsSetup
) {
this.apiClient = new IndexPatternsApiClient(http);
this.notifications = notifications;

this.config = config;
this.savedObjectsClient = savedObjectsClient;
Expand Down Expand Up @@ -129,7 +133,8 @@ export class IndexPatterns {
(cfg: any) => this.config.get(cfg),
this.savedObjectsClient,
this.apiClient,
indexPatternCache
indexPatternCache,
this.notifications
).init();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
UiSettingsClientContract,
SavedObjectsClientContract,
HttpServiceBase,
NotificationsSetup,
} from 'src/core/public';
import { Field, FieldList, FieldType } from './fields';
import { createFlattenHitWrapper } from './index_patterns';
Expand All @@ -36,6 +37,7 @@ export interface IndexPatternDependencies {
uiSettings: UiSettingsClientContract;
savedObjectsClient: SavedObjectsClientContract;
http: HttpServiceBase;
notifications: NotificationsSetup;
}

/**
Expand All @@ -44,12 +46,12 @@ export interface IndexPatternDependencies {
* @internal
*/
export class IndexPatternsService {
public setup({ uiSettings, savedObjectsClient, http }: IndexPatternDependencies) {
public setup({ uiSettings, savedObjectsClient, http, notifications }: IndexPatternDependencies) {
return {
FieldList,
flattenHitWrapper: createFlattenHitWrapper(),
formatHitProvider,
indexPatterns: new IndexPatterns(uiSettings, savedObjectsClient, http),
indexPatterns: new IndexPatterns(uiSettings, savedObjectsClient, http, notifications),
IndexPatternSelect: createIndexPatternSelect(savedObjectsClient),
__LEGACY: {
// For BWC we must temporarily export the class implementation of Field,
Expand Down
5 changes: 3 additions & 2 deletions src/legacy/core_plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { CoreSetup, CoreStart, Plugin } from '../../../../core/public';
import { CoreSetup, CoreStart, Plugin } from 'kibana/public';
import { SearchService, SearchSetup, createSearchBar, StatetfulSearchBarProps } from './search';
import { QueryService, QuerySetup } from './query';
import { FilterService, FilterSetup } from './filter';
Expand Down Expand Up @@ -96,13 +96,14 @@ export class DataPlugin
private setupApi!: DataSetup;

public setup(core: CoreSetup, { __LEGACY }: DataPluginSetupDependencies): DataSetup {
const { uiSettings, http } = core;
const { uiSettings, http, notifications } = core;
const savedObjectsClient = __LEGACY.savedObjectsClient;

const indexPatternsService = this.indexPatterns.setup({
uiSettings,
savedObjectsClient,
http,
notifications,
});

const timefilterService = this.timefilter.setup({
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/core_plugins/data/public/shim/legacy_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export const initLegacyModule = once((): void => {
_service = new IndexPatterns(
npStart.core.uiSettings,
npStart.core.savedObjects.client,
npStart.core.http
npStart.core.http,
npStart.core.notifications
);
return _service;
});
Expand Down

0 comments on commit bee4212

Please sign in to comment.