Skip to content

Commit

Permalink
fix(sqllab): wiped out unsaved changes by delayed actions
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark committed Oct 19, 2022
1 parent 52d33b0 commit 587b2be
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
49 changes: 31 additions & 18 deletions superset-frontend/src/SqlLab/reducers/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,22 @@ import {
} from '../../reduxUtils';

function alterUnsavedQueryEditorState(state, updatedState, id) {
if (state.tabHistory[state.tabHistory.length - 1] !== id) {
const { queryEditors } = alterInArr(
state,
'queryEditors',
{ id },
updatedState,
);
return {
queryEditors,
};
}
return {
...(state.unsavedQueryEditor.id === id && state.unsavedQueryEditor),
...(id ? { id, ...updatedState } : state.unsavedQueryEditor),
unsavedQueryEditor: {
...(state.unsavedQueryEditor.id === id && state.unsavedQueryEditor),
...(id ? { id, ...updatedState } : state.unsavedQueryEditor),
},
};
}

Expand Down Expand Up @@ -227,7 +240,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.START_QUERY_VALIDATION]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
validationResult: {
Expand Down Expand Up @@ -259,7 +272,7 @@ export default function sqlLabReducer(state = {}, action) {
// Otherwise, persist the results on the queryEditor state
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
validationResult: {
Expand Down Expand Up @@ -367,7 +380,7 @@ export default function sqlLabReducer(state = {}, action) {

return {
...newState,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
latestQueryId: action.query.id,
Expand Down Expand Up @@ -538,7 +551,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SETDB]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
dbId: action.dbId,
Expand All @@ -550,7 +563,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_FUNCTION_NAMES]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
functionNames: action.functionNames,
Expand All @@ -562,7 +575,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_SCHEMA]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
schema: action.schema,
Expand All @@ -574,7 +587,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_SCHEMA_OPTIONS]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
schemaOptions: action.options,
Expand All @@ -586,7 +599,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_TABLE_OPTIONS]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
tableOptions: action.options,
Expand All @@ -598,7 +611,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_TITLE]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
name: action.name,
Expand All @@ -610,7 +623,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_SQL]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
sql: action.sql,
Expand All @@ -622,7 +635,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_QUERY_LIMIT]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
queryLimit: action.queryLimit,
Expand All @@ -634,7 +647,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_TEMPLATE_PARAMS]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
templateParams: action.templateParams,
Expand All @@ -646,7 +659,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_SELECTED_TEXT]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
selectedText: action.sql,
Expand All @@ -658,7 +671,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_SET_AUTORUN]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
autorun: action.autorun,
Expand All @@ -670,7 +683,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_PERSIST_HEIGHT]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
northPercent: action.northPercent,
Expand All @@ -683,7 +696,7 @@ export default function sqlLabReducer(state = {}, action) {
[actions.QUERY_EDITOR_TOGGLE_LEFT_BAR]() {
return {
...state,
unsavedQueryEditor: alterUnsavedQueryEditorState(
...alterUnsavedQueryEditorState(
state,
{
hideLeftBar: action.hideLeftBar,
Expand Down
26 changes: 23 additions & 3 deletions superset-frontend/src/SqlLab/reducers/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,33 @@ describe('sqlLabReducer', () => {
const selectedText = 'TEST';
const action = {
type: actions.QUERY_EDITOR_SET_SELECTED_TEXT,
queryEditor: newState.queryEditors[0],
queryEditor: qe,
sql: selectedText,
};
expect(newState.queryEditors[0].selectedText).toBeFalsy();
expect(qe.selectedText).toBeFalsy();
newState = sqlLabReducer(newState, action);
expect(newState.unsavedQueryEditor.selectedText).toBe(selectedText);
expect(newState.unsavedQueryEditor.id).toBe(newState.queryEditors[0].id);
expect(newState.unsavedQueryEditor.id).toBe(qe.id);
});
it('should not wiped out unsaved changes while delayed async call intercepted', () => {
const expectedSql = 'Updated SQL WORKING IN PROGRESS--';
const action = {
type: actions.QUERY_EDITOR_SET_SQL,
queryEditor: qe,
sql: expectedSql,
};
newState = sqlLabReducer(newState, action);
expect(newState.unsavedQueryEditor.sql).toBe(expectedSql);
const interceptedAction = {
type: actions.QUERY_EDITOR_SET_FUNCTION_NAMES,
queryEditor: newState.queryEditors[0],
functionNames: ['func1', 'func2'],
};
newState = sqlLabReducer(newState, interceptedAction);
expect(newState.unsavedQueryEditor.sql).toBe(expectedSql);
expect(newState.queryEditors[0].functionNames).toBe(
interceptedAction.functionNames,
);
});
});
describe('Tables', () => {
Expand Down

0 comments on commit 587b2be

Please sign in to comment.