etr2460 commented on a change in pull request #8060: SIP-23: Persist SQL Lab 
state in the backend
URL: 
https://github.com/apache/incubator-superset/pull/8060#discussion_r320853804
 
 

 ##########
 File path: superset/assets/src/SqlLab/actions/sqlLab.js
 ##########
 @@ -271,40 +303,230 @@ export function setDatabases(databases) {
   return { type: SET_DATABASES, databases };
 }
 
+function migrateTable(table, queryEditorId, dispatch) {
+  return SupersetClient.post({
+    endpoint: encodeURI('/tableschemaview/'),
+    postPayload: { table: { ...table, queryEditorId } },
+  })
+    .then(({ json }) => {
+      const newTable = {
+        ...table,
+        id: json.id,
+        queryEditorId,
+      };
+      return dispatch({ type: MIGRATE_TABLE, oldTable: table, newTable });
+    })
+    .catch(() => dispatch(addDangerToast(t('Unable to migrate table'))));
+}
+
+function migrateQuery(queryId, queryEditorId, dispatch) {
+  return SupersetClient.post({
+    endpoint: encodeURI(`/tabstateview/${queryEditorId}/migrate_query`),
+    postPayload: { queryId },
+  })
+    .then(() => dispatch({ type: MIGRATE_QUERY, queryId, queryEditorId }))
+    .catch(() => dispatch(addDangerToast(t('Unable to migrate query'))));
+}
+
+export function migrateQueryEditorFromLocalStorage(queryEditor, tables, 
queries) {
+  return function (dispatch) {
+    return SupersetClient.post({ endpoint: '/tabstateview/', postPayload: { 
queryEditor } })
+      .then(({ json }) => {
+        const newQueryEditor = {
+          ...queryEditor,
+          id: json.id.toString(),
+        };
+        dispatch({ type: MIGRATE_QUERY_EDITOR, oldQueryEditor: queryEditor, 
newQueryEditor });
+        dispatch({ type: MIGRATE_TAB_HISTORY, oldId: queryEditor.id, newId: 
newQueryEditor.id });
+        return Promise.all([
+          ...tables.map(table => migrateTable(table, newQueryEditor.id, 
dispatch)),
+          ...queries.map(query => migrateQuery(query.id, newQueryEditor.id, 
dispatch)),
+        ]);
+      })
+      .catch(() => dispatch(addDangerToast(t('Unable to migrate query 
editor'))));
+  };
+}
+
 export function addQueryEditor(queryEditor) {
-  const newQueryEditor = {
-    ...queryEditor,
-    id: shortid.generate(),
+  return function (dispatch) {
+    const sync = isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE)
+      ? SupersetClient.post({ endpoint: '/tabstateview/', postPayload: { 
queryEditor } })
+      : Promise.resolve({ json: { id: shortid.generate() } });
+
+    return sync
+      .then(({ json }) => {
+        const newQueryEditor = {
+          ...queryEditor,
+          id: json.id.toString(),
+        };
+        return dispatch({ type: ADD_QUERY_EDITOR, queryEditor: newQueryEditor 
});
+      })
+      .catch(() => dispatch(addDangerToast(t('Unable to add a new tab'))));
   };
-  return { type: ADD_QUERY_EDITOR, queryEditor: newQueryEditor };
 }
 
 export function cloneQueryToNewTab(query) {
-  return { type: CLONE_QUERY_TO_NEW_TAB, query };
+  return function (dispatch, getState) {
+    const { queryEditors, tabHistory } = getState();
+    const progenitor = queryEditors.find(qe => qe.id === 
tabHistory[tabHistory.length - 1]);
 
 Review comment:
   ++ for fancy words, but `originQuery` might be a bit more understandable to 
non-native English speakers (or English speakers themselves, I had to look this 
up to confirm I understood it :D ) 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to