Files
superset2/superset/assets/javascripts/explorev2/reducers/exploreReducer.js
vera-liu a8480f5492 Added Alert for ControlPanel and ChartContainer (#1626)
* Added Alert for ControlPanel and ChartContainer

Done:
 - Add alert for Control Panel when fetch_datasource_metadata failes
 - Add alert for Chart Container when update_explore query fails

* Changed color to warning-yellow

* Solve linter issue

* Fixed indent and delete error_redirect
2016-11-18 11:17:06 -08:00

108 lines
3.7 KiB
JavaScript

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.SET_FIELD_OPTIONS]() {
const newState = Object.assign({}, state);
const optionsByFieldName = action.options;
const fieldNames = Object.keys(optionsByFieldName);
fieldNames.forEach((fieldName) => {
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]() {
return addToArr(state, 'filters', action.filter);
},
[actions.REMOVE_FILTER]() {
return removeFromArr(state, 'filters', action.filter);
},
[actions.CHANGE_FILTER_FIELD]() {
return alterInArr(state, 'filters', action.filter, { field: action.field });
},
[actions.CHANGE_FILTER_OP]() {
return alterInArr(state, 'filters', action.filter, { op: action.op });
},
[actions.CHANGE_FILTER_VALUE]() {
return alterInArr(state, 'filters', action.filter, { value: action.value });
},
[actions.SET_FIELD_VALUE]() {
const newFormData = action.key === 'datasource' ?
defaultFormData(state.viz.form_data.viz_type) : Object.assign({}, state.viz.form_data);
if (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;
}
newFormData[action.key] = action.value ? 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 });
},
};
if (action.type in actionHandlers) {
return actionHandlers[action.type]();
}
return state;
};