Skip to content

Commit

Permalink
[Time to Visualize] Small Attribute Service Fixes (#82072) (#82316)
Browse files Browse the repository at this point in the history
* Removed some dashboard-centric wording from the Attribute Service & removed type argument from save method
  • Loading branch information
ThomThomson authored Nov 2, 2020
1 parent 87db37c commit fc4a04b
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,12 @@ export class BookEmbeddableFactoryDefinition
return { ...savedObject.attributes };
}

private async saveMethod(
type: string,
attributes: BookSavedObjectAttributes,
savedObjectId?: string
) {
private async saveMethod(attributes: BookSavedObjectAttributes, savedObjectId?: string) {
const { savedObjectsClient } = await this.getStartServices();
if (savedObjectId) {
return savedObjectsClient.update(type, savedObjectId, attributes);
return savedObjectsClient.update(this.type, savedObjectId, attributes);
}
return savedObjectsClient.create(type, attributes);
return savedObjectsClient.create(this.type, attributes);
}

private async checkForDuplicateTitleMethod(props: OnSaveProps): Promise<true> {
Expand Down
10 changes: 3 additions & 7 deletions examples/embeddable_examples/public/book/edit_book_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ export const createEditBookAction = (getStartServices: () => Promise<StartServic
execute: async ({ embeddable }: ActionContext) => {
const { openModal, getAttributeService, savedObjectsClient } = await getStartServices();
const attributeService = getAttributeService<BookSavedObjectAttributes>(BOOK_SAVED_OBJECT, {
saveMethod: async (
type: string,
attributes: BookSavedObjectAttributes,
savedObjectId?: string
) => {
saveMethod: async (attributes: BookSavedObjectAttributes, savedObjectId?: string) => {
if (savedObjectId) {
return savedObjectsClient.update(type, savedObjectId, attributes);
return savedObjectsClient.update(BOOK_EMBEDDABLE, savedObjectId, attributes);
}
return savedObjectsClient.create(type, attributes);
return savedObjectsClient.create(BOOK_EMBEDDABLE, attributes);
},
checkForDuplicateTitle: (props: OnSaveProps) => {
return new Promise(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('attributeService', () => {
let byValueInput: TestByValueInput;
let byReferenceInput: { id: string; savedObjectId: string };
const defaultSaveMethod = (
type: string,
testAttributes: TestAttributes,
savedObjectId?: string
): Promise<{ id: string }> => {
Expand Down Expand Up @@ -166,7 +165,7 @@ describe('attributeService', () => {
expect(await attributeService.wrapAttributes(attributes, true, byReferenceInput)).toEqual(
byReferenceInput
);
expect(saveMethod).toHaveBeenCalledWith(defaultTestType, attributes, '123');
expect(saveMethod).toHaveBeenCalledWith(attributes, '123');
});

it('uses custom save method when given an id', async () => {
Expand All @@ -179,11 +178,7 @@ describe('attributeService', () => {
expect(await attributeService.wrapAttributes(attributes, true, byReferenceInput)).toEqual(
byReferenceInput
);
expect(saveMethod).toHaveBeenCalledWith(
defaultTestType,
attributes,
byReferenceInput.savedObjectId
);
expect(saveMethod).toHaveBeenCalledWith(attributes, byReferenceInput.savedObjectId);
});

it('uses custom save method given no id', async () => {
Expand All @@ -196,7 +191,7 @@ describe('attributeService', () => {
expect(await attributeService.wrapAttributes(attributes, true)).toEqual({
savedObjectId: '678',
});
expect(saveMethod).toHaveBeenCalledWith(defaultTestType, attributes, undefined);
expect(saveMethod).toHaveBeenCalledWith(attributes, undefined);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export const ATTRIBUTE_SERVICE_KEY = 'attributes';

export interface AttributeServiceOptions<A extends { title: string }> {
saveMethod: (
type: string,
attributes: A,
savedObjectId?: string
) => Promise<{ id?: string } | { error: Error }>;
Expand Down Expand Up @@ -106,20 +105,20 @@ export class AttributeService<
return { [ATTRIBUTE_SERVICE_KEY]: newAttributes } as ValType;
}
try {
const savedItem = await this.options.saveMethod(this.type, newAttributes, savedObjectId);
const savedItem = await this.options.saveMethod(newAttributes, savedObjectId);
if ('id' in savedItem) {
return { ...originalInput, savedObjectId: savedItem.id } as RefType;
}
return { ...originalInput } as RefType;
} catch (error) {
this.toasts.addDanger({
title: i18n.translate('embeddableApi.attributeService.saveToLibraryError', {
defaultMessage: `Panel was not saved to the library. Error: {errorMessage}`,
defaultMessage: `An error occurred while saving. Error: {errorMessage}`,
values: {
errorMessage: error.message,
},
}),
'data-test-subj': 'saveDashboardFailure',
'data-test-subj': 'attributeServiceSaveFailure',
});
return Promise.reject({ error });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ export class VisualizeEmbeddableFactory
}
}

private async saveMethod(
type: string,
attributes: VisualizeSavedObjectAttributes
): Promise<{ id: string }> {
private async saveMethod(attributes: VisualizeSavedObjectAttributes): Promise<{ id: string }> {
try {
const { title, savedVis } = attributes;
const visObj = attributes.vis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const savedVis: Document = {
visualizationType: '',
};
const defaultSaveMethod = (
type: string,
testAttributes: LensSavedObjectAttributes,
savedObjectId?: string
): Promise<{ id: string }> => {
Expand Down
6 changes: 1 addition & 5 deletions x-pack/plugins/lens/public/lens_attribute_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export function getLensAttributeService(
LensByValueInput,
LensByReferenceInput
>(DOC_TYPE, {
saveMethod: async (
type: string,
attributes: LensSavedObjectAttributes,
savedObjectId?: string
) => {
saveMethod: async (attributes: LensSavedObjectAttributes, savedObjectId?: string) => {
const savedDoc = await savedObjectStore.save({
...attributes,
savedObjectId,
Expand Down

0 comments on commit fc4a04b

Please sign in to comment.