diff --git a/frontend/src/Editor/Editor.jsx b/frontend/src/Editor/Editor.jsx index 3e4f7fca8d..961dea439f 100644 --- a/frontend/src/Editor/Editor.jsx +++ b/frontend/src/Editor/Editor.jsx @@ -323,6 +323,7 @@ class EditorComponent extends React.Component { }, showQuerySearchField: false, }); + this.runQueries(data.data_queries); } ); }); @@ -383,9 +384,7 @@ class EditorComponent extends React.Component { async () => { if (isEmpty(this.state.editingVersion)) await this.createInitVersion(appId); - computeComponentState(this, this.state.appDefinition.components).then(() => { - this.runQueries(data.data_queries); - }); + computeComponentState(this, this.state.appDefinition.components); this.setWindowTitle(data.name); this.setState({ showComments: !!queryString.parse(this.props.location.search).threadId, diff --git a/frontend/src/_helpers/appUtils.js b/frontend/src/_helpers/appUtils.js index 9f8577d866..2d5f6fb7d8 100644 --- a/frontend/src/_helpers/appUtils.js +++ b/frontend/src/_helpers/appUtils.js @@ -669,7 +669,7 @@ export function previewQuery(_ref, query, editorState, calledFromQuery = false) return new Promise(function (resolve, reject) { let queryExecutionPromise = null; if (query.kind === 'runjs') { - queryExecutionPromise = executeMultilineJS(_ref, query.options.code, editorState, true); + queryExecutionPromise = executeMultilineJS(_ref, query.options.code, editorState, query.id, true); } else { queryExecutionPromise = dataqueryService.preview(query, options); } @@ -773,7 +773,7 @@ export function runQuery(_ref, queryId, queryName, confirmed = undefined, mode = _self.setState({ currentState: newState }, () => { let queryExecutionPromise = null; if (query.kind === 'runjs') { - queryExecutionPromise = executeMultilineJS(_self, query.options.code, _ref, false, confirmed, mode); + queryExecutionPromise = executeMultilineJS(_self, query.options.code, _ref, query.id, false, confirmed, mode); } else { queryExecutionPromise = dataqueryService.run(queryId, options); } diff --git a/frontend/src/_helpers/utils.js b/frontend/src/_helpers/utils.js index 1c4fd6b925..1ff8f408cf 100644 --- a/frontend/src/_helpers/utils.js +++ b/frontend/src/_helpers/utils.js @@ -4,6 +4,7 @@ import _ from 'lodash'; import axios from 'axios'; import JSON5 from 'json5'; import { previewQuery, executeAction } from '@/_helpers/appUtils'; +import { toast } from 'react-hot-toast'; export function findProp(obj, prop, defval) { if (typeof defval === 'undefined') defval = null; @@ -318,7 +319,16 @@ export function validateEmail(email) { } // eslint-disable-next-line no-unused-vars -export async function executeMultilineJS(_ref, code, editorState, isPreview, confirmed = undefined, mode = '') { +export async function executeMultilineJS( + _ref, + code, + editorState, + queryId, + isPreview, + // eslint-disable-next-line no-unused-vars + confirmed = undefined, + mode = '' +) { //:: confirmed arg is unused const { currentState } = _ref.state; let result = {}, @@ -327,7 +337,12 @@ export async function executeMultilineJS(_ref, code, editorState, isPreview, con const actions = { runQuery: function (queryName = '') { const query = _ref.state.dataQueries.find((query) => query.name === queryName); - if (_.isEmpty(query)) return; + + if (_.isEmpty(query) || queryId === query?.id) { + const errorMsg = queryId === query?.id ? 'Cannot run query from itself' : 'Query not found'; + toast.error(errorMsg); + return; + } if (isPreview) { return previewQuery(_ref, query, editorState, true); } else {