Skip to content

Commit

Permalink
Remove savedObjectsClient from attribute service
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Sep 28, 2020
1 parent ceae41b commit 384653f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ export class BookEmbeddableFactoryDefinition

private async getAttributeService() {
if (!this.attributeService) {
this.attributeService = await (await this.getStartServices()).getAttributeService<
this.attributeService = (await this.getStartServices()).getAttributeService<
BookSavedObjectAttributes
>(this.type, {
saveMethod: this.saveMethod.bind(this),
unwrapMethod: this.unwrapMethod.bind(this),
unwrapMethod: await this.unwrapMethod.bind(this),
});
}
return this.attributeService!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,8 @@ import {
EmbeddableFactory,
EmbeddableFactoryNotFoundError,
} from '../embeddable_plugin';
import {
SavedObjectsClientContract,
I18nStart,
NotificationsStart,
OverlayStart,
} from '../../../../core/public';
import {
SavedObjectSaveModal,
OnSaveProps,
SaveResult,
checkForDuplicateTitle,
} from '../../../saved_objects/public';
import { I18nStart, NotificationsStart } from '../../../../core/public';
import { SavedObjectSaveModal, OnSaveProps, SaveResult } from '../../../saved_objects/public';

/**
* The attribute service is a shared, generic service that embeddables can use to provide the functionality
Expand All @@ -57,7 +47,8 @@ export interface AttributeServiceOptions<A extends { title: string }> {
attributes: A,
savedObjectId?: string
) => Promise<{ id?: string } | { error: Error }>;
unwrapMethod: (savedObjectId: string) => A;
unwrapMethod?: (savedObjectId: string) => A;
checkForDuplicateTitle: (props: OnSaveProps) => Promise<true>;
}

export class AttributeService<
Expand All @@ -75,8 +66,6 @@ export class AttributeService<
saveModal: React.ReactElement,
I18nContext: I18nStart['Context']
) => void,
private savedObjectsClient: SavedObjectsClientContract,
private overlays: OverlayStart,
private i18nContext: I18nStart['Context'],
private toasts: NotificationsStart['toasts'],
getEmbeddableFactory?: EmbeddableStart['getEmbeddableFactory'],
Expand All @@ -92,7 +81,7 @@ export class AttributeService<
}

public async unwrapAttributes(input: RefType | ValType): Promise<SavedObjectAttributes> {
if (this.inputIsRefType(input)) {
if (this.inputIsRefType(input) && this.options && this.options.unwrapMethod) {
return this.options.unwrapMethod(input.savedObjectId);
}
return input[ATTRIBUTE_SERVICE_KEY];
Expand Down Expand Up @@ -165,21 +154,7 @@ export class AttributeService<
}
return new Promise<RefType>((resolve, reject) => {
const onSave = async (props: OnSaveProps): Promise<SaveResult> => {
await checkForDuplicateTitle(
{
title: props.newTitle,
copyOnSave: false,
lastSavedTitle: '',
getEsType: () => this.type,
getDisplayName: this.embeddableFactory?.getDisplayName || (() => this.type),
},
props.isTitleDuplicateConfirmed,
props.onTitleDuplicate,
{
savedObjectsClient: this.savedObjectsClient,
overlays: this.overlays,
}
);
await this.options.checkForDuplicateTitle(props);
try {
const newAttributes = { ...input[ATTRIBUTE_SERVICE_KEY] };
newAttributes.title = props.newTitle;
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,6 @@ export class DashboardPlugin
new AttributeService(
type,
showSaveModal,
core.savedObjects.client,
core.overlays,
core.i18n.Context,
core.notifications.toasts,
embeddable.getEmbeddableFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { i18n } from '@kbn/i18n';
import { SavedObjectMetaData } from 'src/plugins/saved_objects/public';
import { SavedObjectMetaData, OnSaveProps } from 'src/plugins/saved_objects/public';
import { first } from 'rxjs/operators';
import { SavedObjectAttributes } from '../../../../core/public';
import {
Expand Down Expand Up @@ -51,14 +51,15 @@ import { StartServicesGetter } from '../../../kibana_utils/public';
import { VisualizationsStartDeps } from '../plugin';
import { VISUALIZE_ENABLE_LABS_SETTING } from '../../common/constants';
import { AttributeService } from '../../../dashboard/public';
import { checkForDuplicateTitle } from '../../../saved_objects/public';

interface VisualizationAttributes extends SavedObjectAttributes {
visState: string;
}

export interface VisualizeEmbeddableFactoryDeps {
start: StartServicesGetter<
Pick<VisualizationsStartDeps, 'inspector' | 'embeddable' | 'dashboard'>
Pick<VisualizationsStartDeps, 'inspector' | 'embeddable' | 'dashboard' | 'savedObjectsClient'>
>;
}

Expand Down Expand Up @@ -129,7 +130,10 @@ export class VisualizeEmbeddableFactory
VisualizeSavedObjectAttributes,
VisualizeByValueInput,
VisualizeByReferenceInput
>(this.type, { saveMethod: this.onSave });
>(this.type, {
saveMethod: this.saveMethod.bind(this),
checkForDuplicateTitle: this.checkTitle.bind(this),
});
}
return this.attributeService!;
}
Expand Down Expand Up @@ -183,7 +187,7 @@ export class VisualizeEmbeddableFactory
}
}

private async onSave(
private async saveMethod(
type: string,
attributes: VisualizeSavedObjectAttributes
): Promise<{ id: string }> {
Expand Down Expand Up @@ -225,4 +229,24 @@ export class VisualizeEmbeddableFactory
throw error;
}
}

public async checkTitle(props: OnSaveProps): Promise<true> {
const savedObjectsClient = await this.deps.start().core.savedObjects.client;
const overlays = await this.deps.start().core.overlays;
return await checkForDuplicateTitle(
{
title: props.newTitle,
copyOnSave: false,
lastSavedTitle: '',
getEsType: () => this.type,
getDisplayName: this.getDisplayName || (() => this.type),
},
props.isTitleDuplicateConfirmed,
props.onTitleDuplicate,
{
savedObjectsClient,
overlays,
}
);
}
}
2 changes: 2 additions & 0 deletions src/plugins/visualizations/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
CoreStart,
Plugin,
ApplicationStart,
SavedObjectsClientContract,
} from '../../../core/public';
import { TypesService, TypesSetup, TypesStart } from './vis_types';
import {
Expand Down Expand Up @@ -112,6 +113,7 @@ export interface VisualizationsStartDeps {
application: ApplicationStart;
dashboard: DashboardStart;
getAttributeService: DashboardStart['getAttributeService'];
savedObjectsClient: SavedObjectsClientContract;
}

/**
Expand Down

0 comments on commit 384653f

Please sign in to comment.