All files / javascripts/explorev2/reducers exploreReducer.js

31.37% Statements 16/51
50% Branches 6/12
18.18% Functions 4/22
30% Lines 15/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 1451x 1x 1x   1x 3x                                                                                                         1x 1x         1x                                           2x     2x           2x     2x   2x                                                                             3x 3x        
import { defaultFormData } from '../stores/store';
import * as actions from '../actions/exploreActions';
import { addToArr, removeFromArr, alterInArr } from '../../../utils/reducerUtils';
 
export const exploreReducer = function (state, action) {
  const actionHandlers = {
    [actions.TOGGLE_FAVE_STAR]() {
      return Object.assign({}, state, { isStarred: action.isStarred });
    },
 
    [actions.FETCH_STARTED]() {
      return Object.assign({}, state, { isDatasourceMetaLoading: true });
    },
 
    [actions.FETCH_SUCCEEDED]() {
      return Object.assign({}, state, { isDatasourceMetaLoading: false });
    },
 
    [actions.FETCH_FAILED]() {
      // todo(alanna) handle failure/error state
      return Object.assign({}, state,
        {
          isDatasourceMetaLoading: false,
          controlPanelAlert: action.error,
        });
    },
    [actions.REMOVE_CONTROL_PANEL_ALERT]() {
      return Object.assign({}, state, { controlPanelAlert: null });
    },
 
    [actions.FETCH_DASHBOARDS_SUCCEEDED]() {
      return Object.assign({}, state, { dashboards: action.choices });
    },
 
    [actions.FETCH_DASHBOARDS_FAILED]() {
      return Object.assign({}, state,
        { saveModalAlert: `fetching dashboards failed for ${action.userId}` });
    },
 
    [actions.SET_FIELD_OPTIONS]() {
      const newState = Object.assign({}, state);
      const optionsByFieldName = action.options;
      const fieldNames = Object.keys(optionsByFieldName);
 
      fieldNames.forEach((fieldName) => {
        if (fieldName === 'filterable_cols') {
          newState.filterColumnOpts = optionsByFieldName[fieldName];
        } else {
          newState.fields[fieldName].choices = optionsByFieldName[fieldName];
        }
      });
      return Object.assign({}, state, newState);
    },
 
    [actions.SET_FILTER_COLUMN_OPTS]() {
      return Object.assign({}, state, { filterColumnOpts: action.filterColumnOpts });
    },
    [actions.ADD_FILTER]() {
      const newFormData = addToArr(state.viz.form_data, 'filters', action.filter);
      const newState = Object.assign(
        {},
        state,
        { viz: Object.assign({}, state.viz, { form_data: newFormData }) }
      );
      return newState;
    },
    [actions.REMOVE_FILTER]() {
      const newFormData = removeFromArr(state.viz.form_data, 'filters', action.filter);
      return Object.assign(
        {},
        state,
        { viz: Object.assign({}, state.viz, { form_data: newFormData }) }
      );
    },
    [actions.CHANGE_FILTER]() {
      const changes = {};
      changes[action.field] = action.value;
      const newFormData = alterInArr(
        state.viz.form_data, 'filters', action.filter, changes);
      return Object.assign(
        {},
        state,
        { viz: Object.assign({}, state.viz, { form_data: newFormData }) }
      );
    },
    [actions.SET_FIELD_VALUE]() {
      const newFormData = action.key === 'datasource' ?
        defaultFormData(state.viz.form_data.viz_type, action.datasource_type) :
        Object.assign({}, state.viz.form_data);
      Iif (action.key === 'datasource') {
        newFormData.datasource_name = action.label;
        newFormData.slice_id = state.viz.form_data.slice_id;
        newFormData.slice_name = state.viz.form_data.slice_name;
        newFormData.viz_type = state.viz.form_data.viz_type;
      }
      Iif (action.key === 'viz_type') {
        newFormData.previous_viz_type = state.viz.form_data.viz_type;
      }
      newFormData[action.key] = (action.value !== undefined)
        ? action.value : (!state.viz.form_data[action.key]);
      return Object.assign(
        {},
        state,
        { viz: Object.assign({}, state.viz, { form_data: newFormData }) }
      );
    },
    [actions.UPDATE_CHART]() {
      const vizUpdates = {
        column_formats: action.viz.column_formats,
        json_endpoint: action.viz.json_endpoint,
        csv_endpoint: action.viz.csv_endpoint,
        standalone_endpoint: action.viz.standalone_endpoint,
        query: action.viz.query,
        data: action.viz.data,
      };
      return Object.assign(
        {},
        state,
        {
          viz: Object.assign({}, state.viz, vizUpdates),
          isChartLoading: false,
        });
    },
    [actions.CHART_UPDATE_STARTED]() {
      return Object.assign({}, state, { isChartLoading: true });
    },
    [actions.CHART_UPDATE_FAILED]() {
      return Object.assign({}, state, { isChartLoading: false, chartAlert: action.error });
    },
    [actions.REMOVE_CHART_ALERT]() {
      return Object.assign({}, state, { chartAlert: null });
    },
    [actions.SAVE_SLICE_FAILED]() {
      return Object.assign({}, state, { saveModalAlert: 'Failed to save slice' });
    },
    [actions.REMOVE_SAVE_MODAL_ALERT]() {
      return Object.assign({}, state, { saveModalAlert: null });
    },
  };
  Eif (action.type in actionHandlers) {
    return actionHandlers[action.type]();
  }
  return state;
};