Skip to content

Commit

Permalink
fix(sqllab): custom url params disappeared
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark committed May 5, 2023
1 parent 841726d commit d6d279f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { newQueryTabName } from 'src/SqlLab/utils/newQueryTabName';
import QueryProvider from 'src/views/QueryProvider';

fetchMock.get('glob:*/api/v1/database/*', {});
fetchMock.get('glob:*/savedqueryviewapi/api/get/*', {});
fetchMock.get('glob:*/api/v1/saved_query/*', {});
fetchMock.get('glob:*/kv/*', {});

describe('TabbedSqlEditors', () => {
Expand Down Expand Up @@ -131,6 +131,18 @@ describe('TabbedSqlEditors', () => {
'/superset/sqllab',
);
});
it('should handle custom url params', async () => {
uriStub.returns({
sql: 1,
dbid: 1,
custom_value: 'str',
extra_attr1: 'true',
});
await mountWithAct();
expect(window.history.replaceState.getCall(0).args[2]).toBe(
'/superset/sqllab?custom_value=str&extra_attr1=true',
);
});
});
it('should removeQueryEditor', () => {
wrapper = getWrapper();
Expand Down
76 changes: 42 additions & 34 deletions superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,63 @@ class TabbedSqlEditors extends React.PureComponent {
// but for some reason this data isn't being passed properly through
// the reducer.
const bootstrapData = getBootstrapData();
const query = {
const {
id,
name,
sql,
savedQueryId,
datasourceKey,
queryId,
dbid,
dbname,
schema,
autorun,
new: isNewQuery,
...urlParams
} = {
...bootstrapData.requested_query,
...URI(window.location).search(true),
};

// Popping a new tab based on the querystring
if (
query.id ||
query.sql ||
query.savedQueryId ||
query.datasourceKey ||
query.queryId
) {
if (query.id) {
this.props.actions.popStoredQuery(query.id);
} else if (query.savedQueryId) {
this.props.actions.popSavedQuery(query.savedQueryId);
} else if (query.queryId) {
this.props.actions.popQuery(query.queryId);
} else if (query.datasourceKey) {
this.props.actions.popDatasourceQuery(query.datasourceKey, query.sql);
} else if (query.sql) {
let dbId = query.dbid;
if (dbId) {
dbId = parseInt(dbId, 10);
if (id || sql || savedQueryId || datasourceKey || queryId) {
if (id) {
this.props.actions.popStoredQuery(id);
} else if (savedQueryId) {
this.props.actions.popSavedQuery(savedQueryId);
} else if (queryId) {
this.props.actions.popQuery(queryId);
} else if (datasourceKey) {
this.props.actions.popDatasourceQuery(datasourceKey, sql);
} else if (sql) {
let databaseId = dbid;
if (databaseId) {
databaseId = parseInt(databaseId, 10);
} else {
const { databases } = this.props;
const dbName = query.dbname;
if (dbName) {
const databaseName = dbname;
if (databaseName) {
Object.keys(databases).forEach(db => {
if (databases[db].database_name === dbName) {
dbId = databases[db].id;
if (databases[db].database_name === databaseName) {
databaseId = databases[db].id;
}
});
}
}
const newQueryEditor = {
name: query.name,
dbId,
schema: query.schema,
autorun: query.autorun,
sql: query.sql,
name,
dbId: databaseId,
schema,
autorun,
sql,
};
this.props.actions.addQueryEditor(newQueryEditor);
}
this.popNewTab();
} else if (query.new || this.props.queryEditors.length === 0) {
this.popNewTab(urlParams);
} else if (isNewQuery || this.props.queryEditors.length === 0) {
this.newQueryEditor();

if (query.new) {
if (isNewQuery) {
window.history.replaceState({}, document.title, this.state.sqlLabUrl);
}
} else {
Expand All @@ -187,9 +194,10 @@ class TabbedSqlEditors extends React.PureComponent {
}
}

popNewTab() {
popNewTab(urlParams) {
// Clean the url in browser history
window.history.replaceState({}, document.title, this.state.sqlLabUrl);
const updatedUrl = `${URI(this.state.sqlLabUrl).query(urlParams)}`;
window.history.replaceState({}, document.title, updatedUrl);
}

activeQueryEditor() {
Expand Down

0 comments on commit d6d279f

Please sign in to comment.