Skip to content

Commit

Permalink
[chore] Move create or update filter action (#2636)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
  • Loading branch information
igorDykhta authored Sep 13, 2024
1 parent 16a3ac2 commit 6ffb1dc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/actions/src/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const ActionTypes = {
// visState
ADD_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`,
Expand Down
29 changes: 29 additions & 0 deletions src/actions/src/vis-state-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 64 additions & 0 deletions src/reducers/src/vis-state-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 visStateUpdaters
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/src/vis-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6ffb1dc

Please sign in to comment.