(
+ ({ sqlLab: { unsavedQueryEditor } }) => {
+ const updatedQueryEditor = {
+ ...queryEditor,
+ ...(unsavedQueryEditor.id === queryEditor.id && unsavedQueryEditor),
+ };
+ return updatedQueryEditor.schema;
+ },
+ );
useEffect(() => {
const bool = querystring.parse(window.location.search).db;
@@ -263,7 +273,7 @@ export default function SqlEditorLeftBar({
onSchemasLoad={handleSchemasLoad}
onTableSelectChange={onTablesChange}
onTablesLoad={handleTablesLoad}
- schema={queryEditor.schema}
+ schema={schema}
tableValue={selectedTableNames}
sqlLabMode
/>
diff --git a/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx b/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
index c5b94f8b354..7c53181866f 100644
--- a/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
+++ b/superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
@@ -262,6 +262,9 @@ class TabbedSqlEditors extends React.PureComponent {
const qeid = this.props.tabHistory[this.props.tabHistory.length - 1];
if (key !== qeid) {
const queryEditor = this.props.queryEditors.find(qe => qe.id === key);
+ if (!queryEditor) {
+ return;
+ }
this.props.actions.switchQueryEditor(
queryEditor,
this.props.displayLimit,
diff --git a/superset-frontend/src/SqlLab/components/TableElement/index.tsx b/superset-frontend/src/SqlLab/components/TableElement/index.tsx
index b05c875e120..1ecc7822203 100644
--- a/superset-frontend/src/SqlLab/components/TableElement/index.tsx
+++ b/superset-frontend/src/SqlLab/components/TableElement/index.tsx
@@ -270,6 +270,7 @@ const TableElement = ({ table, actions, ...props }: TableElementProps) => {
const metadata = (
setHover(true)}
onMouseLeave={() => setHover(false)}
css={{ paddingTop: 6 }}
diff --git a/superset-frontend/src/SqlLab/reducers/sqlLab.js b/superset-frontend/src/SqlLab/reducers/sqlLab.js
index a12db71a37d..bab832d6c61 100644
--- a/superset-frontend/src/SqlLab/reducers/sqlLab.js
+++ b/superset-frontend/src/SqlLab/reducers/sqlLab.js
@@ -110,7 +110,12 @@ export default function sqlLabReducer(state = {}, action) {
);
},
[actions.REMOVE_QUERY_EDITOR]() {
- let newState = removeFromArr(state, 'queryEditors', action.queryEditor);
+ const queryEditor = {
+ ...action.queryEditor,
+ ...(action.queryEditor.id === state.unsavedQueryEditor.id &&
+ state.unsavedQueryEditor),
+ };
+ let newState = removeFromArr(state, 'queryEditors', queryEditor);
// List of remaining queryEditor ids
const qeIds = newState.queryEditors.map(qe => qe.id);
@@ -127,10 +132,19 @@ export default function sqlLabReducer(state = {}, action) {
// Remove associated table schemas
const tables = state.tables.filter(
- table => table.queryEditorId !== action.queryEditor.id,
+ table => table.queryEditorId !== queryEditor.id,
);
- newState = { ...newState, tabHistory, tables, queries };
+ newState = {
+ ...newState,
+ tabHistory,
+ tables,
+ queries,
+ unsavedQueryEditor: {
+ ...(action.queryEditor.id !== state.unsavedQueryEditor.id &&
+ state.unsavedQueryEditor),
+ },
+ };
return newState;
},
[actions.REMOVE_QUERY]() {
diff --git a/superset-frontend/src/SqlLab/reducers/sqlLab.test.js b/superset-frontend/src/SqlLab/reducers/sqlLab.test.js
index 82483712f88..088d01031a5 100644
--- a/superset-frontend/src/SqlLab/reducers/sqlLab.test.js
+++ b/superset-frontend/src/SqlLab/reducers/sqlLab.test.js
@@ -75,6 +75,28 @@ describe('sqlLabReducer', () => {
initialState.queryEditors.length,
);
});
+ it('should remove a query editor including unsaved changes', () => {
+ expect(newState.queryEditors).toHaveLength(
+ initialState.queryEditors.length + 1,
+ );
+ let action = {
+ type: actions.QUERY_EDITOR_SETDB,
+ queryEditor: qe,
+ dbId: 123,
+ };
+ newState = sqlLabReducer(newState, action);
+ expect(newState.unsavedQueryEditor.dbId).toEqual(action.dbId);
+ action = {
+ type: actions.REMOVE_QUERY_EDITOR,
+ queryEditor: qe,
+ };
+ newState = sqlLabReducer(newState, action);
+ expect(newState.queryEditors).toHaveLength(
+ initialState.queryEditors.length,
+ );
+ expect(newState.unsavedQueryEditor.dbId).toBeUndefined();
+ expect(newState.unsavedQueryEditor.id).toBeUndefined();
+ });
it('should set q query editor active', () => {
const expectedTitle = 'new updated title';
const addQueryEditorAction = {