mirror of
https://github.com/apache/superset.git
synced 2026-04-25 11:04:48 +00:00
refactor(Dashboard): Native filters form update endpoint (#30609)
This commit is contained in:
@@ -16,7 +16,8 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import { DataMask, FilterConfiguration, Filters } from '@superset-ui/core';
|
||||
import { DataMask, Filters } from '@superset-ui/core';
|
||||
import { SaveFilterChangesType } from 'src/dashboard/components/nativeFilters/FiltersConfigModal/types';
|
||||
import { getInitialDataMask } from './reducer';
|
||||
|
||||
export const CLEAR_DATA_MASK_STATE = 'CLEAR_DATA_MASK_STATE';
|
||||
@@ -37,32 +38,25 @@ export interface INITDATAMASK {
|
||||
dataMask: DataMask;
|
||||
}
|
||||
|
||||
export const SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE =
|
||||
'SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE';
|
||||
|
||||
export interface SetDataMaskForFilterConfigComplete {
|
||||
type: typeof SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE;
|
||||
filterConfig: FilterConfiguration;
|
||||
export const SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE =
|
||||
'SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE';
|
||||
export interface SetDataMaskForFilterChangesComplete {
|
||||
type: typeof SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE;
|
||||
filterChanges: SaveFilterChangesType;
|
||||
filters?: Filters;
|
||||
}
|
||||
|
||||
export const SET_DATA_MASK_FOR_FILTER_CONFIG_FAIL =
|
||||
'SET_DATA_MASK_FOR_FILTER_CONFIG_FAIL';
|
||||
|
||||
export interface SetDataMaskForFilterConfigFail {
|
||||
type: typeof SET_DATA_MASK_FOR_FILTER_CONFIG_FAIL;
|
||||
filterConfig: FilterConfiguration;
|
||||
}
|
||||
export function setDataMaskForFilterConfigComplete(
|
||||
filterConfig: FilterConfiguration,
|
||||
export function setDataMaskForFilterChangesComplete(
|
||||
filterChanges: SaveFilterChangesType,
|
||||
filters?: Filters,
|
||||
): SetDataMaskForFilterConfigComplete {
|
||||
): SetDataMaskForFilterChangesComplete {
|
||||
return {
|
||||
type: SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE,
|
||||
filterConfig,
|
||||
type: SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE,
|
||||
filterChanges,
|
||||
filters,
|
||||
};
|
||||
}
|
||||
|
||||
export function updateDataMask(
|
||||
filterId: string | number,
|
||||
dataMask: DataMask,
|
||||
@@ -87,5 +81,4 @@ export function clearDataMaskState(): ClearDataMaskState {
|
||||
export type AnyDataMaskAction =
|
||||
| ClearDataMaskState
|
||||
| UpdateDataMask
|
||||
| SetDataMaskForFilterConfigFail
|
||||
| SetDataMaskForFilterConfigComplete;
|
||||
| SetDataMaskForFilterChangesComplete;
|
||||
|
||||
@@ -32,10 +32,11 @@ import {
|
||||
} from '@superset-ui/core';
|
||||
import { NATIVE_FILTER_PREFIX } from 'src/dashboard/components/nativeFilters/FiltersConfigModal/utils';
|
||||
import { HYDRATE_DASHBOARD } from 'src/dashboard/actions/hydrate';
|
||||
import { SaveFilterChangesType } from 'src/dashboard/components/nativeFilters/FiltersConfigModal/types';
|
||||
import {
|
||||
AnyDataMaskAction,
|
||||
CLEAR_DATA_MASK_STATE,
|
||||
SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE,
|
||||
SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE,
|
||||
UPDATE_DATA_MASK,
|
||||
} from './actions';
|
||||
import { areObjectsEqual } from '../reduxUtils';
|
||||
@@ -100,6 +101,37 @@ function fillNativeFilters(
|
||||
});
|
||||
}
|
||||
|
||||
function updateDataMaskForFilterChanges(
|
||||
filterChanges: SaveFilterChangesType,
|
||||
mergedDataMask: DataMaskStateWithId,
|
||||
draftDataMask: DataMaskStateWithId,
|
||||
initialDataMask?: Filters,
|
||||
) {
|
||||
const dataMask = initialDataMask || {};
|
||||
|
||||
Object.entries(dataMask).forEach(([key, value]) => {
|
||||
mergedDataMask[key] = { ...value, ...value.defaultDataMask };
|
||||
});
|
||||
|
||||
filterChanges.deleted.forEach((filterId: string) => {
|
||||
delete mergedDataMask[filterId];
|
||||
});
|
||||
|
||||
filterChanges.modified.forEach((filter: Filter) => {
|
||||
mergedDataMask[filter.id] = {
|
||||
...getInitialDataMask(filter.id),
|
||||
...filter.defaultDataMask,
|
||||
...filter,
|
||||
};
|
||||
});
|
||||
|
||||
Object.values(draftDataMask).forEach(filter => {
|
||||
if (!String(filter?.id).startsWith(NATIVE_FILTER_PREFIX)) {
|
||||
mergedDataMask[filter?.id] = filter;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const dataMaskReducer = produce(
|
||||
(draft: DataMaskStateWithId, action: AnyDataMaskAction) => {
|
||||
const cleanState = {};
|
||||
@@ -136,15 +168,14 @@ const dataMaskReducer = produce(
|
||||
action.data.dataMask,
|
||||
);
|
||||
return cleanState;
|
||||
case SET_DATA_MASK_FOR_FILTER_CONFIG_COMPLETE:
|
||||
fillNativeFilters(
|
||||
action.filterConfig ?? [],
|
||||
case SET_DATA_MASK_FOR_FILTER_CHANGES_COMPLETE:
|
||||
updateDataMaskForFilterChanges(
|
||||
action.filterChanges,
|
||||
cleanState,
|
||||
draft,
|
||||
action.filters,
|
||||
);
|
||||
return cleanState;
|
||||
|
||||
default:
|
||||
return draft;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user