Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
[chore] Move create or update filter action (#2636)
Igor Dykhta6ffb1dcb190e
diff --git a/src/actions/src/action-types.ts b/src/actions/src/action-types.tsindex 22d45a8be..875393883 100644--- a/src/actions/src/action-types.ts+++ b/src/actions/src/action-types.ts@@ -42,6 +42,7 @@ export const ActionTypes = {// visStateADD_DATA: `${ACTION_PREFIX}ADD_DATA`,ADD_FILTER: `${ACTION_PREFIX}ADD_FILTER`,+ CREATE_OR_UPDATE_FILTER: `${ACTION_PREFIX}CREATE_OR_UPDATE_FILTER`,ADD_LAYER: `${ACTION_PREFIX}ADD_LAYER`,APPLY_LAYER_CONFIG: `${ACTION_PREFIX}APPLY_LAYER_CONFIG`,DUPLICATE_LAYER: `${ACTION_PREFIX}DUPLICATE_LAYER`,diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.tsindex c03a4e81b..d7f256cbf 100644--- a/src/actions/src/vis-state-actions.ts+++ b/src/actions/src/vis-state-actions.ts@@ -401,6 +401,35 @@ export function addFilter(};}+export type CreateOrUpdateFilterUpdaterAction = {+ id?: string;+ dataId?: string | string[];+ field?: string | string[];+ value?: any;+};++/**+ * Create or updates a filter+ * @memberof visStateActions+ * @param dataId - dataset `id` this new filter is associated with+ * @returns action+ * @public+ */+export function createOrUpdateFilter(+ id?: string,+ dataId?: string | string[],+ field?: string | string[],+ value?: any+): Merge<CreateOrUpdateFilterUpdaterAction, {type: typeof ActionTypes.CREATE_OR_UPDATE_FILTER}> {+ return {+ type: ActionTypes.CREATE_OR_UPDATE_FILTER,+ id,+ dataId,+ field,+ value+ };+}+export type AddLayerUpdaterAction = {config?: object;datasetId?: string;diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.tsindex 8e01b6f5d..3729104e6 100644--- a/src/reducers/src/vis-state-updaters.ts+++ b/src/reducers/src/vis-state-updaters.ts@@ -1068,6 +1068,70 @@ export const addFilterUpdater = (filters: [...state.filters, getDefaultFilter({dataId: action.dataId, id: action.id})]};+/**+ * Create or update a filter+ * @memberof visStateUpdaters+ * @public+ */+export const createOrUpdateFilterUpdater = (+ state: VisState,+ action: VisStateActions.CreateOrUpdateFilterUpdaterAction+): VisState => {+ const {id, dataId, field, value} = action;++ let newState = state;+ const originalIndex = newState.filters.findIndex(f => f.id === id);+ let filterIndex = originalIndex;+ if (!id && !dataId) {+ return newState;+ }+ if (originalIndex < 0 && dataId) {+ newState = addFilterUpdater(newState, {dataId});+ if (newState.filters.length !== state.filters.length + 1) {+ // No new filter was added+ return state;+ }+ // Here we are assuming that the filter was added at the end+ filterIndex = newState.filters.length - 1;+ newState.filters[filterIndex] = {+ ...newState.filters[filterIndex],+ ...(id ? {id} : null)+ };+ }++ // No need to update this if it's a newly created filter+ // First we make sure all the dataIds that fields refer to are updated+ if (originalIndex >= 0 && dataId) {+ // If the dataId is an array, we need to update each one individually as they need a correct valueIndex passed+ newState = (Array.isArray(dataId) ? dataId : [dataId]).reduce((accu, d, index) => {+ return setFilterUpdater(accu, {+ idx: filterIndex,+ prop: 'dataId',+ value: d,+ valueIndex: index+ });+ }, newState);+ }+ // Then we update the fields+ if (field) {+ // If the field is an array, we need to update each field individually as they need a correct valueIndex passed+ newState = (Array.isArray(field) ? field : [field]).reduce((accu, f, index) => {+ return setFilterUpdater(accu, {+ idx: filterIndex,+ prop: 'name',+ value: f,+ valueIndex: index+ });+ }, newState);+ }+ // Then we update the value separately+ if (value !== null && typeof value !== 'undefined') {+ newState = setFilterUpdater(newState, {idx: filterIndex, prop: 'value', value});+ }++ return newState;+};+/*** Set layer color palette ui state* @memberof visStateUpdatersdiff --git a/src/reducers/src/vis-state.ts b/src/reducers/src/vis-state.tsindex bb46c8cd5..9d8bfa3e4 100644--- a/src/reducers/src/vis-state.ts+++ b/src/reducers/src/vis-state.ts@@ -12,6 +12,8 @@ import * as visStateUpdaters from './vis-state-updaters';const actionHandler = {[ActionTypes.ADD_FILTER]: visStateUpdaters.addFilterUpdater,+ [ActionTypes.CREATE_OR_UPDATE_FILTER]: visStateUpdaters.createOrUpdateFilterUpdater,+[ActionTypes.ADD_LAYER]: visStateUpdaters.addLayerUpdater,[ActionTypes.APPLY_LAYER_CONFIG]: visStateUpdaters.applyLayerConfigUpdater,