PromucFlow_constructor/app/client/src/sagas/ActionSagas.ts

544 lines
15 KiB
TypeScript
Raw Normal View History

2019-10-21 15:12:45 +00:00
import {
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
2019-11-25 05:07:27 +00:00
} from "constants/ReduxActionConstants";
import {
all,
call,
put,
2020-02-18 10:41:52 +00:00
select,
takeEvery,
takeLatest,
} from "redux-saga/effects";
2019-11-28 03:56:44 +00:00
import {
2020-02-18 10:41:52 +00:00
EventType,
ExecuteActionPayload,
2019-11-28 03:56:44 +00:00
PageAction,
} from "constants/ActionConstants";
2019-10-21 15:12:45 +00:00
import ActionAPI, {
2019-11-11 11:42:52 +00:00
ActionApiResponse,
2019-10-21 15:12:45 +00:00
ActionCreateUpdateResponse,
2019-11-12 09:43:13 +00:00
ActionResponse,
2019-10-21 15:12:45 +00:00
ExecuteActionRequest,
2020-02-18 10:41:52 +00:00
PaginationField,
2019-11-20 10:57:05 +00:00
Property,
2019-10-21 15:12:45 +00:00
RestAction,
2019-11-25 05:07:27 +00:00
} from "api/ActionAPI";
2020-01-30 13:23:04 +00:00
import { AppState } from "reducers";
2019-10-21 15:12:45 +00:00
import _ from "lodash";
2019-11-25 05:07:27 +00:00
import { mapToPropList } from "utils/AppsmithUtils";
2020-02-03 12:19:10 +00:00
import { AppToaster } from "components/editorComponents/ToastComponent";
2019-11-25 05:07:27 +00:00
import { GenericApiResponse } from "api/ApiResponses";
import {
2020-01-24 09:54:40 +00:00
copyActionError,
copyActionSuccess,
createActionSuccess,
deleteActionSuccess,
2020-02-18 10:41:52 +00:00
executeApiActionRequest,
executeApiActionSuccess,
FetchActionsPayload,
2020-01-24 09:54:40 +00:00
moveActionError,
moveActionSuccess,
runApiAction,
updateActionSuccess,
2019-11-25 05:07:27 +00:00
} from "actions/actionActions";
2019-11-20 10:57:05 +00:00
import {
getDynamicBindings,
getDynamicValue,
2019-11-20 10:57:05 +00:00
isDynamicValue,
2020-01-27 13:53:33 +00:00
removeBindingsFromObject,
2019-11-25 05:07:27 +00:00
} from "utils/DynamicBindingUtils";
2019-11-13 07:34:59 +00:00
import { validateResponse } from "./ErrorSagas";
2019-11-20 10:57:05 +00:00
import { getFormData } from "selectors/formSelectors";
2019-11-25 05:07:27 +00:00
import { API_EDITOR_FORM_NAME } from "constants/forms";
2020-01-30 13:23:04 +00:00
import { executeAction, executeActionError } from "actions/widgetActions";
2020-02-18 10:41:52 +00:00
import { getParsedDataTree } from "selectors/dataTreeSelectors";
2019-12-23 12:12:58 +00:00
import { transformRestAction } from "transformers/RestActionTransformer";
2020-01-30 13:23:04 +00:00
import { getActionResponses } from "selectors/entitiesSelector";
2020-02-18 10:41:52 +00:00
import {
ActionDescription,
RunActionPayload,
} from "entities/DataTree/dataTreeFactory";
import {
getCurrentApplicationId,
getPageList,
} from "selectors/editorSelectors";
import history from "utils/history";
import {
BUILDER_PAGE_URL,
getApplicationViewerPageURL,
} from "constants/routes";
import { ToastType } from "react-toastify";
2019-10-21 15:12:45 +00:00
2019-11-25 09:15:11 +00:00
export const getAction = (
2019-11-05 05:09:50 +00:00
state: AppState,
actionId: string,
): RestAction | undefined => {
2020-01-30 13:23:04 +00:00
const action = _.find(state.entities.actions, a => a.config.id === actionId);
return action ? action.config : undefined;
2019-10-21 15:12:45 +00:00
};
2019-11-12 09:43:13 +00:00
const createActionResponse = (response: ActionApiResponse): ActionResponse => ({
...response.data,
...response.clientMeta,
});
const createActionErrorResponse = (
response: ActionApiResponse,
): ActionResponse => ({
body: response.responseMeta.error || { error: "Error" },
statusCode: response.responseMeta.error
? response.responseMeta.error.code
: "Error",
headers: {},
duration: "0",
size: "0",
});
2019-12-02 09:50:25 +00:00
export function* evaluateDynamicBoundValueSaga(path: string): any {
2020-01-17 09:28:26 +00:00
const tree = yield select(getParsedDataTree);
2020-02-18 10:41:52 +00:00
const dynamicResult = getDynamicValue(`{{${path}}}`, tree);
return dynamicResult.result;
2019-10-21 15:12:45 +00:00
}
2019-11-20 10:57:05 +00:00
export function* getActionParams(jsonPathKeys: string[] | undefined) {
if (_.isNil(jsonPathKeys)) return [];
const values: any = _.flatten(
yield all(
jsonPathKeys.map((jsonPath: string) => {
2019-12-02 09:50:25 +00:00
return call(evaluateDynamicBoundValueSaga, jsonPath);
2019-11-20 10:57:05 +00:00
}),
),
);
const dynamicBindings: Record<string, string> = {};
jsonPathKeys.forEach((key, i) => {
let value = values[i];
if (typeof value === "object") value = JSON.stringify(value);
dynamicBindings[key] = value;
2019-11-20 10:57:05 +00:00
});
return mapToPropList(dynamicBindings);
}
2020-02-18 10:41:52 +00:00
// function* executeJSActionSaga(jsAction: ExecuteJSActionPayload) {
// const tree = yield select(getParsedDataTree);
// const result = JSExecutionManagerSingleton.evaluateSync(
// jsAction.jsFunction,
// tree,
// );
//
// yield put({
// type: ReduxActionTypes.SAVE_JS_EXECUTION_RECORD,
// payload: {
// [jsAction.jsFunctionId]: result,
// },
// });
// }
2019-11-28 03:56:44 +00:00
2020-02-07 02:32:52 +00:00
export function* executeAPIQueryActionSaga(
2020-02-18 10:41:52 +00:00
apiAction: RunActionPayload,
event: EventType,
2020-02-07 02:32:52 +00:00
) {
2020-02-18 10:41:52 +00:00
const { actionId, onSuccess, onError } = apiAction;
2019-11-13 07:34:59 +00:00
try {
2020-02-18 10:41:52 +00:00
yield put(executeApiActionRequest({ id: apiAction.actionId }));
const api: RestAction = yield select(getAction, actionId);
2019-11-20 10:57:05 +00:00
const params: Property[] = yield call(getActionParams, api.jsonPathKeys);
2020-02-18 10:41:52 +00:00
const pagination =
event === EventType.ON_NEXT_PAGE
? "NEXT"
: event === EventType.ON_PREV_PAGE
? "PREV"
: undefined;
2019-11-13 07:34:59 +00:00
const executeActionRequest: ExecuteActionRequest = {
2020-02-18 10:41:52 +00:00
action: { id: actionId },
2019-11-20 10:57:05 +00:00
params,
2020-02-18 10:41:52 +00:00
paginationField: pagination,
2019-11-13 07:34:59 +00:00
};
const response: ActionApiResponse = yield ActionAPI.executeAction(
executeActionRequest,
2019-10-21 15:12:45 +00:00
);
2019-11-13 07:34:59 +00:00
let payload = createActionResponse(response);
if (response.responseMeta && response.responseMeta.error) {
payload = createActionErrorResponse(response);
2020-02-18 10:41:52 +00:00
if (onError) {
yield put(
executeAction({
dynamicString: onError,
event: {
type: EventType.ON_ERROR,
},
responseData: payload,
}),
);
2019-11-13 07:34:59 +00:00
}
2020-01-30 13:23:04 +00:00
yield put(
executeActionError({
2020-02-18 10:41:52 +00:00
actionId,
2020-01-30 13:23:04 +00:00
error: response.responseMeta.error,
}),
);
2019-11-13 07:34:59 +00:00
} else {
2020-02-18 10:41:52 +00:00
yield put(
executeApiActionSuccess({ id: apiAction.actionId, response: payload }),
);
if (onSuccess) {
yield put(
executeAction({
dynamicString: onSuccess,
event: {
type: EventType.ON_SUCCESS,
},
responseData: payload,
}),
);
2019-11-13 07:34:59 +00:00
}
}
2019-11-13 07:34:59 +00:00
return response;
} catch (error) {
2020-01-30 13:23:04 +00:00
yield put(
executeActionError({
2020-02-18 10:41:52 +00:00
actionId: actionId,
2020-01-30 13:23:04 +00:00
error,
}),
);
2020-02-18 10:41:52 +00:00
if (onError) {
yield put(
executeAction({
dynamicString: `{{${onError}}}`,
event: {
type: EventType.ON_ERROR,
},
responseData: {},
}),
);
}
2019-11-06 06:35:15 +00:00
}
2019-10-21 15:12:45 +00:00
}
2020-02-18 10:41:52 +00:00
function* navigateActionSaga(action: { pageName: string }, event: EventType) {
const pageList = yield select(getPageList);
const applicationId = yield select(getCurrentApplicationId);
const page = _.find(pageList, { pageName: action.pageName });
if (page) {
// TODO need to make this check via RENDER_MODE;
const path = history.location.pathname.endsWith("/edit")
? BUILDER_PAGE_URL(applicationId, page.pageId)
: getApplicationViewerPageURL(applicationId, page.pageId);
history.push(path);
}
}
2020-02-18 10:41:52 +00:00
export function* executeActionTriggers(
trigger: ActionDescription<any>,
event: EventType,
) {
switch (trigger.type) {
case "RUN_ACTION":
yield call(executeAPIQueryActionSaga, trigger.payload, event);
break;
case "NAVIGATE_TO":
yield call(navigateActionSaga, trigger.payload, event);
break;
case "NAVIGATE_TO_URL":
if (trigger.payload.url) {
window.location.href = trigger.payload.url;
}
2020-02-18 10:41:52 +00:00
break;
case "SHOW_ALERT":
AppToaster.show({
message: trigger.payload.message,
type: trigger.payload.style,
});
break;
default:
yield put(
executeActionError({
error: "Trigger type unknown",
actionId: "",
}),
);
}
}
2020-02-18 10:41:52 +00:00
export function* executeAppAction(action: ReduxAction<ExecuteActionPayload>) {
const { dynamicString, event, responseData } = action.payload;
const tree = yield select(getParsedDataTree);
const { triggers } = getDynamicValue(dynamicString, tree, responseData, true);
if (triggers) {
yield all(
triggers.map(trigger => call(executeActionTriggers, trigger, event.type)),
2020-01-30 13:23:04 +00:00
);
2019-10-21 15:12:45 +00:00
}
}
export function* createActionSaga(actionPayload: ReduxAction<RestAction>) {
2019-11-13 07:34:59 +00:00
try {
const response: ActionCreateUpdateResponse = yield ActionAPI.createAPI(
actionPayload.payload,
);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AppToaster.show({
message: `${actionPayload.payload.name} Action created`,
2020-02-18 10:41:52 +00:00
type: ToastType.SUCCESS,
2019-11-13 07:34:59 +00:00
});
yield put(createActionSuccess(response.data));
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.CREATE_ACTION_ERROR,
2020-01-24 09:54:40 +00:00
payload: actionPayload.payload,
2019-10-21 15:12:45 +00:00
});
}
}
export function* fetchActionsSaga(action: ReduxAction<FetchActionsPayload>) {
2019-11-13 07:34:59 +00:00
try {
2020-01-24 09:54:40 +00:00
const { applicationId } = action.payload;
const response: GenericApiResponse<RestAction[]> = yield ActionAPI.fetchActions(
2020-01-24 09:54:40 +00:00
applicationId,
);
2019-11-13 07:34:59 +00:00
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_ACTIONS_SUCCESS,
payload: response.data,
});
}
} catch (error) {
2019-10-21 15:12:45 +00:00
yield put({
type: ReduxActionErrorTypes.FETCH_ACTIONS_ERROR,
2019-11-13 07:34:59 +00:00
payload: { error },
2019-10-21 15:12:45 +00:00
});
}
}
export function* updateActionSaga(
actionPayload: ReduxAction<{ data: RestAction }>,
) {
2019-11-13 07:34:59 +00:00
try {
2019-12-23 12:12:58 +00:00
const { data } = actionPayload.payload;
const action = transformRestAction(data);
2019-11-13 07:34:59 +00:00
const response: GenericApiResponse<RestAction> = yield ActionAPI.updateAPI(
2019-12-23 12:12:58 +00:00
action,
2019-11-13 07:34:59 +00:00
);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AppToaster.show({
message: `${actionPayload.payload.data.name} Action updated`,
2020-02-18 10:41:52 +00:00
type: ToastType.SUCCESS,
2019-11-13 07:34:59 +00:00
});
yield put(updateActionSuccess({ data: response.data }));
yield put(runApiAction(data.id));
2019-11-13 07:34:59 +00:00
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.UPDATE_ACTION_ERROR,
2019-12-11 15:14:38 +00:00
payload: { error, id: actionPayload.payload.data.id },
2019-10-21 15:12:45 +00:00
});
}
}
export function* deleteActionSaga(actionPayload: ReduxAction<{ id: string }>) {
2019-11-13 07:34:59 +00:00
try {
const id = actionPayload.payload.id;
const response: GenericApiResponse<RestAction> = yield ActionAPI.deleteAction(
id,
);
2019-11-13 07:34:59 +00:00
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AppToaster.show({
message: `${response.data.name} Action deleted`,
2020-02-18 10:41:52 +00:00
type: ToastType.SUCCESS,
2019-11-13 07:34:59 +00:00
});
yield put(deleteActionSuccess({ id }));
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_ACTION_ERROR,
2019-12-11 15:14:38 +00:00
payload: { error, id: actionPayload.payload.id },
2019-10-21 15:12:45 +00:00
});
}
}
2020-02-07 02:32:52 +00:00
export function* runApiActionSaga(
reduxAction: ReduxAction<{
id: string;
paginationField: PaginationField;
}>,
) {
2019-11-20 10:57:05 +00:00
try {
const {
values,
dirty,
valid,
2020-02-07 02:32:52 +00:00
}: {
values: RestAction;
dirty: boolean;
valid: boolean;
} = yield select(getFormData, API_EDITOR_FORM_NAME);
const actionObject: PageAction = yield select(getAction, values.id);
2019-11-20 10:57:05 +00:00
let action: ExecuteActionRequest["action"] = { id: values.id };
let jsonPathKeys = actionObject.jsonPathKeys;
2019-11-20 10:57:05 +00:00
if (!valid) {
console.error("Form error");
return;
}
if (dirty) {
2020-02-07 02:32:52 +00:00
action = _.omit(transformRestAction(values), "id") as RestAction;
2019-11-20 10:57:05 +00:00
const actionString = JSON.stringify(action);
if (isDynamicValue(actionString)) {
const { paths } = getDynamicBindings(actionString);
// Replace cause the existing keys could have been updated
jsonPathKeys = paths.filter(path => !!path);
2019-11-20 10:57:05 +00:00
} else {
jsonPathKeys = [];
}
}
2020-02-07 02:32:52 +00:00
const { paginationField } = reduxAction.payload;
2019-11-20 10:57:05 +00:00
const params = yield call(getActionParams, jsonPathKeys);
const response: ActionApiResponse = yield ActionAPI.executeAction({
action,
params,
2020-02-07 02:32:52 +00:00
paginationField,
2019-11-20 10:57:05 +00:00
});
let payload = createActionResponse(response);
if (response.responseMeta && response.responseMeta.error) {
payload = createActionErrorResponse(response);
}
const id = values.id || "DRY_RUN";
yield put({
type: ReduxActionTypes.RUN_API_SUCCESS,
payload: { [id]: payload },
});
} catch (error) {
yield put({
type: ReduxActionErrorTypes.RUN_API_ERROR,
2020-02-07 02:32:52 +00:00
payload: { error, id: reduxAction.payload },
2019-11-20 10:57:05 +00:00
});
}
}
2020-01-30 10:55:37 +00:00
function* executePageLoadActionsSaga(action: ReduxAction<PageAction[][]>) {
const pageActions = action.payload;
2020-02-18 10:41:52 +00:00
const actionPayloads: RunActionPayload[][] = pageActions.map(actionSet =>
2020-01-30 10:55:37 +00:00
actionSet.map(action => ({
actionId: action.id,
2020-02-18 10:41:52 +00:00
onSuccess: "",
onError: "",
2020-01-30 10:55:37 +00:00
})),
);
for (const actionSet of actionPayloads) {
2020-01-31 11:13:16 +00:00
const apiResponses = yield select(getActionResponses);
2020-01-30 10:55:37 +00:00
const filteredSet = actionSet.filter(
2020-01-31 11:13:16 +00:00
action => !apiResponses[action.actionId],
2020-01-30 10:55:37 +00:00
);
2020-02-18 10:41:52 +00:00
yield* yield all(
filteredSet.map(a =>
call(executeAPIQueryActionSaga, a, EventType.ON_PAGE_LOAD),
),
);
}
}
2020-01-24 09:54:40 +00:00
function* moveActionSaga(
action: ReduxAction<{
id: string;
destinationPageId: string;
originalPageId: string;
name: string;
}>,
) {
const drafts = yield select(state => state.ui.apiPane.drafts);
const dirty = action.payload.id in drafts;
const actionObject: RestAction = dirty
? drafts[action.payload.id]
: yield select(getAction, action.payload.id);
2020-01-27 13:53:33 +00:00
const withoutBindings = removeBindingsFromObject(actionObject);
2020-01-24 09:54:40 +00:00
try {
const response = yield ActionAPI.moveAction({
2020-01-27 13:53:33 +00:00
action: {
...withoutBindings,
name: action.payload.name,
},
2020-01-24 09:54:40 +00:00
destinationPageId: action.payload.destinationPageId,
});
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AppToaster.show({
message: `${response.data.name} Action moved`,
2020-02-18 10:41:52 +00:00
type: ToastType.SUCCESS,
2020-01-24 09:54:40 +00:00
});
}
2020-01-27 13:53:33 +00:00
yield put(moveActionSuccess(response.data));
2020-01-24 09:54:40 +00:00
} catch (e) {
AppToaster.show({
message: `Error while moving action ${actionObject.name}`,
2020-02-18 10:41:52 +00:00
type: ToastType.ERROR,
2020-01-24 09:54:40 +00:00
});
yield put(
moveActionError({
id: action.payload.id,
originalPageId: action.payload.originalPageId,
}),
);
}
}
function* copyActionSaga(
action: ReduxAction<{ id: string; destinationPageId: string; name: string }>,
) {
const drafts = yield select(state => state.ui.apiPane.drafts);
const dirty = action.payload.id in drafts;
2020-01-27 13:53:33 +00:00
let actionObject = dirty
2020-01-24 09:54:40 +00:00
? drafts[action.payload.id]
: yield select(getAction, action.payload.id);
2020-01-27 13:53:33 +00:00
if (action.payload.destinationPageId !== actionObject.pageId) {
actionObject = removeBindingsFromObject(actionObject);
}
2020-01-24 09:54:40 +00:00
try {
const copyAction = {
...(_.omit(actionObject, "id") as RestAction),
name: action.payload.name,
pageId: action.payload.destinationPageId,
};
const response = yield ActionAPI.createAPI(copyAction);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AppToaster.show({
message: `${actionObject.name} Action copied`,
2020-02-18 10:41:52 +00:00
type: ToastType.SUCCESS,
2020-01-24 09:54:40 +00:00
});
}
yield put(copyActionSuccess(response.data));
} catch (e) {
AppToaster.show({
message: `Error while copying action ${actionObject.name}`,
2020-02-18 10:41:52 +00:00
type: ToastType.ERROR,
2020-01-24 09:54:40 +00:00
});
yield put(copyActionError(action.payload));
}
}
2019-10-21 15:12:45 +00:00
export function* watchActionSagas() {
yield all([
takeEvery(ReduxActionTypes.FETCH_ACTIONS_INIT, fetchActionsSaga),
2020-02-18 10:41:52 +00:00
takeLatest(ReduxActionTypes.EXECUTE_ACTION, executeAppAction),
2019-11-20 10:57:05 +00:00
takeLatest(ReduxActionTypes.RUN_API_REQUEST, runApiActionSaga),
takeLatest(ReduxActionTypes.CREATE_ACTION_INIT, createActionSaga),
takeLatest(ReduxActionTypes.UPDATE_ACTION_INIT, updateActionSaga),
takeLatest(ReduxActionTypes.DELETE_ACTION_INIT, deleteActionSaga),
takeLatest(
ReduxActionTypes.EXECUTE_PAGE_LOAD_ACTIONS,
executePageLoadActionsSaga,
),
2020-01-24 09:54:40 +00:00
takeLatest(ReduxActionTypes.MOVE_ACTION_INIT, moveActionSaga),
takeLatest(ReduxActionTypes.COPY_ACTION_INIT, copyActionSaga),
2019-10-21 15:12:45 +00:00
]);
}