All files / javascripts/explorev2/reducers exploreReducer.js

37.5% Statements 21/56
25% Branches 2/8
18.18% Functions 4/22
36.36% Lines 20/55
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 145 146 147 148 149 150 151 152 153 154 155 156 157  1x 1x 1x 1x 1x   1x 3x                                                                             1x 1x         1x                                           2x 2x             2x   2x 2x 2x 2x 2x                                                                                                                           3x 3x        
/* eslint camelcase: 0 */
import { defaultFormData } from '../stores/store';
import * as actions from '../actions/exploreActions';
import { addToArr, removeFromArr, alterInArr } from '../../../utils/reducerUtils';
import { now } from '../../modules/dates';
import { getExploreUrl } from '../exploreUtils';
 
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_DATASOURCE]() {
      return Object.assign({}, state, { datasource: action.datasource });
    },
    [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]() {
      let newFormData = Object.assign({}, state.viz.form_data);
      Iif (action.fieldName === 'datasource') {
        newFormData = defaultFormData(state.viz.form_data.viz_type, action.datasource_type);
        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;
      }
      newFormData[action.fieldName] = action.value;
 
      const fields = Object.assign({}, state.fields);
      const field = fields[action.fieldName];
      field.value = action.value;
      field.validationErrors = action.validationErrors;
      return Object.assign(
        {},
        state,
        {
          fields,
          viz: Object.assign({}, state.viz, { form_data: newFormData }),
        }
      );
    },
    [actions.CHART_UPDATE_SUCCEEDED]() {
      return Object.assign(
        {},
        state,
        {
          chartStatus: 'success',
          viz: Object.assign({}, state.viz, { query: action.query }),
        }
      );
    },
    [actions.CHART_UPDATE_STARTED]() {
      const chartUpdateStartTime = now();
      const form_data = Object.assign({}, state.viz.form_data);
      const datasource_type = state.datasource_type;
      const vizUpdates = {
        json_endpoint: getExploreUrl(form_data, datasource_type, 'json'),
        csv_endpoint: getExploreUrl(form_data, datasource_type, 'csv'),
        standalone_endpoint:
          getExploreUrl(form_data, datasource_type, 'standalone'),
      };
      return Object.assign({}, state,
        {
          chartStatus: 'loading',
          chartUpdateEndTime: null,
          chartUpdateStartTime,
          viz: Object.assign({}, state.viz, vizUpdates),
        });
    },
    [actions.CHART_UPDATE_FAILED]() {
      return Object.assign({}, state, {
        chartStatus: 'failed',
        chartAlert: action.error,
        chartUpdateEndTime: now(),
        viz: Object.assign({}, state.viz, { query: action.query }),
      });
    },
    [actions.UPDATE_CHART_STATUS]() {
      const newState = Object.assign({}, state, { chartStatus: action.status });
      if (action.status === 'success' || action.status === 'failed') {
        newState.chartUpdateEndTime = now();
      }
      return newState;
    },
    [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;
};