Skip to content

Commit

Permalink
[sqllab] Fixed js error when results are not available (#1715)
Browse files Browse the repository at this point in the history
* Fixed js error when results are not available

* Flush data and query in results when running new query, keeping columns

* add exception for columns

* Move setState from componentWillMount to componentWillReceiveProps

* Nit
  • Loading branch information
vera-liu committed Dec 5, 2016
1 parent d8864bc commit eb0655c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
3 changes: 1 addition & 2 deletions superset/assets/javascripts/SqlLab/components/QueryTable.jsx
Expand Up @@ -47,8 +47,7 @@ class QueryTable extends React.PureComponent {
this.setState({ showVisualizeModal: false });
}
showVisualizeModal(query) {
this.setState({ showVisualizeModal: true });
this.setState({ activeQuery: query });
this.setState({ activeQuery: query, showVisualizeModal: true });
}
restoreSql(query) {
this.props.actions.queryEditorSetSql({ id: query.sqlEditorId }, query.sql);
Expand Down
28 changes: 18 additions & 10 deletions superset/assets/javascripts/SqlLab/components/VisualizeModal.jsx
Expand Up @@ -34,21 +34,21 @@ class VisualizeModal extends React.PureComponent {
hints: [],
};
}
componentWillMount() {
this.setStateFromProps();
}
componentDidMount() {
this.validate();
}
setStateFromProps() {
componentWillReceiveProps(nextProps) {
this.setStateFromProps(nextProps);
}
setStateFromProps(props) {
if (
!this.props.query ||
!this.props.query.results ||
!this.props.query.results.columns) {
!props.query ||
!props.query.results ||
!props.query.results.columns) {
return;
}
const columns = {};
this.props.query.results.columns.forEach((col) => {
props.query.results.columns.forEach((col) => {
columns[col.name] = col;
});
this.setState({ columns });
Expand Down Expand Up @@ -125,8 +125,16 @@ class VisualizeModal extends React.PureComponent {
this.setState({ columns }, this.validate);
}
render() {
if (!(this.props.query)) {
return <div />;
if (!(this.props.query) || !(this.props.query.results) || !(this.props.query.results.columns)) {
return (
<div className="VisualizeModal">
<Modal show={this.props.show} onHide={this.props.onHide}>
<Modal.Body>
No results available for this query
</Modal.Body>
</Modal>
</div>
);
}
const tableData = this.props.query.results.columns.map((col) => ({
column: col.name,
Expand Down
6 changes: 4 additions & 2 deletions superset/assets/javascripts/SqlLab/reducers.js
Expand Up @@ -134,8 +134,10 @@ export const sqlLabReducer = function (state, action) {
let newState = Object.assign({}, state);
if (action.query.sqlEditorId) {
const qe = getFromArr(state.queryEditors, action.query.sqlEditorId);
if (qe.latestQueryId) {
const q = Object.assign({}, state.queries[qe.latestQueryId], { results: null });
if (qe.latestQueryId && state.queries[qe.latestQueryId]) {
const newResults = Object.assign(
{}, state.queries[qe.latestQueryId].results, { data: [], query: null });
const q = Object.assign({}, state.queries[qe.latestQueryId], { results: newResults });
const queries = Object.assign({}, state.queries, { [q.id]: q });
newState = Object.assign({}, state, { queries });
}
Expand Down

0 comments on commit eb0655c

Please sign in to comment.