export default function dashboardStateReducer()

in superset-frontend/src/dashboard/reducers/dashboardState.js [56:280]


export default function dashboardStateReducer(state = {}, action) {
  const actionHandlers = {
    [HYDRATE_DASHBOARD]() {
      return { ...state, ...action.data.dashboardState };
    },
    [UPDATE_CSS]() {
      return { ...state, css: action.css };
    },
    [ADD_SLICE]() {
      const updatedSliceIds = new Set(state.sliceIds);
      updatedSliceIds.add(action.slice.slice_id);
      return {
        ...state,
        sliceIds: Array.from(updatedSliceIds),
      };
    },
    [REMOVE_SLICE]() {
      const { sliceId } = action;
      const updatedSliceIds = new Set(state.sliceIds);
      updatedSliceIds.delete(sliceId);

      return {
        ...state,
        sliceIds: Array.from(updatedSliceIds),
      };
    },
    [TOGGLE_FAVE_STAR]() {
      return { ...state, isStarred: action.isStarred };
    },
    [TOGGLE_PUBLISHED]() {
      return { ...state, isPublished: action.isPublished };
    },
    [SET_EDIT_MODE]() {
      return {
        ...state,
        editMode: action.editMode,
      };
    },
    [SET_MAX_UNDO_HISTORY_EXCEEDED]() {
      const { maxUndoHistoryExceeded = true } = action.payload;
      return { ...state, maxUndoHistoryExceeded };
    },
    [SHOW_BUILDER_PANE]() {
      return { ...state };
    },
    [SET_COLOR_SCHEME]() {
      return {
        ...state,
        colorScheme: action.colorScheme,
        updatedColorScheme: true,
      };
    },
    [SET_DASHBOARD_LABELS_COLORMAP_SYNCABLE]() {
      return {
        ...state,
        labelsColorMapMustSync: true,
      };
    },
    [SET_DASHBOARD_LABELS_COLORMAP_SYNCED]() {
      return {
        ...state,
        labelsColorMapMustSync: false,
      };
    },
    [SET_DASHBOARD_SHARED_LABELS_COLORS_SYNCABLE]() {
      return {
        ...state,
        sharedLabelsColorsMustSync: true,
      };
    },
    [SET_DASHBOARD_SHARED_LABELS_COLORS_SYNCED]() {
      return {
        ...state,
        sharedLabelsColorsMustSync: false,
      };
    },
    [TOGGLE_EXPAND_SLICE]() {
      const updatedExpandedSlices = { ...state.expandedSlices };
      const { sliceId } = action;
      if (updatedExpandedSlices[sliceId]) {
        delete updatedExpandedSlices[sliceId];
      } else {
        updatedExpandedSlices[sliceId] = true;
      }
      return { ...state, expandedSlices: updatedExpandedSlices };
    },
    [ON_CHANGE]() {
      return { ...state, hasUnsavedChanges: true };
    },
    [SAVE_DASHBOARD_STARTED]() {
      return {
        ...state,
        dashboardIsSaving: true,
      };
    },
    [SAVE_DASHBOARD_FINISHED]() {
      return {
        ...state,
        dashboardIsSaving: false,
      };
    },
    [ON_SAVE]() {
      return {
        ...state,
        hasUnsavedChanges: false,
        maxUndoHistoryExceeded: false,
        editMode: false,
        updatedColorScheme: false,
        // server-side returns last_modified_time for latest change
        lastModifiedTime: action.lastModifiedTime,
      };
    },
    [SET_UNSAVED_CHANGES]() {
      const { hasUnsavedChanges } = action.payload;
      return { ...state, hasUnsavedChanges };
    },
    [SET_REFRESH_FREQUENCY]() {
      return {
        ...state,
        refreshFrequency: action.refreshFrequency,
        shouldPersistRefreshFrequency: action.isPersistent,
        hasUnsavedChanges: action.isPersistent,
      };
    },
    [ON_REFRESH]() {
      return {
        ...state,
        isRefreshing: true,
      };
    },
    [ON_FILTERS_REFRESH]() {
      return {
        ...state,
        isFiltersRefreshing: true,
      };
    },
    [ON_FILTERS_REFRESH_SUCCESS]() {
      return {
        ...state,
        isFiltersRefreshing: false,
      };
    },
    [ON_REFRESH_SUCCESS]() {
      return {
        ...state,
        isRefreshing: false,
      };
    },
    [SET_DIRECT_PATH]() {
      return {
        ...state,
        directPathToChild: action.path,
        directPathLastUpdated: Date.now(),
      };
    },
    [SET_ACTIVE_TAB]() {
      const newActiveTabs = new Set(state.activeTabs).difference(
        new Set(action.inactiveTabs.concat(action.prevTabId)),
      );
      const newInactiveTabs = new Set(state.inactiveTabs)
        .difference(new Set(action.activeTabs))
        .union(new Set(action.inactiveTabs));

      return {
        ...state,
        inactiveTabs: Array.from(newInactiveTabs),
        activeTabs: Array.from(newActiveTabs.union(new Set(action.activeTabs))),
      };
    },
    [SET_ACTIVE_TABS]() {
      return {
        ...state,
        activeTabs: action.activeTabs,
      };
    },
    [SET_OVERRIDE_CONFIRM]() {
      return {
        ...state,
        overwriteConfirmMetadata: action.overwriteConfirmMetadata,
      };
    },
    [SET_FOCUSED_FILTER_FIELD]() {
      return {
        ...state,
        focusedFilterField: {
          chartId: action.chartId,
          column: action.column,
        },
      };
    },
    [UNSET_FOCUSED_FILTER_FIELD]() {
      // dashboard only has 1 focused filter field at a time,
      // but when user switch different filter boxes,
      // browser didn't always fire onBlur and onFocus events in order.
      if (
        !state.focusedFilterField ||
        action.chartId !== state.focusedFilterField.chartId ||
        action.column !== state.focusedFilterField.column
      ) {
        return state;
      }
      return {
        ...state,
        focusedFilterField: null,
      };
    },
    [SET_FULL_SIZE_CHART_ID]() {
      return {
        ...state,
        fullSizeChartId: action.chartId,
      };
    },
    [SET_DATASETS_STATUS]() {
      return {
        ...state,
        datasetsStatus: action.status,
      };
    },
  };

  if (action.type in actionHandlers) {
    return actionHandlers[action.type]();
  }
  return state;
}