Skip to content

Commit

Permalink
Using custom save method in visualize embeddable factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Sep 10, 2020
1 parent db9adc0 commit 1189f44
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestAttributes>(defaultTestType);
attributeService.setOptions({ customSaveMethod });
expect(await attributeService.wrapAttributes(attributes, true)).toEqual({
savedObjectId: '678',
});
expect(customSaveMethod).toHaveBeenCalledWith(defaultTestType, attributes, undefined);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,4 @@ export class AttributeService<
}
});
};

public setOptions = (options: AttributeServiceOptions<SavedObjectAttributes>) => {
this.options = options;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
};

Expand All @@ -429,65 +432,30 @@ export class VisualizeEmbeddable
};

getInputAsRefType = async (): Promise<VisualizeByReferenceInput> => {
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<VisualizeByReferenceInput>((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 }
);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class VisualizeEmbeddableFactory
VisualizeSavedObjectAttributes,
VisualizeByValueInput,
VisualizeByReferenceInput
>(this.type);
>(this.type, { customSaveMethod: this.onSave });
}
return this.attributeService!;
}
Expand Down Expand Up @@ -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: '' };
}
}
}

0 comments on commit 1189f44

Please sign in to comment.