Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[feat] Introduced updateDatasetProps to update dataset information (#2133)
Igor Dykhta953711acd116
diff --git a/src/actions/src/action-types.ts b/src/actions/src/action-types.tsindex b350b331b..e31ea26af 100644--- a/src/actions/src/action-types.ts+++ b/src/actions/src/action-types.ts@@ -85,6 +85,7 @@ const ActionTypes = {UPDATE_OVERLAY_BLENDING: `${ACTION_PREFIX}UPDATE_OVERLAY_BLENDING`,UPDATE_VIS_DATA: `${ACTION_PREFIX}UPDATE_VIS_DATA`,RENAME_DATASET: `${ACTION_PREFIX}RENAME_DATASET`,+ UPDATE_DATASET_PROPS: `${ACTION_PREFIX}UPDATE_DATASET_PROPS`,TOGGLE_FILTER_ANIMATION: `${ACTION_PREFIX}TOGGLE_FILTER_ANIMATION`,UPDATE_FILTER_ANIMATION_SPEED: `${ACTION_PREFIX}UPDATE_FILTER_ANIMATION_SPEED`,PLAY_ANIMATION: `${ACTION_PREFIX}PLAY_ANIMATION`,diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.tsindex b442f5082..2cdd3567a 100644--- a/src/actions/src/vis-state-actions.ts+++ b/src/actions/src/vis-state-actions.ts@@ -686,6 +686,35 @@ export function renameDataset(};}+export type UpdateDatasetPropsUpdaterAction = {+ dataId: string;+ props: {+ label?: string;+ color?: RGBColor;+ metadata?: Record<string, unknown>;+ };+};+/**+ * Update an existing dataset props in `visState`+ * @param dataId - ***required** Id of the dataset to update+ * @param props - ***required** New props to update+ * @returns action+ */+export function updateDatasetProps(+ dataId: string,+ props: {+ label?: string;+ color?: RGBColor;+ metadata?: Record<string, unknown>;+ }+): Merge<UpdateDatasetPropsUpdaterAction, {type: typeof ActionTypes.UPDATE_DATASET_PROPS}> {+ return {+ type: ActionTypes.UPDATE_DATASET_PROPS,+ dataId,+ props+ };+}+export type ToggleFilterAnimationUpdaterAction = {idx: number;};diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.tsindex 96c523bf4..703ce484d 100644--- a/src/reducers/src/vis-state-updaters.ts+++ b/src/reducers/src/vis-state-updaters.ts@@ -25,6 +25,7 @@ import uniq from 'lodash.uniq';import get from 'lodash.get';import xor from 'lodash.xor';import copy from 'copy-to-clipboard';+import deepmerge from 'deepmerge';// Tasksimport {LOAD_FILE_TASK, UNWRAP_TASK, PROCESS_FILE_DATA, DELAY_TASK} from '@kepler.gl/tasks';// Actions@@ -47,6 +48,7 @@ import {toArray,arrayInsert,generateHashId,+ isPlainObject,addNewLayersToSplitMap,computeSplitMapLayers,removeLayerFromSplitMaps,@@ -1364,22 +1366,7 @@ export const updateTableColorUpdater = (state: VisState,action: VisStateActions.UpdateDatasetColorUpdater): VisState => {- const {dataId, newColor} = action;- const {datasets} = state;-- if (isRgbColor(newColor)) {- const existing = datasets[dataId];- existing.updateTableColor(newColor);-- return {- ...state,- datasets: {- ...state.datasets,- [dataId]: copyTableAndUpdate(existing, {})- }- };- }- return state;+ return updateDatasetPropsUpdater(state, {dataId: action.dataId, props: {color: action.newColor}});};/**@@ -1691,22 +1678,65 @@ export function renameDatasetUpdater(state: VisState,action: VisStateActions.RenameDatasetUpdaterAction): VisState {- const {dataId, label} = action;+ return updateDatasetPropsUpdater(state, {dataId: action.dataId, props: {label: action.label}});+}++const ALLOWED_UPDATE_DATASET_PROPS = ['label', 'color', 'metadata'];++/**+ * Validates properties before updating the dataset.+ * Makes sure each property is in the allowed list+ * Makes sure color value is RGB+ * Performs deep merge when updating metadata+ */+const validateDatasetUpdateProps = (props, dataset) => {+ const validatedProps = Object.entries(props).reduce((acc, entry) => {+ const [key, value] = entry;+ // is it allowed ?+ if (!ALLOWED_UPDATE_DATASET_PROPS.includes(key)) {+ return acc;+ }++ // if we are adding a color but it is not RGB we don't accept the value+ // in the future as we add more props we should change this if into a switch+ if (key === 'color' && !isRgbColor(value)) {+ return acc;+ }++ // do we need deep merge ?+ return {...acc, [key]: isPlainObject(value) ? deepmerge(dataset[key] || {}, value) : value};+ }, {});++ return validatedProps;+};++/**+ * Update Dataset props (label, color, meta). Do not use to update data or any related properties+ * @memberof visStateUpdaters+ * @public+ */+export function updateDatasetPropsUpdater(+ state: VisState,+ action: VisStateActions.UpdateDatasetPropsUpdaterAction+): VisState {+ const {dataId, props} = action;const {datasets} = state;const existing = datasets[dataId];if (existing) {- const newDataset = copyTableAndUpdate(existing, {label});+ const validatedProps = validateDatasetUpdateProps(props, existing);+ // validate props: just color for now+ // we only allow label, color and meta to be updated+ // const newTable = copyTableAndUpdate(existing, validatedProps);return {...state,datasets: {...datasets,- [dataId]: newDataset+ [dataId]: copyTableAndUpdate(existing, validatedProps)}};}- // No-op if the dataset doesn't existreturn state;}diff --git a/src/reducers/src/vis-state.ts b/src/reducers/src/vis-state.tsindex 817f7500f..b14166e07 100644--- a/src/reducers/src/vis-state.ts+++ b/src/reducers/src/vis-state.ts@@ -120,6 +120,8 @@ const actionHandler = {[ActionTypes.RENAME_DATASET]: visStateUpdaters.renameDatasetUpdater,+ [ActionTypes.UPDATE_DATASET_PROPS]: visStateUpdaters.updateDatasetPropsUpdater,+[ActionTypes.SET_FEATURES]: visStateUpdaters.setFeaturesUpdater,[ActionTypes.DELETE_FEATURE]: visStateUpdaters.deleteFeatureUpdater,diff --git a/test/node/reducers/vis-state-test.js b/test/node/reducers/vis-state-test.jsindex 211321c4d..a860fd3f7 100644--- a/test/node/reducers/vis-state-test.js+++ b/test/node/reducers/vis-state-test.js@@ -2139,6 +2139,79 @@ test('#visStateReducer -> RENAME_DATASET', t => {t.end();});+test('#visStateReducer -> UPDATE_COLOR_TABLE', t => {+ const initialState = StateWTripGeojson.visState;++ t.deepEqual(+ initialState.datasets[tripDataInfo.id].color,+ [192, 108, 132],+ 'Initial color as expected'+ );++ const newColor = [255, 255, 255];+ const updated = reducer(+ initialState,+ VisStateActions.updateTableColor(tripDataInfo.id, newColor)+ );++ assertDatasetIsTable(t, updated.datasets[tripDataInfo.id]);+ t.equal(updated.datasets[tripDataInfo.id].color, newColor, 'Updated color as expected');++ t.end();+});++test('#visStateReducer -> UPDATE_TABLE_PROPS', t => {+ const initialState = StateWTripGeojson.visState;++ // update label+ t.equal(+ initialState.datasets[tripDataInfo.id].label,+ tripDataInfo.label,+ 'Initial label as expected'+ );++ const newLabel = 'New label!!!11';+ let updated = reducer(+ initialState,+ VisStateActions.updateDatasetProps(tripDataInfo.id, {label: newLabel})+ );++ assertDatasetIsTable(t, updated.datasets[tripDataInfo.id]);+ t.equal(updated.datasets[tripDataInfo.id].label, newLabel, 'Updated label as expected');++ // update color+ t.deepEqual(+ updated.datasets[tripDataInfo.id].color,+ [192, 108, 132],+ 'Initial color as expected'+ );++ const newColor = [255, 255, 255];+ updated = reducer(+ updated,+ VisStateActions.updateDatasetProps(tripDataInfo.id, {color: newColor})+ );+ assertDatasetIsTable(t, updated.datasets[tripDataInfo.id]);+ t.equal(updated.datasets[tripDataInfo.id].label, newLabel, 'Updated color as expected');++ // update meta+ updated = reducer(+ updated,+ VisStateActions.updateDatasetProps(tripDataInfo.id, {metadata: {test: true}})+ );+ assertDatasetIsTable(t, updated.datasets[tripDataInfo.id]);+ t.deepEqual(+ updated.datasets[tripDataInfo.id].metadata,+ {+ ...updated.datasets[tripDataInfo.id].metadata,+ test: true+ },+ 'Updated color as expected'+ );++ t.end();+});+test('#visStateReducer -> SET_FILTER.name', t => {const oldState = CloneDeep(StateWFilters.visState);const oldFilter0 = oldState.filters[0];@@ -4709,22 +4782,6 @@ test('#visStateReducer -> SORT_TABLE_COLUMN', t => {t.end();});-test('#visStateReducer -> updateTableColor', t => {- const initialState = CloneDeep(StateWFiles.visState);- const newColor = [150, 150, 150];-- const nextState = reducer(- initialState,- VisStateActions.updateTableColor(testCsvDataId, newColor)- );-- // test dataset is table- assertDatasetIsTable(t, nextState.datasets[testCsvDataId]);- t.deepEqual(nextState.datasets[testCsvDataId].color, newColor, 'should update dataset color');-- t.end();-});-test('#visStateReducer -> PIN_TABLE_COLUMN', t => {const initialState = CloneDeep(StateWFiles.visState);