Skip to content

Commit 5ddf6a5

Browse files
committed
[Maps] convert SavedGisMap to TS (#72286)
* [Maps] convert SavedGisMap to TS * i18n translate new map title
1 parent 4defcd6 commit 5ddf6a5

2 files changed

Lines changed: 42 additions & 27 deletions

File tree

x-pack/plugins/maps/public/routing/bootstrap/services/saved_gis_map.js renamed to x-pack/plugins/maps/public/routing/bootstrap/services/saved_gis_map.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
*/
66

77
import _ from 'lodash';
8-
import { createSavedObjectClass } from '../../../../../../../src/plugins/saved_objects/public';
8+
import { SavedObjectReference } from 'kibana/public';
9+
import { i18n } from '@kbn/i18n';
10+
import {
11+
createSavedObjectClass,
12+
SavedObject,
13+
SavedObjectKibanaServices,
14+
} from '../../../../../../../src/plugins/saved_objects/public';
915
import {
1016
getTimeFilters,
1117
getMapZoom,
@@ -18,73 +24,82 @@ import {
1824
} from '../../../selectors/map_selectors';
1925
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../../../selectors/ui_selectors';
2026
import { copyPersistentState } from '../../../reducers/util';
27+
// @ts-expect-error
2128
import { extractReferences, injectReferences } from '../../../../common/migrations/references';
2229
import { getExistingMapPath, MAP_SAVED_OBJECT_TYPE } from '../../../../common/constants';
30+
// @ts-expect-error
2331
import { getStore } from '../../store_operations';
32+
import { MapStoreState } from '../../../reducers/store';
33+
import { LayerDescriptor } from '../../../../common/descriptor_types';
34+
35+
export interface ISavedGisMap extends SavedObject {
36+
layerListJSON?: string;
37+
mapStateJSON?: string;
38+
uiStateJSON?: string;
39+
getLayerList(): LayerDescriptor[];
40+
syncWithStore(): void;
41+
}
2442

25-
export function createSavedGisMapClass(services) {
43+
export function createSavedGisMapClass(services: SavedObjectKibanaServices) {
2644
const SavedObjectClass = createSavedObjectClass(services);
2745

28-
class SavedGisMap extends SavedObjectClass {
29-
static type = MAP_SAVED_OBJECT_TYPE;
46+
class SavedGisMap extends SavedObjectClass implements ISavedGisMap {
47+
public static type = MAP_SAVED_OBJECT_TYPE;
3048

3149
// Mappings are used to place object properties into saved object _source
32-
static mapping = {
50+
public static mapping = {
3351
title: 'text',
3452
description: 'text',
3553
mapStateJSON: 'text',
3654
layerListJSON: 'text',
3755
uiStateJSON: 'text',
3856
};
39-
static fieldOrder = ['title', 'description'];
40-
static searchSource = false;
57+
public static fieldOrder = ['title', 'description'];
58+
public static searchSource = false;
4159

42-
constructor(id) {
60+
public showInRecentlyAccessed = true;
61+
public layerListJSON?: string;
62+
public mapStateJSON?: string;
63+
public uiStateJSON?: string;
64+
65+
constructor(id: string) {
4366
super({
4467
type: SavedGisMap.type,
4568
mapping: SavedGisMap.mapping,
4669
searchSource: SavedGisMap.searchSource,
4770
extractReferences,
48-
injectReferences: (savedObject, references) => {
71+
injectReferences: (savedObject: ISavedGisMap, references: SavedObjectReference[]) => {
4972
const { attributes } = injectReferences({
5073
attributes: { layerListJSON: savedObject.layerListJSON },
5174
references,
5275
});
5376

5477
savedObject.layerListJSON = attributes.layerListJSON;
55-
56-
const indexPatternIds = references
57-
.filter((reference) => {
58-
return reference.type === 'index-pattern';
59-
})
60-
.map((reference) => {
61-
return reference.id;
62-
});
63-
savedObject.indexPatternIds = _.uniq(indexPatternIds);
6478
},
6579

6680
// if this is null/undefined then the SavedObject will be assigned the defaults
67-
id: id,
81+
id,
6882

6983
// default values that will get assigned if the doc is new
7084
defaults: {
71-
title: 'New Map',
85+
title: i18n.translate('xpack.maps.newMapTitle', {
86+
defaultMessage: 'New Map',
87+
}),
7288
description: '',
7389
},
7490
});
75-
this.showInRecentlyAccessed = true;
76-
}
7791

78-
getFullPath() {
79-
return getExistingMapPath(this.id);
92+
this.getFullPath = () => {
93+
return getExistingMapPath(this.id!);
94+
};
8095
}
8196

8297
getLayerList() {
8398
return this.layerListJSON ? JSON.parse(this.layerListJSON) : null;
8499
}
85100

86101
syncWithStore() {
87-
const state = getStore().getState();
102+
const state: MapStoreState = getStore().getState();
88103
const layerList = getLayerListRaw(state);
89104
const layerListConfigOnly = copyPersistentState(layerList);
90105
this.layerListJSON = JSON.stringify(layerListConfigOnly);

x-pack/plugins/maps/public/selectors/map_selectors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { ISource } from '../classes/sources/source';
5252
import { ITMSSource } from '../classes/sources/tms_source';
5353
import { IVectorSource } from '../classes/sources/vector_source';
5454
import { ILayer } from '../classes/layers/layer';
55+
import { ISavedGisMap } from '../routing/bootstrap/services/saved_gis_map';
5556

5657
function createLayerInstance(
5758
layerDescriptor: LayerDescriptor,
@@ -419,12 +420,11 @@ export const areLayersLoaded = createSelector(
419420

420421
export function hasUnsavedChanges(
421422
state: MapStoreState,
422-
savedMap: unknown,
423+
savedMap: ISavedGisMap,
423424
initialLayerListConfig: LayerDescriptor[]
424425
) {
425426
const layerListConfigOnly = copyPersistentState(getLayerListRaw(state));
426427

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

430430
return !savedLayerList

0 commit comments

Comments
 (0)