Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

feat: include locale in exported map and restore on open (#3407)

Igor Dykhtaaff793013bb9
diff --git a/src/reducers/src/combined-updaters.ts b/src/reducers/src/combined-updaters.ts
index 7b47783e5..0ccb6e091 100644
--- a/src/reducers/src/combined-updaters.ts
+++ b/src/reducers/src/combined-updaters.ts
@@ -4,10 +4,9 @@
import {
toggleModalUpdater,
loadFilesSuccessUpdater as uiStateLoadFilesSuccessUpdater,
- setMapControlSettingsUpdater as uiStateSetMapControlSettingsUpdater,
- toggleMapControlUpdater as uiStateToggleMapControlUpdater,
toggleMapControlUpdater,
- toggleSplitMapUpdater as uiStateToggleSplitMapUpdater
+ toggleSplitMapUpdater as uiStateToggleSplitMapUpdater,
+ receiveMapConfigUpdater as uiStateReceiveMapConfigUpdater
} from './ui-state-updaters';
import {
updateVisDataUpdater as visStateUpdateVisDataUpdater,
@@ -207,27 +206,11 @@ export const addDataToMapUpdater = (
)
),
pick_('mapStyle')(apply_(styleMapConfigUpdater, payload_({config: parsedConfig, options}))),
- pick_('uiState')(apply_(uiStateLoadFilesSuccessUpdater, payload_(null))),
-
- if_(
- Boolean(parsedConfig?.uiState?.mapControls?.mapLegend?.active),
- pick_('uiState')(
- apply_(uiStateToggleMapControlUpdater, payload_({panelId: 'mapLegend', index: 0}))
- )
+ pick_('uiState')(
+ apply_(uiStateReceiveMapConfigUpdater, payload_({config: parsedConfig, options}))
),
+ pick_('uiState')(apply_(uiStateLoadFilesSuccessUpdater, payload_(null))),
- if_(
- Boolean(parsedConfig?.uiState?.mapControls?.mapLegend?.settings),
- pick_('uiState')(
- apply_(
- uiStateSetMapControlSettingsUpdater,
- payload_({
- panelId: 'mapLegend',
- settings: parsedConfig?.uiState?.mapControls?.mapLegend?.settings ?? {}
- })
- )
- )
- ),
pick_('uiState')(apply_(toggleModalUpdater, payload_(null))),
pick_('uiState')(
merge_(
diff --git a/src/reducers/src/ui-state-updaters.ts b/src/reducers/src/ui-state-updaters.ts
index 3325fbede..471ff2e61 100644
--- a/src/reducers/src/ui-state-updaters.ts
+++ b/src/reducers/src/ui-state-updaters.ts
@@ -26,6 +26,7 @@ import {
ActionTypes,
KeplerGlInitPayload,
LoadFilesErrUpdaterAction,
+ ReceiveMapConfigPayload,
UIStateActions
} from '@kepler.gl/actions';
import {
@@ -974,3 +975,59 @@ export const togglePanelListViewUpdater = (
[stateProp]: listView
};
};
+
+/**
+ * Merge received ui state config when loading a saved map
+ * @memberof uiStateUpdaters
+ * @param state `uiState`
+ * @param action
+ * @param action.payload saved map config `{mapStyle, visState, mapState, uiState}`
+ * @returns nextState
+ * @public
+ */
+export const receiveMapConfigUpdater = (
+ state: UiState,
+ {
+ payload: {config}
+ }: {
+ type?: (typeof ActionTypes)['RECEIVE_MAP_CONFIG'];
+ payload: ReceiveMapConfigPayload;
+ }
+): UiState => {
+ const {uiState} = config || {};
+ if (!uiState) {
+ return state;
+ }
+
+ let newState = state;
+
+ if (uiState.mapControls?.mapLegend?.active) {
+ const currentLegend = newState.mapControls.mapLegend;
+ newState = {
+ ...newState,
+ mapControls: {
+ ...newState.mapControls,
+ mapLegend: {
+ show: true,
+ ...currentLegend,
+ active: true,
+ activeMapIndex: 0
+ }
+ }
+ };
+ }
+
+ if (uiState.mapControls?.mapLegend?.settings) {
+ newState = setMapControlSettingsUpdater(newState, {
+ payload: {panelId: 'mapLegend', settings: uiState.mapControls.mapLegend.settings}
+ } as UIStateActions.setMapControlSettingsUpdaterAction);
+ }
+
+ if (uiState.locale) {
+ newState = setLocaleUpdater(newState, {
+ payload: {locale: uiState.locale}
+ } as UIStateActions.SetLocaleUpdaterAction);
+ }
+
+ return newState;
+};
diff --git a/src/reducers/src/ui-state.ts b/src/reducers/src/ui-state.ts
index 8b0f763da..b2193c00c 100644
--- a/src/reducers/src/ui-state.ts
+++ b/src/reducers/src/ui-state.ts
@@ -46,7 +46,8 @@ const actionHandler = {
[ActionTypes.TOGGLE_SPLIT_MAP]: uiStateUpdaters.toggleSplitMapUpdater,
[ActionTypes.SHOW_DATASET_TABLE]: uiStateUpdaters.showDatasetTableUpdater,
[ActionTypes.SET_LOCALE]: uiStateUpdaters.setLocaleUpdater,
- [ActionTypes.TOGGLE_PANEL_LIST_VIEW]: uiStateUpdaters.togglePanelListViewUpdater
+ [ActionTypes.TOGGLE_PANEL_LIST_VIEW]: uiStateUpdaters.togglePanelListViewUpdater,
+ [ActionTypes.RECEIVE_MAP_CONFIG]: uiStateUpdaters.receiveMapConfigUpdater
};
/* Reducer */
diff --git a/src/schemas/src/schema-manager.ts b/src/schemas/src/schema-manager.ts
index 593624866..7ea120d76 100644
--- a/src/schemas/src/schema-manager.ts
+++ b/src/schemas/src/schema-manager.ts
@@ -68,7 +68,11 @@ export type SavedMap = {
export type LoadedMap = {datasets?: ParsedDataset[] | null; config?: ParsedConfig | null};
export const reducerSchema: {
- [key: string]: typeof mapStateSchema | typeof visStateSchema | typeof mapStyleSchema;
+ [key: string]:
+ | typeof mapStateSchema
+ | typeof visStateSchema
+ | typeof mapStyleSchema
+ | typeof uiStateSchema;
} = {
visState: visStateSchema,
mapState: mapStateSchema,
diff --git a/src/schemas/src/ui-state-schema.ts b/src/schemas/src/ui-state-schema.ts
index 8204267c5..683c6c613 100644
--- a/src/schemas/src/ui-state-schema.ts
+++ b/src/schemas/src/ui-state-schema.ts
@@ -25,7 +25,8 @@ export const propertiesV1 = {
})
},
key: 'mapControls'
- })
+ }),
+ locale: null
};
const uiStateSchema = {
diff --git a/src/types/schemas.d.ts b/src/types/schemas.d.ts
index 0b8c1ca6e..81f8ec5a0 100644
--- a/src/types/schemas.d.ts
+++ b/src/types/schemas.d.ts
@@ -135,15 +135,21 @@ export type ParsedVisState = {
};
export type ParsedUiState = {
- mapControls: {
- mapLegend: {
- active: boolean;
- settings: {
- position: string;
- contentHeight: number;
+ mapControls?: {
+ mapLegend?: {
+ active?: boolean;
+ settings?: {
+ position?: {
+ x: number;
+ y: number;
+ anchorX: 'left' | 'right';
+ anchorY: 'top' | 'bottom';
+ };
+ contentHeight?: number;
};
};
};
+ locale?: string;
};
export type SavedMapState = {
diff --git a/test/node/reducers/composer-state-test.js b/test/node/reducers/composer-state-test.js
index 0fe174318..ece4c36f8 100644
--- a/test/node/reducers/composer-state-test.js
+++ b/test/node/reducers/composer-state-test.js
@@ -170,6 +170,84 @@ test('#composerStateReducer - addDataToMapUpdater: uiState', t => {
t.end();
});
+test('#composerStateReducer - addDataToMapUpdater: mapLegend', t => {
+ const state = keplerGlReducer(undefined, registerEntry({id: 'test'})).test;
+
+ t.equal(
+ state.uiState.mapControls.mapLegend.active,
+ false,
+ 'mapLegend should be inactive by default'
+ );
+
+ const newState = addDataToMapUpdater(state, {
+ payload: {
+ datasets: {
+ data: mockRawData,
+ info: {id: 'foo'}
+ },
+ config: {
+ uiState: {
+ mapControls: {
+ mapLegend: {
+ active: true,
+ settings: {
+ position: {x: 20, y: 40, anchorX: 'left', anchorY: 'top'},
+ contentHeight: 150
+ }
+ }
+ }
+ }
+ }
+ }
+ });
+
+ drainTasksForTesting();
+
+ t.equal(
+ newState.uiState.mapControls.mapLegend.active,
+ true,
+ 'mapLegend should be set to active from config'
+ );
+ t.deepEqual(
+ newState.uiState.mapControls.mapLegend.settings.position,
+ {x: 20, y: 40, anchorX: 'left', anchorY: 'top'},
+ 'mapLegend settings position should be set from config'
+ );
+ t.equal(
+ newState.uiState.mapControls.mapLegend.settings.contentHeight,
+ 150,
+ 'mapLegend settings contentHeight should be set from config'
+ );
+
+ t.end();
+});
+
+test('#composerStateReducer - addDataToMapUpdater: locale', t => {
+ const state = keplerGlReducer(undefined, registerEntry({id: 'test'})).test;
+
+ t.equal(state.uiState.locale, 'en', 'locale should be en by default');
+
+ const newState = addDataToMapUpdater(state, {
+ payload: {
+ datasets: {
+ data: mockRawData,
+ info: {id: 'foo'}
+ },
+ config: {
+ uiState: {
+ locale: 'es'
+ }
+ }
+ }
+ });
+
+ drainTasksForTesting();
+
+ t.equal(newState.uiState.locale, 'es', 'locale should be set to es from config');
+
+ t.end();
+});
+
test('#composerStateReducer - addDataToMapUpdater: keepExistingConfig', t => {
const data = processCsvData(testCsvData);
diff --git a/test/node/schemas/index.js b/test/node/schemas/index.js
index f4b7c75bc..d76474e64 100644
--- a/test/node/schemas/index.js
+++ b/test/node/schemas/index.js
@@ -7,3 +7,4 @@ import './map-state-schema-test';
import './map-style-schema-test';
import './dataset-schema-test';
import './schema-conversion-test';
+import './ui-state-schema-test';
diff --git a/test/node/schemas/ui-state-schema-test.js b/test/node/schemas/ui-state-schema-test.js
new file mode 100644
index 000000000..757ae11ce
--- /dev/null
+++ b/test/node/schemas/ui-state-schema-test.js
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import test from 'tape';
+import cloneDeep from 'lodash/cloneDeep';
+import SchemaManager from '@kepler.gl/schemas';
+import {InitialState} from 'test/helpers/mock-state';
+
+test('#uiStateSchema -> v1 -> save load uiState', t => {
+ const initialState = cloneDeep(InitialState);
+ const savedState = SchemaManager.getConfigToSave(initialState);
+
+ const uiToSave = savedState.config.uiState;
+ const uiLoaded = SchemaManager.parseSavedConfig(savedState).uiState;
+
+ t.deepEqual(
+ Object.keys(uiToSave).sort(),
+ ['locale', 'mapControls'].sort(),
+ 'saved uiState should have mapControls and locale'
+ );
+
+ t.equal(uiToSave.locale, 'en', 'default locale should be en');
+ t.deepEqual(
+ uiToSave.mapControls,
+ {mapLegend: {active: false}},
+ 'default mapLegend should be inactive with no settings'
+ );
+
+ t.deepEqual(uiLoaded, uiToSave, 'loaded uiState should match saved uiState');
+
+ t.end();
+});
+
+test('#uiStateSchema -> v1 -> save load uiState with mapLegend active and locale', t => {
+ const initialState = cloneDeep(InitialState);
+
+ initialState.uiState = {
+ ...initialState.uiState,
+ locale: 'es',
+ mapControls: {
+ ...initialState.uiState.mapControls,
+ mapLegend: {
+ ...initialState.uiState.mapControls.mapLegend,
+ active: true,
+ settings: {
+ position: {x: 10, y: 30, anchorX: 'right', anchorY: 'bottom'},
+ contentHeight: 200
+ }
+ }
+ }
+ };
+
+ const savedState = SchemaManager.getConfigToSave(initialState);
+ const uiToSave = savedState.config.uiState;
+
+ t.equal(uiToSave.locale, 'es', 'locale should be saved as es');
+ t.equal(uiToSave.mapControls.mapLegend.active, true, 'mapLegend active should be saved as true');
+ t.deepEqual(
+ uiToSave.mapControls.mapLegend.settings,
+ {position: {x: 10, y: 30, anchorX: 'right', anchorY: 'bottom'}, contentHeight: 200},
+ 'mapLegend settings should be saved'
+ );
+
+ const uiLoaded = SchemaManager.parseSavedConfig(savedState).uiState;
+ t.deepEqual(uiLoaded, uiToSave, 'loaded uiState should match saved uiState');
+
+ t.end();
+});