Skip to content

Commit

Permalink
Make instant controls store state in URL (#4449)
Browse files Browse the repository at this point in the history
* Add to history on instant control change

* Update latestQueryFormData on render triggered

* Add new message type

* Update latestQueryFormData in UPDATE_QUERY_FORM_DATA
  • Loading branch information
betodealmeida authored and mistercrunch committed Feb 21, 2018
1 parent d4a2f4e commit c317657
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions superset/assets/javascripts/chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export function renderTriggered(value, key) {
return { type: RENDER_TRIGGERED, value, key };
}

export const UPDATE_QUERY_FORM_DATA = 'UPDATE_QUERY_FORM_DATA';
export function updateQueryFormData(value, key) {
return { type: UPDATE_QUERY_FORM_DATA, value, key };
}

export const RUN_QUERY = 'RUN_QUERY';
export function runQuery(formData, force = false, timeout = 60, key) {
return (dispatch) => {
Expand Down Expand Up @@ -170,6 +175,7 @@ export function runQuery(formData, force = false, timeout = 60, key) {
return Promise.all([
queryPromise,
dispatch(triggerQuery(false, key)),
dispatch(updateQueryFormData(payload, key)),
...annotationLayers.map(x => dispatch(runAnnotationQuery(x, timeout, formData, key))),
]);
};
Expand Down
4 changes: 3 additions & 1 deletion superset/assets/javascripts/chart/chartReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default function chartReducer(charts = {}, action) {
chartUpdateEndTime: null,
chartUpdateStartTime: now(),
queryRequest: action.queryRequest,
latestQueryFormData: action.latestQueryFormData,
};
},
[actions.CHART_UPDATE_STOPPED](state) {
Expand Down Expand Up @@ -93,6 +92,9 @@ export default function chartReducer(charts = {}, action) {
[actions.RENDER_TRIGGERED](state) {
return { ...state, lastRendered: action.value };
},
[actions.UPDATE_QUERY_FORM_DATA](state) {
return { ...state, latestQueryFormData: action.value };
},
[actions.ANNOTATION_QUERY_STARTED](state) {
if (state.annotationQuery &&
state.annotationQuery[action.annotation.name]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ class ExploreViewContainer extends React.Component {
this.props.actions.fetchDatasourceMetadata(np.form_data.datasource, true);
}
// if any control value changed and it's an instant control
if (Object.keys(np.controls).some(key => (np.controls[key].renderTrigger &&
typeof this.props.controls[key] !== 'undefined' &&
!areObjectsEqual(np.controls[key].value, this.props.controls[key].value)))
) {
if (this.instantControlChanged(this.props.controls, np.controls)) {
this.props.actions.updateQueryFormData(
getFormDataFromControls(np.controls), this.props.chart.chartKey);
this.props.actions.renderTriggered(new Date().getTime(), this.props.chart.chartKey);
}
}

componentDidUpdate() {
/* eslint no-unused-vars: 0 */
componentDidUpdate(prevProps, prevState) {
this.triggerQueryIfNeeded();

if (this.instantControlChanged(prevProps.controls, this.props.controls)) {
this.addHistory({});
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -117,6 +121,14 @@ class ExploreViewContainer extends React.Component {
return `${window.innerHeight - navHeight}px`;
}

instantControlChanged(prevControls, currentControls) {
return Object.keys(currentControls).some(key => (
currentControls[key].renderTrigger &&
typeof prevControls[key] !== 'undefined' &&
!areObjectsEqual(currentControls[key].value, prevControls[key].value)
));
}

triggerQueryIfNeeded() {
if (this.props.chart.triggerQuery && !this.hasErrors()) {
this.props.actions.runQuery(this.props.form_data, false,
Expand Down

0 comments on commit c317657

Please sign in to comment.