Skip to content

Commit

Permalink
[explore] Split large reducer logic in ExploreViewContainer (#3088)
Browse files Browse the repository at this point in the history
* split reducer logic for ExploreViewContainer

* fix saveModal component and unit tests

* revert changes in SaveModal_spec.
will make another commit just to improve test coverage for SaveModal component.

* remove comment-out code

* fix merge confilicts
  • Loading branch information
Grace Guo authored and mistercrunch committed Aug 10, 2017
1 parent 08b7e89 commit b3107bb
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 329 deletions.
70 changes: 70 additions & 0 deletions superset/assets/javascripts/explore/actions/chartActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getExploreUrl } from '../exploreUtils';
import { getFormDataFromControls } from '../stores/store';
import { QUERY_TIMEOUT_THRESHOLD } from '../../constants';
import { triggerQuery } from './exploreActions';

const $ = window.$ = require('jquery');

export const CHART_UPDATE_STARTED = 'CHART_UPDATE_STARTED';
export function chartUpdateStarted(queryRequest, latestQueryFormData) {
return { type: CHART_UPDATE_STARTED, queryRequest, latestQueryFormData };
}

export const CHART_UPDATE_SUCCEEDED = 'CHART_UPDATE_SUCCEEDED';
export function chartUpdateSucceeded(queryResponse) {
return { type: CHART_UPDATE_SUCCEEDED, queryResponse };
}

export const CHART_UPDATE_STOPPED = 'CHART_UPDATE_STOPPED';
export function chartUpdateStopped(queryRequest) {
if (queryRequest) {
queryRequest.abort();
}
return { type: CHART_UPDATE_STOPPED };
}

export const CHART_UPDATE_TIMEOUT = 'CHART_UPDATE_TIMEOUT';
export function chartUpdateTimeout(statusText) {
return { type: CHART_UPDATE_TIMEOUT, statusText };
}

export const CHART_UPDATE_FAILED = 'CHART_UPDATE_FAILED';
export function chartUpdateFailed(queryResponse) {
return { type: CHART_UPDATE_FAILED, queryResponse };
}

export const UPDATE_CHART_STATUS = 'UPDATE_CHART_STATUS';
export function updateChartStatus(status) {
return { type: UPDATE_CHART_STATUS, status };
}

export const CHART_RENDERING_FAILED = 'CHART_RENDERING_FAILED';
export function chartRenderingFailed(error) {
return { type: CHART_RENDERING_FAILED, error };
}

export const RUN_QUERY = 'RUN_QUERY';
export function runQuery(formData, force = false) {
return function (dispatch, getState) {
const { explore } = getState();
const lastQueryFormData = getFormDataFromControls(explore.controls);
const url = getExploreUrl(formData, 'json', force);
const queryRequest = $.ajax({
url,
dataType: 'json',
success(queryResponse) {
dispatch(chartUpdateSucceeded(queryResponse));
},
error(err) {
if (err.statusText === 'timeout') {
dispatch(chartUpdateTimeout(err.statusText));
} else if (err.statusText !== 'abort') {
dispatch(chartUpdateFailed(err.responseJSON));
}
},
timeout: QUERY_TIMEOUT_THRESHOLD,
});
dispatch(chartUpdateStarted(queryRequest, lastQueryFormData));
dispatch(triggerQuery(false));
};
}
123 changes: 2 additions & 121 deletions superset/assets/javascripts/explore/actions/exploreActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint camelcase: 0 */
import { getExploreUrl } from '../exploreUtils';
import { QUERY_TIMEOUT_THRESHOLD } from '../../constants';

const $ = window.$ = require('jquery');

Expand Down Expand Up @@ -37,8 +35,8 @@ export function resetControls() {
}

export const TRIGGER_QUERY = 'TRIGGER_QUERY';
export function triggerQuery() {
return { type: TRIGGER_QUERY };
export function triggerQuery(value = true) {
return { type: TRIGGER_QUERY, value };
}

export function fetchDatasourceMetadata(datasourceKey, alsoTriggerQuery = false) {
Expand Down Expand Up @@ -95,39 +93,6 @@ export function setControlValue(controlName, value, validationErrors) {
return { type: SET_FIELD_VALUE, controlName, value, validationErrors };
}

export const CHART_UPDATE_STARTED = 'CHART_UPDATE_STARTED';
export function chartUpdateStarted(queryRequest) {
return { type: CHART_UPDATE_STARTED, queryRequest };
}

export const CHART_UPDATE_SUCCEEDED = 'CHART_UPDATE_SUCCEEDED';
export function chartUpdateSucceeded(queryResponse) {
return { type: CHART_UPDATE_SUCCEEDED, queryResponse };
}

export const CHART_UPDATE_STOPPED = 'CHART_UPDATE_STOPPED';
export function chartUpdateStopped(queryRequest) {
if (queryRequest) {
queryRequest.abort();
}
return { type: CHART_UPDATE_STOPPED };
}

export const CHART_UPDATE_TIMEOUT = 'CHART_UPDATE_TIMEOUT';
export function chartUpdateTimeout(statusText) {
return { type: CHART_UPDATE_TIMEOUT, statusText };
}

export const CHART_UPDATE_FAILED = 'CHART_UPDATE_FAILED';
export function chartUpdateFailed(queryResponse) {
return { type: CHART_UPDATE_FAILED, queryResponse };
}

export const CHART_RENDERING_FAILED = 'CHART_RENDERING_FAILED';
export function chartRenderingFailed(error) {
return { type: CHART_RENDERING_FAILED, error };
}

export const UPDATE_EXPLORE_ENDPOINTS = 'UPDATE_EXPLORE_ENDPOINTS';
export function updateExploreEndpoints(jsonUrl, csvUrl, standaloneUrl) {
return { type: UPDATE_EXPLORE_ENDPOINTS, jsonUrl, csvUrl, standaloneUrl };
Expand All @@ -143,95 +108,11 @@ export function removeChartAlert() {
return { type: REMOVE_CHART_ALERT };
}

export const FETCH_DASHBOARDS_SUCCEEDED = 'FETCH_DASHBOARDS_SUCCEEDED';
export function fetchDashboardsSucceeded(choices) {
return { type: FETCH_DASHBOARDS_SUCCEEDED, choices };
}

export const FETCH_DASHBOARDS_FAILED = 'FETCH_DASHBOARDS_FAILED';
export function fetchDashboardsFailed(userId) {
return { type: FETCH_DASHBOARDS_FAILED, userId };
}

export function fetchDashboards(userId) {
return function (dispatch) {
const url = '/dashboardmodelviewasync/api/read?_flt_0_owners=' + userId;
$.ajax({
type: 'GET',
url,
success: (data) => {
const choices = [];
for (let i = 0; i < data.pks.length; i++) {
choices.push({ value: data.pks[i], label: data.result[i].dashboard_title });
}
dispatch(fetchDashboardsSucceeded(choices));
},
error: () => {
dispatch(fetchDashboardsFailed(userId));
},
});
};
}

export const SAVE_SLICE_FAILED = 'SAVE_SLICE_FAILED';
export function saveSliceFailed() {
return { type: SAVE_SLICE_FAILED };
}
export const SAVE_SLICE_SUCCESS = 'SAVE_SLICE_SUCCESS';
export function saveSliceSuccess(data) {
return { type: SAVE_SLICE_SUCCESS, data };
}

export const REMOVE_SAVE_MODAL_ALERT = 'REMOVE_SAVE_MODAL_ALERT';
export function removeSaveModalAlert() {
return { type: REMOVE_SAVE_MODAL_ALERT };
}

export function saveSlice(url) {
return function (dispatch) {
return $.get(url, (data, status) => {
if (status === 'success') {
dispatch(saveSliceSuccess(data));
} else {
dispatch(saveSliceFailed());
}
});
};
}

export const UPDATE_CHART_TITLE = 'UPDATE_CHART_TITLE';
export function updateChartTitle(slice_name) {
return { type: UPDATE_CHART_TITLE, slice_name };
}

export const UPDATE_CHART_STATUS = 'UPDATE_CHART_STATUS';
export function updateChartStatus(status) {
return { type: UPDATE_CHART_STATUS, status };
}

export const RUN_QUERY = 'RUN_QUERY';
export function runQuery(formData, force = false) {
return function (dispatch) {
const url = getExploreUrl(formData, 'json', force);
const queryRequest = $.ajax({
url,
dataType: 'json',
success(queryResponse) {
dispatch(chartUpdateSucceeded(queryResponse));
},
error(err) {
if (err.statusText === 'timeout') {
dispatch(chartUpdateTimeout(err.statusText));
} else if (err.statusText !== 'abort') {
dispatch(chartUpdateFailed(err.responseJSON));
}
},
timeout: QUERY_TIMEOUT_THRESHOLD,
});
dispatch(chartUpdateStarted(queryRequest));
};
}

export const RENDER_TRIGGERED = 'RENDER_TRIGGERED';
export function renderTriggered() {
return { type: RENDER_TRIGGERED };
Expand Down
57 changes: 57 additions & 0 deletions superset/assets/javascripts/explore/actions/saveModalActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const $ = window.$ = require('jquery');

export const FETCH_DASHBOARDS_SUCCEEDED = 'FETCH_DASHBOARDS_SUCCEEDED';
export function fetchDashboardsSucceeded(choices) {
return { type: FETCH_DASHBOARDS_SUCCEEDED, choices };
}

export const FETCH_DASHBOARDS_FAILED = 'FETCH_DASHBOARDS_FAILED';
export function fetchDashboardsFailed(userId) {
return { type: FETCH_DASHBOARDS_FAILED, userId };
}

export function fetchDashboards(userId) {
return function (dispatch) {
const url = '/dashboardmodelviewasync/api/read?_flt_0_owners=' + userId;
return $.ajax({
type: 'GET',
url,
success: (data) => {
const choices = [];
for (let i = 0; i < data.pks.length; i++) {
choices.push({ value: data.pks[i], label: data.result[i].dashboard_title });
}
dispatch(fetchDashboardsSucceeded(choices));
},
error: () => {
dispatch(fetchDashboardsFailed(userId));
},
});
};
}

export const SAVE_SLICE_FAILED = 'SAVE_SLICE_FAILED';
export function saveSliceFailed() {
return { type: SAVE_SLICE_FAILED };
}
export const SAVE_SLICE_SUCCESS = 'SAVE_SLICE_SUCCESS';
export function saveSliceSuccess(data) {
return { type: SAVE_SLICE_SUCCESS, data };
}

export const REMOVE_SAVE_MODAL_ALERT = 'REMOVE_SAVE_MODAL_ALERT';
export function removeSaveModalAlert() {
return { type: REMOVE_SAVE_MODAL_ALERT };
}

export function saveSlice(url) {
return function (dispatch) {
return $.get(url, (data, status) => {
if (status === 'success') {
dispatch(saveSliceSuccess(data));
} else {
dispatch(saveSliceFailed());
}
});
};
}
38 changes: 19 additions & 19 deletions superset/assets/javascripts/explore/components/ChartContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,29 +322,29 @@ class ChartContainer extends React.PureComponent {

ChartContainer.propTypes = propTypes;

function mapStateToProps(state) {
const formData = getFormDataFromControls(state.controls);
function mapStateToProps({ explore, chart }) {
const formData = getFormDataFromControls(explore.controls);
return {
alert: state.chartAlert,
can_overwrite: state.can_overwrite,
can_download: state.can_download,
chartStatus: state.chartStatus,
chartUpdateEndTime: state.chartUpdateEndTime,
chartUpdateStartTime: state.chartUpdateStartTime,
datasource: state.datasource,
column_formats: state.datasource ? state.datasource.column_formats : null,
containerId: state.slice ? `slice-container-${state.slice.slice_id}` : 'slice-container',
alert: explore.chartAlert,
can_overwrite: explore.can_overwrite,
can_download: explore.can_download,
datasource: explore.datasource,
column_formats: explore.datasource ? explore.datasource.column_formats : null,
containerId: explore.slice ? `slice-container-${explore.slice.slice_id}` : 'slice-container',
formData,
latestQueryFormData: state.latestQueryFormData,
isStarred: state.isStarred,
queryResponse: state.queryResponse,
slice: state.slice,
standalone: state.standalone,
isStarred: explore.isStarred,
slice: explore.slice,
standalone: explore.standalone,
table_name: formData.datasource_name,
viz_type: formData.viz_type,
triggerRender: state.triggerRender,
datasourceType: state.datasource.type,
datasourceId: state.datasource_id,
triggerRender: explore.triggerRender,
datasourceType: explore.datasource.type,
datasourceId: explore.datasource_id,
chartStatus: chart.chartStatus,
chartUpdateEndTime: chart.chartUpdateEndTime,
chartUpdateStartTime: chart.chartUpdateStartTime,
latestQueryFormData: chart.latestQueryFormData,
queryResponse: chart.queryResponse,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ class ControlPanelsContainer extends React.Component {

ControlPanelsContainer.propTypes = propTypes;

function mapStateToProps(state) {
function mapStateToProps({ explore }) {
return {
alert: state.controlPanelAlert,
isDatasourceMetaLoading: state.isDatasourceMetaLoading,
controls: state.controls,
exploreState: state,
alert: explore.controlPanelAlert,
isDatasourceMetaLoading: explore.isDatasourceMetaLoading,
controls: explore.controls,
exploreState: explore,
};
}

Expand Down
Loading

0 comments on commit b3107bb

Please sign in to comment.