Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] convert SavedGisMap to TS #72286

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

import _ from 'lodash';
import { createSavedObjectClass } from '../../../../../../../src/plugins/saved_objects/public';
import { SavedObjectReference } from 'kibana/public';
import {
createSavedObjectClass,
SavedObject,
SavedObjectKibanaServices,
} from '../../../../../../../src/plugins/saved_objects/public';
import {
getTimeFilters,
getMapZoom,
Expand All @@ -18,73 +23,80 @@ import {
} from '../../../selectors/map_selectors';
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../../../selectors/ui_selectors';
import { copyPersistentState } from '../../../reducers/util';
// @ts-expect-error
import { extractReferences, injectReferences } from '../../../../common/migrations/references';
import { getExistingMapPath, MAP_SAVED_OBJECT_TYPE } from '../../../../common/constants';
// @ts-expect-error
import { getStore } from '../../store_operations';
import { MapStoreState } from '../../../reducers/store';
import { LayerDescriptor } from '../../../../common/descriptor_types';

export interface ISavedGisMap extends SavedObject {
layerListJSON?: string;
mapStateJSON?: string;
uiStateJSON?: string;
getLayerList(): LayerDescriptor[];
syncWithStore(): void;
}

export function createSavedGisMapClass(services) {
export function createSavedGisMapClass(services: SavedObjectKibanaServices) {
const SavedObjectClass = createSavedObjectClass(services);

class SavedGisMap extends SavedObjectClass {
static type = MAP_SAVED_OBJECT_TYPE;
class SavedGisMap extends SavedObjectClass implements ISavedGisMap {
public static type = MAP_SAVED_OBJECT_TYPE;

// Mappings are used to place object properties into saved object _source
static mapping = {
public static mapping = {
title: 'text',
description: 'text',
mapStateJSON: 'text',
layerListJSON: 'text',
uiStateJSON: 'text',
};
static fieldOrder = ['title', 'description'];
static searchSource = false;
public static fieldOrder = ['title', 'description'];
public static searchSource = false;

constructor(id) {
public showInRecentlyAccessed = true;
public layerListJSON?: string;
public mapStateJSON?: string;
public uiStateJSON?: string;

constructor(id: string) {
super({
type: SavedGisMap.type,
mapping: SavedGisMap.mapping,
searchSource: SavedGisMap.searchSource,
extractReferences,
injectReferences: (savedObject, references) => {
injectReferences: (savedObject: ISavedGisMap, references: SavedObjectReference[]) => {
const { attributes } = injectReferences({
attributes: { layerListJSON: savedObject.layerListJSON },
references,
});

savedObject.layerListJSON = attributes.layerListJSON;

const indexPatternIds = references
.filter((reference) => {
return reference.type === 'index-pattern';
})
.map((reference) => {
return reference.id;
});
savedObject.indexPatternIds = _.uniq(indexPatternIds);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

savedObject.indexPatternIds was added when MapEmbeddable was first introduced in https://github.com/elastic/kibana/pull/31473/files#diff-1bb10565f2889677ad2ded8210390726R50. At the time MapEmbeddableFactory used savedObject.indexPatternIds to obtain index patterns used by the map

#35542 changed the way MapEmbeddableFactory obtained indexPatternIds to pulling them from store, acb78a6#diff-e347d1b056afd109215cc604d63d4aefL47. However, that PR failed to clean up setting indexPatternIds on savedMap.

Since savedMap.indexPatternIds is no longer used, it is safe to remove this code

},

// if this is null/undefined then the SavedObject will be assigned the defaults
id: id,
id,

// default values that will get assigned if the doc is new
defaults: {
title: 'New Map',
description: '',
},
});
this.showInRecentlyAccessed = true;
}

getFullPath() {
return getExistingMapPath(this.id);
this.getFullPath = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this function getting created in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescripting. Something about functional method versus prototypemethod.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds related to the fact that it's now an arrow function. Does it need to be? It doesn't look like this PR changes any calls to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for typescripting purposes, yes. I had to copy what SavedDashboard was doing to get TS to be happy.

return getExistingMapPath(this.id!);
};
}

getLayerList() {
return this.layerListJSON ? JSON.parse(this.layerListJSON) : null;
}

syncWithStore() {
const state = getStore().getState();
const state: MapStoreState = getStore().getState();
const layerList = getLayerListRaw(state);
const layerListConfigOnly = copyPersistentState(layerList);
this.layerListJSON = JSON.stringify(layerListConfigOnly);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/public/selectors/map_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { ISource } from '../classes/sources/source';
import { ITMSSource } from '../classes/sources/tms_source';
import { IVectorSource } from '../classes/sources/vector_source';
import { ILayer } from '../classes/layers/layer';
import { ISavedGisMap } from '../routing/bootstrap/services/saved_gis_map';

function createLayerInstance(
layerDescriptor: LayerDescriptor,
Expand Down Expand Up @@ -419,12 +420,11 @@ export const areLayersLoaded = createSelector(

export function hasUnsavedChanges(
state: MapStoreState,
savedMap: unknown,
savedMap: ISavedGisMap,
initialLayerListConfig: LayerDescriptor[]
) {
const layerListConfigOnly = copyPersistentState(getLayerListRaw(state));

// @ts-expect-error
const savedLayerList = savedMap.getLayerList();

return !savedLayerList
Expand Down