Skip to content

Commit 115ab70

Browse files
fix(sql lab): MultiSelector component render twice (apache#20706)
* fix(sql lab): MultiSelector component render twice * filter null/undefined tables
1 parent e60083b commit 115ab70

File tree

6 files changed

+43
-17
lines changed

6 files changed

+43
-17
lines changed

superset-frontend/src/SqlLab/actions/sqlLab.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const QUERY_EDITOR_SAVED = 'QUERY_EDITOR_SAVED';
4040
export const CLONE_QUERY_TO_NEW_TAB = 'CLONE_QUERY_TO_NEW_TAB';
4141
export const REMOVE_QUERY_EDITOR = 'REMOVE_QUERY_EDITOR';
4242
export const MERGE_TABLE = 'MERGE_TABLE';
43-
export const REMOVE_TABLE = 'REMOVE_TABLE';
43+
export const REMOVE_TABLES = 'REMOVE_TABLES';
4444
export const END_QUERY = 'END_QUERY';
4545
export const REMOVE_QUERY = 'REMOVE_QUERY';
4646
export const EXPAND_TABLE = 'EXPAND_TABLE';
@@ -1213,16 +1213,21 @@ export function collapseTable(table) {
12131213
};
12141214
}
12151215

1216-
export function removeTable(table) {
1216+
export function removeTables(tables) {
12171217
return function (dispatch) {
1218+
const tablesToRemove = tables?.filter(Boolean) ?? [];
12181219
const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
1219-
? SupersetClient.delete({
1220-
endpoint: encodeURI(`/tableschemaview/${table.id}`),
1221-
})
1220+
? Promise.all(
1221+
tablesToRemove.map(table =>
1222+
SupersetClient.delete({
1223+
endpoint: encodeURI(`/tableschemaview/${table.id}`),
1224+
}),
1225+
),
1226+
)
12221227
: Promise.resolve();
12231228

12241229
return sync
1225-
.then(() => dispatch({ type: REMOVE_TABLE, table }))
1230+
.then(() => dispatch({ type: REMOVE_TABLES, tables: tablesToRemove }))
12261231
.catch(() =>
12271232
dispatch(
12281233
addDangerToast(

superset-frontend/src/SqlLab/actions/sqlLab.test.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,23 +835,40 @@ describe('async actions', () => {
835835
});
836836
});
837837

838-
describe('removeTable', () => {
838+
describe('removeTables', () => {
839839
it('updates the table schema state in the backend', () => {
840840
expect.assertions(2);
841841

842842
const table = { id: 1 };
843843
const store = mockStore({});
844844
const expectedActions = [
845845
{
846-
type: actions.REMOVE_TABLE,
847-
table,
846+
type: actions.REMOVE_TABLES,
847+
tables: [table],
848848
},
849849
];
850-
return store.dispatch(actions.removeTable(table)).then(() => {
850+
return store.dispatch(actions.removeTables([table])).then(() => {
851851
expect(store.getActions()).toEqual(expectedActions);
852852
expect(fetchMock.calls(updateTableSchemaEndpoint)).toHaveLength(1);
853853
});
854854
});
855+
856+
it('deletes multiple tables and updates the table schema state in the backend', () => {
857+
expect.assertions(2);
858+
859+
const tables = [{ id: 1 }, { id: 2 }];
860+
const store = mockStore({});
861+
const expectedActions = [
862+
{
863+
type: actions.REMOVE_TABLES,
864+
tables,
865+
},
866+
];
867+
return store.dispatch(actions.removeTables(tables)).then(() => {
868+
expect(store.getActions()).toEqual(expectedActions);
869+
expect(fetchMock.calls(updateTableSchemaEndpoint)).toHaveLength(2);
870+
});
871+
});
855872
});
856873

857874
describe('migrateQueryEditorFromLocalStorage', () => {

superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export default function SqlEditorLeftBar({
167167
actions.addTable(queryEditor, database, tableName, schemaName),
168168
);
169169

170-
currentTables.forEach(table => actions.removeTable(table));
170+
actions.removeTables(currentTables);
171171
};
172172

173173
const onToggleTable = (updatedTables: string[]) => {

superset-frontend/src/SqlLab/components/TableElement/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface TableElementProps {
5757
table: Table;
5858
actions: {
5959
removeDataPreview: (table: Table) => void;
60-
removeTable: (table: Table) => void;
60+
removeTables: (tables: Table[]) => void;
6161
};
6262
}
6363

@@ -85,7 +85,7 @@ const TableElement = ({ table, actions, ...props }: TableElementProps) => {
8585

8686
const removeTable = () => {
8787
actions.removeDataPreview(table);
88-
actions.removeTable(table);
88+
actions.removeTables([table]);
8989
};
9090

9191
const toggleSortColumns = () => {

superset-frontend/src/SqlLab/reducers/sqlLab.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ export default function sqlLabReducer(state = {}, action) {
175175
[actions.COLLAPSE_TABLE]() {
176176
return alterInArr(state, 'tables', action.table, { expanded: false });
177177
},
178-
[actions.REMOVE_TABLE]() {
179-
return removeFromArr(state, 'tables', action.table);
178+
[actions.REMOVE_TABLES]() {
179+
const tableIds = action.tables.map(table => table.id);
180+
return {
181+
...state,
182+
tables: state.tables.filter(table => !tableIds.includes(table.id)),
183+
};
180184
},
181185
[actions.START_QUERY_VALIDATION]() {
182186
let newState = { ...state };

superset-frontend/src/SqlLab/reducers/sqlLab.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ describe('sqlLabReducer', () => {
181181
});
182182
it('should remove a table', () => {
183183
const action = {
184-
type: actions.REMOVE_TABLE,
185-
table: newTable,
184+
type: actions.REMOVE_TABLES,
185+
tables: [newTable],
186186
};
187187
newState = sqlLabReducer(newState, action);
188188
expect(newState.tables).toHaveLength(0);

0 commit comments

Comments
 (0)