2021-09-08 17:32:22 +00:00
|
|
|
import {
|
|
|
|
|
all,
|
|
|
|
|
select,
|
|
|
|
|
put,
|
|
|
|
|
takeEvery,
|
|
|
|
|
debounce,
|
|
|
|
|
call,
|
|
|
|
|
take,
|
2022-03-17 12:05:17 +00:00
|
|
|
takeLatest,
|
2021-09-08 17:32:22 +00:00
|
|
|
} from "redux-saga/effects";
|
|
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
import {
|
|
|
|
|
getCurrentApplicationId,
|
|
|
|
|
getCurrentPageId,
|
|
|
|
|
} from "selectors/editorSelectors";
|
|
|
|
|
import { getJSCollection, getJSCollections } from "selectors/entitiesSelector";
|
|
|
|
|
import { JSCollectionData } from "reducers/entityReducers/jsActionsReducer";
|
|
|
|
|
import { createNewJSFunctionName, getQueryParams } from "utils/AppsmithUtils";
|
|
|
|
|
import { JSCollection, JSAction } from "entities/JSCollection";
|
|
|
|
|
import { createJSCollectionRequest } from "actions/jsActionActions";
|
|
|
|
|
import history from "utils/history";
|
2021-11-08 06:49:22 +00:00
|
|
|
import { executeFunction } from "./EvaluationsSaga";
|
2021-09-23 07:21:57 +00:00
|
|
|
import { getJSCollectionIdFromURL } from "pages/Editor/Explorer/helpers";
|
2021-09-28 07:31:46 +00:00
|
|
|
import {
|
|
|
|
|
getDifferenceInJSCollection,
|
2021-11-08 06:49:22 +00:00
|
|
|
JSUpdate,
|
2021-09-28 07:31:46 +00:00
|
|
|
pushLogsForObjectUpdate,
|
2021-10-26 12:53:58 +00:00
|
|
|
createDummyJSCollectionActions,
|
2021-09-28 07:31:46 +00:00
|
|
|
} from "../utils/JSPaneUtils";
|
2022-03-17 12:05:17 +00:00
|
|
|
import JSActionAPI, {
|
|
|
|
|
RefactorAction,
|
|
|
|
|
SetFunctionPropertyPayload,
|
|
|
|
|
} from "../api/JSActionAPI";
|
|
|
|
|
import ActionAPI from "api/ActionAPI";
|
2021-09-08 17:32:22 +00:00
|
|
|
import {
|
|
|
|
|
updateJSCollectionSuccess,
|
|
|
|
|
refactorJSCollectionAction,
|
2021-11-08 06:49:22 +00:00
|
|
|
updateJSCollectionBodySuccess,
|
2022-03-17 12:05:17 +00:00
|
|
|
updateJSFunction,
|
2021-09-23 07:21:57 +00:00
|
|
|
} from "actions/jsPaneActions";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { getCurrentOrgId } from "selectors/organizationSelectors";
|
|
|
|
|
import { getPluginIdOfPackageName } from "sagas/selectors";
|
|
|
|
|
import { PluginType } from "entities/Action";
|
|
|
|
|
import { Toaster } from "components/ads/Toast";
|
|
|
|
|
import { Variant } from "components/ads/common";
|
|
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
ERROR_JS_COLLECTION_RENAME_FAIL,
|
2021-09-28 07:31:46 +00:00
|
|
|
JS_EXECUTION_SUCCESS,
|
|
|
|
|
JS_EXECUTION_FAILURE,
|
|
|
|
|
JS_EXECUTION_FAILURE_TOASTER,
|
|
|
|
|
JS_FUNCTION_CREATE_SUCCESS,
|
|
|
|
|
JS_FUNCTION_DELETE_SUCCESS,
|
|
|
|
|
JS_FUNCTION_UPDATE_SUCCESS,
|
2022-02-11 18:08:46 +00:00
|
|
|
} from "@appsmith/constants/messages";
|
2021-09-08 17:32:22 +00:00
|
|
|
import { validateResponse } from "./ErrorSagas";
|
|
|
|
|
import AppsmithConsole from "utils/AppsmithConsole";
|
2021-09-28 07:31:46 +00:00
|
|
|
import { ENTITY_TYPE, PLATFORM_ERROR } from "entities/AppsmithConsole";
|
2021-09-08 17:32:22 +00:00
|
|
|
import LOG_TYPE from "entities/AppsmithConsole/logtype";
|
|
|
|
|
import PageApi from "api/PageApi";
|
|
|
|
|
import { updateCanvasWithDSL } from "sagas/PageSagas";
|
|
|
|
|
export const JS_PLUGIN_PACKAGE_NAME = "js-plugin";
|
2022-03-17 12:05:17 +00:00
|
|
|
import { set } from "lodash";
|
2021-12-07 09:45:18 +00:00
|
|
|
import { updateReplayEntity } from "actions/pageActions";
|
2022-03-25 10:43:26 +00:00
|
|
|
import { jsCollectionIdURL } from "RouteBuilder";
|
2021-09-08 17:32:22 +00:00
|
|
|
|
|
|
|
|
function* handleCreateNewJsActionSaga(action: ReduxAction<{ pageId: string }>) {
|
|
|
|
|
const organizationId: string = yield select(getCurrentOrgId);
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const { pageId } = action.payload;
|
|
|
|
|
const pluginId: string = yield select(
|
|
|
|
|
getPluginIdOfPackageName,
|
|
|
|
|
JS_PLUGIN_PACKAGE_NAME,
|
|
|
|
|
);
|
|
|
|
|
if (pageId && pluginId) {
|
2021-09-23 07:21:57 +00:00
|
|
|
const jsActions = yield select(getJSCollections);
|
|
|
|
|
const pageJSActions = jsActions.filter(
|
2021-09-08 17:32:22 +00:00
|
|
|
(a: JSCollectionData) => a.config.pageId === pageId,
|
|
|
|
|
);
|
|
|
|
|
const newJSCollectionName = createNewJSFunctionName(pageJSActions, pageId);
|
2021-10-26 12:53:58 +00:00
|
|
|
const { actions, body } = createDummyJSCollectionActions(
|
|
|
|
|
pageId,
|
|
|
|
|
organizationId,
|
|
|
|
|
);
|
2021-09-08 17:32:22 +00:00
|
|
|
yield put(
|
|
|
|
|
createJSCollectionRequest({
|
|
|
|
|
name: newJSCollectionName,
|
|
|
|
|
pageId,
|
|
|
|
|
organizationId,
|
|
|
|
|
pluginId,
|
2021-10-26 12:53:58 +00:00
|
|
|
body: body,
|
2021-09-15 06:28:25 +00:00
|
|
|
variables: [
|
|
|
|
|
{
|
2021-10-26 12:53:58 +00:00
|
|
|
name: "myVar1",
|
2021-09-15 06:28:25 +00:00
|
|
|
value: [],
|
|
|
|
|
},
|
2021-10-26 12:53:58 +00:00
|
|
|
{
|
|
|
|
|
name: "myVar2",
|
|
|
|
|
value: {},
|
|
|
|
|
},
|
2021-09-15 06:28:25 +00:00
|
|
|
],
|
2021-10-26 12:53:58 +00:00
|
|
|
actions: actions,
|
2021-09-08 17:32:22 +00:00
|
|
|
applicationId,
|
|
|
|
|
pluginType: PluginType.JS,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleJSCollectionCreatedSaga(
|
|
|
|
|
actionPayload: ReduxAction<JSCollection>,
|
|
|
|
|
) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
2022-03-25 10:43:26 +00:00
|
|
|
history.push(
|
|
|
|
|
jsCollectionIdURL({
|
|
|
|
|
collectionId: id,
|
|
|
|
|
params: {},
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 06:49:22 +00:00
|
|
|
function* handleEachUpdateJSCollection(update: JSUpdate) {
|
|
|
|
|
const jsActionId = update.id;
|
2021-09-08 17:32:22 +00:00
|
|
|
const organizationId: string = yield select(getCurrentOrgId);
|
|
|
|
|
if (jsActionId) {
|
|
|
|
|
const jsAction: JSCollection = yield select(getJSCollection, jsActionId);
|
2021-11-08 06:49:22 +00:00
|
|
|
const parsedBody = update.parsedBody;
|
2021-10-19 11:53:15 +00:00
|
|
|
const jsActionTobeUpdated = JSON.parse(JSON.stringify(jsAction));
|
2021-09-21 06:02:45 +00:00
|
|
|
if (parsedBody) {
|
2021-11-08 06:49:22 +00:00
|
|
|
// jsActionTobeUpdated.body = jsAction.body;
|
2021-09-21 06:02:45 +00:00
|
|
|
const data = getDifferenceInJSCollection(parsedBody, jsAction);
|
|
|
|
|
if (data.nameChangedActions.length) {
|
|
|
|
|
for (let i = 0; i < data.nameChangedActions.length; i++) {
|
|
|
|
|
yield put(
|
|
|
|
|
refactorJSCollectionAction({
|
2021-10-19 11:53:15 +00:00
|
|
|
refactorAction: {
|
|
|
|
|
actionId: data.nameChangedActions[i].id,
|
|
|
|
|
collectionName: jsAction.name,
|
|
|
|
|
pageId: data.nameChangedActions[i].pageId,
|
|
|
|
|
oldName: data.nameChangedActions[i].oldName,
|
|
|
|
|
newName: data.nameChangedActions[i].newName,
|
|
|
|
|
},
|
|
|
|
|
actionCollection: jsActionTobeUpdated,
|
2021-09-21 06:02:45 +00:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-10-19 11:53:15 +00:00
|
|
|
} else {
|
|
|
|
|
let newActions: Partial<JSAction>[] = [];
|
|
|
|
|
let updateActions: JSAction[] = [];
|
|
|
|
|
let deletedActions: JSAction[] = [];
|
2021-11-08 06:49:22 +00:00
|
|
|
let updateCollection = false;
|
|
|
|
|
const changedVariables = data.changedVariables;
|
|
|
|
|
if (changedVariables.length) {
|
2021-10-19 11:53:15 +00:00
|
|
|
jsActionTobeUpdated.variables = parsedBody.variables;
|
2021-11-08 06:49:22 +00:00
|
|
|
updateCollection = true;
|
2021-09-21 06:02:45 +00:00
|
|
|
}
|
2021-10-19 11:53:15 +00:00
|
|
|
if (data.newActions.length) {
|
|
|
|
|
newActions = data.newActions;
|
|
|
|
|
for (let i = 0; i < data.newActions.length; i++) {
|
|
|
|
|
jsActionTobeUpdated.actions.push({
|
|
|
|
|
...data.newActions[i],
|
|
|
|
|
organizationId: organizationId,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
updateCollection = true;
|
2021-10-19 11:53:15 +00:00
|
|
|
}
|
|
|
|
|
if (data.updateActions.length > 0) {
|
|
|
|
|
updateActions = data.updateActions;
|
|
|
|
|
let changedActions = [];
|
|
|
|
|
for (let i = 0; i < data.updateActions.length; i++) {
|
|
|
|
|
changedActions = jsActionTobeUpdated.actions.map(
|
|
|
|
|
(js: JSAction) =>
|
|
|
|
|
data.updateActions.find(
|
|
|
|
|
(update: JSAction) => update.id === js.id,
|
|
|
|
|
) || js,
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
updateCollection = true;
|
2021-10-19 11:53:15 +00:00
|
|
|
jsActionTobeUpdated.actions = changedActions;
|
|
|
|
|
}
|
|
|
|
|
if (data.deletedActions.length > 0) {
|
|
|
|
|
deletedActions = data.deletedActions;
|
|
|
|
|
const nonDeletedActions = jsActionTobeUpdated.actions.filter(
|
|
|
|
|
(js: JSAction) => {
|
|
|
|
|
return !data.deletedActions.find((deleted) => {
|
|
|
|
|
return deleted.id === js.id;
|
|
|
|
|
});
|
|
|
|
|
},
|
2021-09-21 06:02:45 +00:00
|
|
|
);
|
2021-11-08 06:49:22 +00:00
|
|
|
updateCollection = true;
|
2021-10-19 11:53:15 +00:00
|
|
|
jsActionTobeUpdated.actions = nonDeletedActions;
|
2021-09-21 06:02:45 +00:00
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
if (updateCollection) {
|
|
|
|
|
yield call(updateJSCollection, {
|
|
|
|
|
jsCollection: jsActionTobeUpdated,
|
|
|
|
|
newActions: newActions,
|
|
|
|
|
updatedActions: updateActions,
|
|
|
|
|
deletedActions: deletedActions,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 06:49:22 +00:00
|
|
|
export function* makeUpdateJSCollection(jsUpdates: Record<string, JSUpdate>) {
|
|
|
|
|
yield all(
|
|
|
|
|
Object.keys(jsUpdates).map((key) =>
|
|
|
|
|
call(handleEachUpdateJSCollection, jsUpdates[key]),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 11:53:15 +00:00
|
|
|
function* updateJSCollection(data: {
|
|
|
|
|
jsCollection: JSCollection;
|
2021-11-08 06:49:22 +00:00
|
|
|
newActions?: Partial<JSAction>[];
|
|
|
|
|
updatedActions?: JSAction[];
|
|
|
|
|
deletedActions?: JSAction[];
|
2021-10-19 11:53:15 +00:00
|
|
|
}) {
|
2021-09-08 17:32:22 +00:00
|
|
|
let jsAction = {};
|
|
|
|
|
const jsActionId = getJSCollectionIdFromURL();
|
|
|
|
|
if (jsActionId) {
|
|
|
|
|
jsAction = yield select(getJSCollection, jsActionId);
|
|
|
|
|
}
|
|
|
|
|
try {
|
2021-10-19 11:53:15 +00:00
|
|
|
const { deletedActions, jsCollection, newActions, updatedActions } = data;
|
2021-09-28 07:31:46 +00:00
|
|
|
if (jsCollection) {
|
|
|
|
|
const response = yield JSActionAPI.updateJSCollection(jsCollection);
|
2021-09-08 17:32:22 +00:00
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
if (isValidResponse) {
|
2021-11-08 06:49:22 +00:00
|
|
|
if (newActions && newActions.length) {
|
2021-09-28 07:31:46 +00:00
|
|
|
pushLogsForObjectUpdate(
|
|
|
|
|
newActions,
|
|
|
|
|
jsCollection,
|
|
|
|
|
createMessage(JS_FUNCTION_CREATE_SUCCESS),
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
if (updatedActions && updatedActions.length) {
|
2021-09-28 07:31:46 +00:00
|
|
|
pushLogsForObjectUpdate(
|
|
|
|
|
updatedActions,
|
|
|
|
|
jsCollection,
|
|
|
|
|
createMessage(JS_FUNCTION_UPDATE_SUCCESS),
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-11-08 06:49:22 +00:00
|
|
|
if (deletedActions && deletedActions.length) {
|
2021-09-28 07:31:46 +00:00
|
|
|
pushLogsForObjectUpdate(
|
|
|
|
|
deletedActions,
|
|
|
|
|
jsCollection,
|
|
|
|
|
createMessage(JS_FUNCTION_DELETE_SUCCESS),
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-08 17:32:22 +00:00
|
|
|
yield put(updateJSCollectionSuccess({ data: response?.data }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.UPDATE_JS_ACTION_ERROR,
|
|
|
|
|
payload: { error, data: jsAction },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleJSObjectNameChangeSuccessSaga(
|
|
|
|
|
action: ReduxAction<{ actionId: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const { actionId } = action.payload;
|
|
|
|
|
const actionObj = yield select(getJSCollection, actionId);
|
|
|
|
|
yield take(ReduxActionTypes.FETCH_JS_ACTIONS_FOR_PAGE_SUCCESS);
|
|
|
|
|
if (!actionObj) {
|
|
|
|
|
// Error case, log to sentry
|
|
|
|
|
Toaster.show({
|
|
|
|
|
text: createMessage(ERROR_JS_COLLECTION_RENAME_FAIL, ""),
|
|
|
|
|
variant: Variant.danger,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (actionObj.pluginType === PluginType.API) {
|
|
|
|
|
const params = getQueryParams();
|
|
|
|
|
if (params.editName) {
|
|
|
|
|
params.editName = "false";
|
|
|
|
|
}
|
2022-03-25 10:43:26 +00:00
|
|
|
history.push(
|
|
|
|
|
jsCollectionIdURL({
|
|
|
|
|
collectionId: actionId,
|
|
|
|
|
params,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:05:17 +00:00
|
|
|
export function* handleExecuteJSFunctionSaga(
|
2021-09-28 07:31:46 +00:00
|
|
|
data: ReduxAction<{
|
|
|
|
|
collectionName: string;
|
|
|
|
|
action: JSAction;
|
|
|
|
|
collectionId: string;
|
|
|
|
|
}>,
|
2021-09-08 17:32:22 +00:00
|
|
|
): any {
|
2021-09-28 07:31:46 +00:00
|
|
|
const { action, collectionId, collectionName } = data.payload;
|
2021-09-08 17:32:22 +00:00
|
|
|
const actionId = action.id;
|
2021-09-21 06:02:45 +00:00
|
|
|
try {
|
2021-12-23 14:17:20 +00:00
|
|
|
const result = yield call(executeFunction, collectionName, action);
|
2021-09-21 06:02:45 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.EXECUTE_JS_FUNCTION_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
results: result,
|
|
|
|
|
collectionId,
|
|
|
|
|
actionId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-09-28 07:31:46 +00:00
|
|
|
AppsmithConsole.info({
|
|
|
|
|
text: createMessage(JS_EXECUTION_SUCCESS),
|
|
|
|
|
source: {
|
|
|
|
|
type: ENTITY_TYPE.JSACTION,
|
|
|
|
|
name: collectionName + "." + action.name,
|
|
|
|
|
id: collectionId,
|
|
|
|
|
},
|
|
|
|
|
state: { response: result },
|
|
|
|
|
});
|
2021-09-21 06:02:45 +00:00
|
|
|
} catch (e) {
|
2021-09-28 07:31:46 +00:00
|
|
|
AppsmithConsole.addError({
|
|
|
|
|
id: actionId,
|
|
|
|
|
logType: LOG_TYPE.ACTION_EXECUTION_ERROR,
|
|
|
|
|
text: createMessage(JS_EXECUTION_FAILURE),
|
|
|
|
|
source: {
|
|
|
|
|
type: ENTITY_TYPE.JSACTION,
|
|
|
|
|
name: collectionName + "." + action.name,
|
|
|
|
|
id: collectionId,
|
|
|
|
|
},
|
|
|
|
|
messages: [
|
|
|
|
|
{
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: PLATFORM_ERROR.PLUGIN_EXECUTION,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2021-09-21 06:02:45 +00:00
|
|
|
Toaster.show({
|
2021-09-28 07:31:46 +00:00
|
|
|
text: e.message || createMessage(JS_EXECUTION_FAILURE_TOASTER),
|
2021-09-21 06:02:45 +00:00
|
|
|
variant: Variant.danger,
|
|
|
|
|
showDebugButton: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-08 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 06:49:22 +00:00
|
|
|
function* handleUpdateJSCollectionBody(
|
2021-12-07 09:45:18 +00:00
|
|
|
actionPayload: ReduxAction<{ body: string; id: string; isReplay: boolean }>,
|
2021-11-08 06:49:22 +00:00
|
|
|
) {
|
|
|
|
|
const jsCollection = yield select(getJSCollection, actionPayload.payload.id);
|
2022-03-17 12:05:17 +00:00
|
|
|
jsCollection["body"] = actionPayload.payload.body;
|
2021-11-08 06:49:22 +00:00
|
|
|
try {
|
|
|
|
|
if (jsCollection) {
|
|
|
|
|
const response = yield JSActionAPI.updateJSCollection(jsCollection);
|
|
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
if (isValidResponse) {
|
|
|
|
|
yield put(updateJSCollectionBodySuccess({ data: response?.data }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.UPDATE_JS_ACTION_BODY_ERROR,
|
|
|
|
|
payload: { error, data: jsCollection },
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-12-07 09:45:18 +00:00
|
|
|
if (!actionPayload.payload.isReplay)
|
|
|
|
|
yield put(
|
|
|
|
|
updateReplayEntity(
|
|
|
|
|
actionPayload.payload.id,
|
|
|
|
|
{
|
|
|
|
|
id: actionPayload.payload.id,
|
|
|
|
|
body: actionPayload.payload.body,
|
|
|
|
|
},
|
|
|
|
|
ENTITY_TYPE.JSACTION,
|
|
|
|
|
),
|
|
|
|
|
);
|
2021-11-08 06:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-08 17:32:22 +00:00
|
|
|
function* handleRefactorJSActionNameSaga(
|
|
|
|
|
data: ReduxAction<{
|
2021-10-19 11:53:15 +00:00
|
|
|
refactorAction: RefactorAction;
|
|
|
|
|
actionCollection: JSCollection;
|
2021-09-08 17:32:22 +00:00
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
const pageResponse = yield call(PageApi.fetchPage, {
|
2021-10-19 11:53:15 +00:00
|
|
|
id: data.payload.refactorAction.pageId,
|
2021-09-08 17:32:22 +00:00
|
|
|
});
|
|
|
|
|
const isPageRequestSuccessful = yield validateResponse(pageResponse);
|
|
|
|
|
if (isPageRequestSuccessful) {
|
|
|
|
|
// get the layoutId from the page response
|
|
|
|
|
const layoutId = pageResponse.data.layouts[0].id;
|
2021-10-19 11:53:15 +00:00
|
|
|
const requestData = {
|
|
|
|
|
refactorAction: {
|
|
|
|
|
...data.payload.refactorAction,
|
|
|
|
|
layoutId: layoutId,
|
|
|
|
|
},
|
|
|
|
|
actionCollection: data.payload.actionCollection,
|
|
|
|
|
};
|
2021-09-08 17:32:22 +00:00
|
|
|
// call to refactor action
|
|
|
|
|
try {
|
2021-10-19 11:53:15 +00:00
|
|
|
const refactorResponse = yield JSActionAPI.updateJSCollectionActionRefactor(
|
|
|
|
|
requestData,
|
|
|
|
|
);
|
2021-09-08 17:32:22 +00:00
|
|
|
|
|
|
|
|
const isRefactorSuccessful = yield validateResponse(refactorResponse);
|
|
|
|
|
|
|
|
|
|
const currentPageId = yield select(getCurrentPageId);
|
|
|
|
|
|
|
|
|
|
if (isRefactorSuccessful) {
|
2021-10-19 11:53:15 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.REFACTOR_JS_ACTION_NAME_SUCCESS,
|
|
|
|
|
payload: { collectionId: data.payload.actionCollection.id },
|
|
|
|
|
});
|
|
|
|
|
if (currentPageId === data.payload.refactorAction.pageId) {
|
2021-09-08 17:32:22 +00:00
|
|
|
yield updateCanvasWithDSL(
|
|
|
|
|
refactorResponse.data,
|
2021-10-19 11:53:15 +00:00
|
|
|
data.payload.refactorAction.pageId,
|
2021-09-08 17:32:22 +00:00
|
|
|
layoutId,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.REFACTOR_JS_ACTION_NAME_ERROR,
|
2021-10-19 11:53:15 +00:00
|
|
|
payload: { collectionId: data.payload.actionCollection.id },
|
2021-09-08 17:32:22 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 12:05:17 +00:00
|
|
|
function* setFunctionPropertySaga(
|
|
|
|
|
data: ReduxAction<SetFunctionPropertyPayload>,
|
|
|
|
|
) {
|
|
|
|
|
const { action, propertyName, value } = data.payload;
|
|
|
|
|
if (!action.id) return;
|
|
|
|
|
const actionId = action.id;
|
|
|
|
|
if (propertyName === "executeOnLoad") {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.TOGGLE_FUNCTION_EXECUTE_ON_LOAD_INIT,
|
|
|
|
|
payload: {
|
|
|
|
|
collectionId: action.collectionId,
|
|
|
|
|
actionId,
|
|
|
|
|
shouldExecute: value,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
yield put(updateJSFunction({ ...data.payload }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleUpdateJSFunctionPropertySaga(
|
|
|
|
|
data: ReduxAction<SetFunctionPropertyPayload>,
|
|
|
|
|
) {
|
|
|
|
|
const { action, propertyName, value } = data.payload;
|
|
|
|
|
if (!action.id) return;
|
|
|
|
|
const actionId = action.id;
|
|
|
|
|
let collection: JSCollection;
|
|
|
|
|
if (action.collectionId) {
|
|
|
|
|
collection = yield select(getJSCollection, action.collectionId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const actions: JSAction[] = collection.actions;
|
|
|
|
|
const updatedActions = actions.map((jsAction: JSAction) => {
|
|
|
|
|
if (jsAction.id === actionId) {
|
|
|
|
|
set(jsAction, propertyName, value);
|
|
|
|
|
return jsAction;
|
|
|
|
|
}
|
|
|
|
|
return jsAction;
|
|
|
|
|
});
|
|
|
|
|
collection.actions = updatedActions;
|
|
|
|
|
const response = yield JSActionAPI.updateJSCollection(collection);
|
|
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
if (isValidResponse) {
|
|
|
|
|
const fieldToBeUpdated = propertyName.replace(
|
|
|
|
|
"actionConfiguration",
|
|
|
|
|
"config",
|
|
|
|
|
);
|
|
|
|
|
AppsmithConsole.info({
|
|
|
|
|
logType: LOG_TYPE.ACTION_UPDATE,
|
|
|
|
|
text: "Configuration updated",
|
|
|
|
|
source: {
|
|
|
|
|
type: ENTITY_TYPE.JSACTION,
|
|
|
|
|
name: collection.name + "." + action.name,
|
|
|
|
|
id: actionId,
|
|
|
|
|
propertyPath: fieldToBeUpdated,
|
|
|
|
|
},
|
|
|
|
|
state: {
|
|
|
|
|
[fieldToBeUpdated]: value,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.UPDATE_JS_FUNCTION_PROPERTY_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
collection,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.UPDATE_JS_FUNCTION_PROPERTY_ERROR,
|
|
|
|
|
payload: collection,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* toggleFunctionExecuteOnLoadSaga(
|
|
|
|
|
action: ReduxAction<{
|
|
|
|
|
collectionId: string;
|
|
|
|
|
actionId: string;
|
|
|
|
|
shouldExecute: boolean;
|
|
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const { actionId, collectionId, shouldExecute } = action.payload;
|
|
|
|
|
const collection = yield select(getJSCollection, collectionId);
|
|
|
|
|
const jsAction = collection.actions.find(
|
|
|
|
|
(action: JSAction) => actionId === action.id,
|
|
|
|
|
);
|
|
|
|
|
const response = yield call(
|
|
|
|
|
ActionAPI.toggleActionExecuteOnLoad,
|
|
|
|
|
actionId,
|
|
|
|
|
shouldExecute,
|
|
|
|
|
);
|
|
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
if (isValidResponse) {
|
|
|
|
|
AppsmithConsole.info({
|
|
|
|
|
logType: LOG_TYPE.ACTION_UPDATE,
|
|
|
|
|
text: "Configuration updated",
|
|
|
|
|
source: {
|
|
|
|
|
type: ENTITY_TYPE.JSACTION,
|
|
|
|
|
name: collection.name + "." + jsAction.name,
|
|
|
|
|
id: actionId,
|
|
|
|
|
propertyPath: "executeOnLoad",
|
|
|
|
|
},
|
|
|
|
|
state: {
|
|
|
|
|
["executeOnLoad"]: shouldExecute,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.TOGGLE_FUNCTION_EXECUTE_ON_LOAD_SUCCESS,
|
|
|
|
|
payload: {
|
|
|
|
|
actionId: actionId,
|
|
|
|
|
collectionId: collectionId,
|
|
|
|
|
executeOnLoad: shouldExecute,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.TOGGLE_ACTION_EXECUTE_ON_LOAD_ERROR,
|
|
|
|
|
payload: error,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 17:32:22 +00:00
|
|
|
export default function* root() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.CREATE_NEW_JS_ACTION,
|
|
|
|
|
handleCreateNewJsActionSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.CREATE_JS_ACTION_SUCCESS,
|
|
|
|
|
handleJSCollectionCreatedSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.SAVE_JS_COLLECTION_NAME_SUCCESS,
|
|
|
|
|
handleJSObjectNameChangeSuccessSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.EXECUTE_JS_FUNCTION_INIT,
|
|
|
|
|
handleExecuteJSFunctionSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.REFACTOR_JS_ACTION_NAME,
|
|
|
|
|
handleRefactorJSActionNameSaga,
|
|
|
|
|
),
|
2021-11-08 06:49:22 +00:00
|
|
|
debounce(
|
|
|
|
|
100,
|
|
|
|
|
ReduxActionTypes.UPDATE_JS_ACTION_BODY_INIT,
|
|
|
|
|
handleUpdateJSCollectionBody,
|
|
|
|
|
),
|
2022-03-17 12:05:17 +00:00
|
|
|
takeEvery(ReduxActionTypes.SET_FUNCTION_PROPERTY, setFunctionPropertySaga),
|
|
|
|
|
takeLatest(
|
|
|
|
|
ReduxActionTypes.UPDATE_JS_FUNCTION_PROPERTY_INIT,
|
|
|
|
|
handleUpdateJSFunctionPropertySaga,
|
|
|
|
|
),
|
|
|
|
|
takeLatest(
|
|
|
|
|
ReduxActionTypes.TOGGLE_FUNCTION_EXECUTE_ON_LOAD_INIT,
|
|
|
|
|
toggleFunctionExecuteOnLoadSaga,
|
|
|
|
|
),
|
2021-09-08 17:32:22 +00:00
|
|
|
]);
|
|
|
|
|
}
|