Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create Chart onClick Functionality #20809

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions superset-frontend/src/SqlLab/components/ResultSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import {
SaveDatasetModal,
} from 'src/SqlLab/components/SaveDatasetModal';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
import { EXPLORE_CHART_DEFAULT } from 'src/SqlLab/types';
import { mountExploreUrl } from 'src/explore/exploreUtils';
import { postFormData } from 'src/explore/exploreUtils/formData';
import ProgressBar from 'src/components/ProgressBar';
import Loading from 'src/components/Loading';
import FilterableTable, {
Expand All @@ -41,6 +44,7 @@ import ExploreCtasResultsButton from '../ExploreCtasResultsButton';
import ExploreResultsButton from '../ExploreResultsButton';
import HighlightedSql from '../HighlightedSql';
import QueryStateLabel from '../QueryStateLabel';
import { URL_PARAMS } from 'src/constants';

enum LIMITING_FACTOR {
QUERY = 'QUERY',
Expand Down Expand Up @@ -134,6 +138,8 @@ export default class ResultSet extends React.PureComponent<
this.reFetchQueryResults = this.reFetchQueryResults.bind(this);
this.toggleExploreResultsButton =
this.toggleExploreResultsButton.bind(this);
this.createExploreResultsOnClick =
this.createExploreResultsOnClick.bind(this);
}

async componentDidMount() {
Expand Down Expand Up @@ -213,6 +219,26 @@ export default class ResultSet extends React.PureComponent<
}
}

async createExploreResultsOnClick() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we use arrow function syntax here, you don't have to add the .bind call on line 141,142:
async createExploreResultsOnClick = () => {
Arrow functions automatically bind this to where the function is defined which has the same effect as calling this.createExploreResultsOnClick = this.createExploreResultsOnClick.bind(this);
I have been updating files I work in to convert to arrow functions and remove the bind calls because it removes the second step and extra code.

const { results } = this.props.query;

if (results.query_id) {
const key = await postFormData(results.query_id, 'query', {
...EXPLORE_CHART_DEFAULT,
datasource: `${results.query_id}__query`,
...{
all_columns: results.columns.map(column => column.name),
},
});
const url = mountExploreUrl(null, {
[URL_PARAMS.formDataKey.name]: key,
});
window.open(url, '_blank', 'noreferrer');
} else {
this.setState({ showSaveDatasetModal: true });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this this.setState({ showSaveDatasetModal: true });. Initially I was trying to use serverId which was not always present and just defaulted to the original behavior. with use of queryId, it should always be there and if it is not we have a bug. We may to change the else to show an error toast indicating the query has no id and cannot be used to create a chart. This should never happen and provides visual indication if something is broken

}
}

renderControls() {
if (this.props.search || this.props.visualize || this.props.csv) {
let { data } = this.props.query.results;
Expand Down Expand Up @@ -250,19 +276,11 @@ export default class ResultSet extends React.PureComponent<
this.props.database?.allows_virtual_table_explore && (
<ExploreResultsButton
database={this.props.database}
onClick={() => {
// There is currently redux / state issue where sometimes a query will have serverId
// and other times it will not. We need this attribute consistently for this to work
// const qid = this.props?.query?.results?.query_id;
// if (qid) {
// // This will open explore using the query as datasource
// window.location.href = `/explore/?dataset_type=query&dataset_id=${qid}`;
// } else {
// this.setState({ showSaveDatasetModal: true });
// }
this.setState({ showSaveDatasetModal: true });
}}
onClick={() => this.setState({ showSaveDatasetModal: true })}
/>
// In order to use the new workflow for a query powered chart, replace the
// above function with:
// onClick={this.createExploreResultsOnClick}
)}
{this.props.csv && (
<Button buttonSize="small" href={`/superset/csv/${query.id}`}>
Expand Down