From 1189f446f64a9e5d632a5fc26bf153c03a692df6 Mon Sep 17 00:00:00 2001 From: Maja Grubic Date: Thu, 10 Sep 2020 13:36:59 +0100 Subject: [PATCH] Using custom save method in visualize embeddable factory --- .../attribute_service.test.ts | 10 --- .../attribute_service/attribute_service.tsx | 4 - .../create_vis_embeddable_from_object.ts | 11 +-- .../public/embeddable/visualize_embeddable.ts | 88 ++++++------------- .../visualize_embeddable_factory.tsx | 38 +++++++- 5 files changed, 66 insertions(+), 85 deletions(-) diff --git a/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts b/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts index 91b86a4b0c8a3..06f380ca3862b 100644 --- a/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts +++ b/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts @@ -189,15 +189,5 @@ describe('attributeService', () => { }); expect(customSaveMethod).toHaveBeenCalledWith(defaultTestType, attributes, undefined); }); - - it('uses custom save method when passed through setOptions', async () => { - const customSaveMethod = jest.fn().mockReturnValue({ id: '678' }); - const attributeService = mockAttributeService(defaultTestType); - attributeService.setOptions({ customSaveMethod }); - expect(await attributeService.wrapAttributes(attributes, true)).toEqual({ - savedObjectId: '678', - }); - expect(customSaveMethod).toHaveBeenCalledWith(defaultTestType, attributes, undefined); - }); }); }); diff --git a/src/plugins/dashboard/public/attribute_service/attribute_service.tsx b/src/plugins/dashboard/public/attribute_service/attribute_service.tsx index dd9dea361a128..84df05154fb63 100644 --- a/src/plugins/dashboard/public/attribute_service/attribute_service.tsx +++ b/src/plugins/dashboard/public/attribute_service/attribute_service.tsx @@ -226,8 +226,4 @@ export class AttributeService< } }); }; - - public setOptions = (options: AttributeServiceOptions) => { - this.options = options; - }; } diff --git a/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts b/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts index 01723bc4e0219..b27d24d980e8d 100644 --- a/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts +++ b/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts @@ -36,8 +36,8 @@ import { } from '../services'; import { VisualizeEmbeddableFactoryDeps } from './visualize_embeddable_factory'; import { VISUALIZE_ENABLE_LABS_SETTING } from '../../common/constants'; -import { AttributeService } from '../../../dashboard/public'; import { SavedVisualizationsLoader } from '../saved_visualizations'; +import { AttributeService } from '../../../dashboard/public'; export const createVisEmbeddableFromObject = (deps: VisualizeEmbeddableFactoryDeps) => async ( vis: Vis, @@ -70,15 +70,6 @@ export const createVisEmbeddableFromObject = (deps: VisualizeEmbeddableFactoryDe const indexPatterns = indexPattern ? [indexPattern] : []; const editable = getCapabilities().visualize.save as boolean; - if (!attributeService) { - const dashboard = deps.start().plugins.dashboard; - attributeService = await dashboard!.getAttributeService< - VisualizeSavedObjectAttributes, - VisualizeByValueInput, - VisualizeByReferenceInput - >('visualization'); - } - return new VisualizeEmbeddable( getTimeFilter(), { diff --git a/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts b/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts index 59220aca3ed05..e265a02357183 100644 --- a/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts +++ b/src/plugins/visualizations/public/embeddable/visualize_embeddable.ts @@ -110,7 +110,7 @@ export class VisualizeEmbeddable private abortController?: AbortController; private readonly deps: VisualizeEmbeddableFactoryDeps; private readonly inspectorAdapters?: Adapters; - private attributeService: AttributeService< + private attributeService?: AttributeService< VisualizeSavedObjectAttributes, VisualizeByValueInput, VisualizeByReferenceInput @@ -121,7 +121,7 @@ export class VisualizeEmbeddable timefilter: TimefilterContract, { vis, editPath, editUrl, indexPatterns, editable, deps }: VisualizeEmbeddableConfiguration, initialInput: VisualizeInput, - attributeService: AttributeService< + attributeService?: AttributeService< VisualizeSavedObjectAttributes, VisualizeByValueInput, VisualizeByReferenceInput @@ -414,6 +414,9 @@ export class VisualizeEmbeddable } inputIsRefType = (input: VisualizeInput): input is VisualizeByReferenceInput => { + if (!this.attributeService) { + throw new Error('AttributeService must be defined for getInputAsRefType'); + } return this.attributeService.inputIsRefType(input as VisualizeByReferenceInput); }; @@ -429,65 +432,30 @@ export class VisualizeEmbeddable }; getInputAsRefType = async (): Promise => { - const savedVis: VisSavedObject = await this.savedVisualizationsLoader?.get({}); + const savedVis = await this.savedVisualizationsLoader?.get({}); if (!savedVis) { - throw new Error('No Saved Vis'); + throw new Error('Error creating a saved vis object'); } - return new Promise((resolve, reject) => { - const onSave = async ( - type: string, - attributes: VisualizeSavedObjectAttributes - ): Promise<{ id: string }> => { - try { - const { title, vis } = attributes; - const saveOptions = { - confirmOverwrite: false, - returnToOrigin: true, - }; - savedVis.title = title; - savedVis.copyOnSave = false; - savedVis.description = ''; - savedVis.searchSourceFields = vis?.data.searchSource?.getSerializedFields(); - const serializedVis = ((vis as unknown) as Vis).serialize(); - const { params, data } = serializedVis; - savedVis.visState = { - title, - type: serializedVis.type, - params, - aggs: data.aggs, - }; - if (vis) { - savedVis.uiStateJSON = vis.uiState.toString(); - } - const id = await savedVis.save(saveOptions); - resolve({ savedObjectId: id, id: this.id }); - return { id }; - } catch (error) { - reject(error); - return { id: '' }; - } - }; - const saveModalTitle = this.getTitle() - ? this.getTitle() - : i18n.translate('visualizations.embeddable.placeholderTitle', { - defaultMessage: 'Placeholder Title', - }); - // @ts-ignore - const attributes: VisualizeSavedObjectAttributes = { - savedVis, - vis: this.vis, - title: this.vis.title, - }; - this.attributeService.setOptions({ - customSaveMethod: onSave, - }); - return this.attributeService.getInputAsRefType( - { - id: this.id, - attributes, - }, - { showSaveModal: true, saveModalTitle } - ); - }); + if (!this.attributeService) { + throw new Error('AttributeService must be defined for getInputAsRefType'); + } + const saveModalTitle = this.getTitle() + ? this.getTitle() + : i18n.translate('visualizations.embeddable.placeholderTitle', { + defaultMessage: 'Placeholder Title', + }); + // @ts-ignore + const attributes: VisualizeSavedObjectAttributes = { + savedVis, + vis: this.vis, + title: this.vis.title, + }; + return this.attributeService.getInputAsRefType( + { + id: this.id, + attributes, + }, + { showSaveModal: true, saveModalTitle } + ); }; } diff --git a/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx b/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx index 221fa5daafb11..cc27dd06f2d2a 100644 --- a/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx +++ b/src/plugins/visualizations/public/embeddable/visualize_embeddable_factory.tsx @@ -129,7 +129,7 @@ export class VisualizeEmbeddableFactory VisualizeSavedObjectAttributes, VisualizeByValueInput, VisualizeByReferenceInput - >(this.type); + >(this.type, { customSaveMethod: this.onSave }); } return this.attributeService!; } @@ -182,4 +182,40 @@ export class VisualizeEmbeddableFactory return undefined; } } + + private async onSave( + type: string, + attributes: VisualizeSavedObjectAttributes + ): Promise<{ id: string }> { + try { + const { title, savedVis } = attributes; + const visObj = attributes.vis; + if (!savedVis) { + throw new Error('No Saved Vis'); + } + const saveOptions = { + confirmOverwrite: false, + returnToOrigin: true, + }; + savedVis.title = title; + savedVis.copyOnSave = false; + savedVis.description = ''; + savedVis.searchSourceFields = visObj?.data.searchSource?.getSerializedFields(); + const serializedVis = ((visObj as unknown) as Vis).serialize(); + const { params, data } = serializedVis; + savedVis.visState = { + title, + type: serializedVis.type, + params, + aggs: data.aggs, + }; + if (visObj) { + savedVis.uiStateJSON = visObj?.uiState.toString(); + } + const id = await savedVis.save(saveOptions); + return { id }; + } catch (error) { + return { id: '' }; + } + } }