2019-11-25 09:15:11 +00:00
|
|
|
/**
|
|
|
|
|
* Handles the Api pane ui state. It looks into the routing based on actions too
|
|
|
|
|
* */
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { all, select, put, takeEvery, take, call } from "redux-saga/effects";
|
|
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionWithMeta,
|
|
|
|
|
ReduxFormActionTypes,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
import { getFormData } from "selectors/formSelectors";
|
2020-04-28 10:47:59 +00:00
|
|
|
import { API_EDITOR_FORM_NAME } from "constants/forms";
|
2020-04-20 08:26:19 +00:00
|
|
|
import {
|
|
|
|
|
DEFAULT_API_ACTION,
|
|
|
|
|
POST_BODY_FORMAT_OPTIONS,
|
|
|
|
|
REST_PLUGIN_PACKAGE_NAME,
|
2020-05-07 07:56:37 +00:00
|
|
|
POST_BODY_FORMATS,
|
2020-05-13 18:20:53 +00:00
|
|
|
CONTENT_TYPE,
|
2020-04-20 08:26:19 +00:00
|
|
|
} from "constants/ApiEditorConstants";
|
2019-11-25 09:15:11 +00:00
|
|
|
import history from "utils/history";
|
2020-04-28 10:47:59 +00:00
|
|
|
import {
|
|
|
|
|
API_EDITOR_ID_URL,
|
|
|
|
|
API_EDITOR_URL,
|
|
|
|
|
getProviderTemplatesURL,
|
|
|
|
|
} from "constants/routes";
|
2020-02-12 09:36:08 +00:00
|
|
|
import {
|
|
|
|
|
getCurrentApplicationId,
|
|
|
|
|
getCurrentPageId,
|
2020-03-24 14:05:19 +00:00
|
|
|
getIsEditorInitialized,
|
2020-04-28 10:47:59 +00:00
|
|
|
getLastSelectedPage,
|
2020-02-12 09:36:08 +00:00
|
|
|
} from "selectors/editorSelectors";
|
2020-05-07 07:56:37 +00:00
|
|
|
import { initialize, autofill, change } from "redux-form";
|
2019-11-25 09:15:11 +00:00
|
|
|
import { getAction } from "./ActionSagas";
|
|
|
|
|
import { AppState } from "reducers";
|
2019-12-23 12:12:58 +00:00
|
|
|
import { Property, RestAction } from "api/ActionAPI";
|
2019-11-25 09:15:11 +00:00
|
|
|
import { changeApi } from "actions/apiPaneActions";
|
|
|
|
|
import {
|
|
|
|
|
API_PATH_START_WITH_SLASH_ERROR,
|
|
|
|
|
FIELD_REQUIRED_ERROR,
|
|
|
|
|
UNIQUE_NAME_ERROR,
|
|
|
|
|
VALID_FUNCTION_NAME_ERROR,
|
|
|
|
|
} from "constants/messages";
|
2020-04-20 08:26:19 +00:00
|
|
|
import { createNewApiName } from "utils/AppsmithUtils";
|
|
|
|
|
import { getPluginIdOfPackageName } from "sagas/selectors";
|
|
|
|
|
import { getActions } from "selectors/entitiesSelector";
|
|
|
|
|
import { ActionData } from "reducers/entityReducers/actionsReducer";
|
|
|
|
|
import { createActionRequest } from "actions/actionActions";
|
2019-11-25 09:15:11 +00:00
|
|
|
|
|
|
|
|
const getApiDraft = (state: AppState, id: string) => {
|
|
|
|
|
const drafts = state.ui.apiPane.drafts;
|
|
|
|
|
if (id in drafts) return drafts[id];
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-20 08:26:19 +00:00
|
|
|
const getActionConfigs = (state: AppState): ActionData["config"][] =>
|
2020-01-30 13:23:04 +00:00
|
|
|
state.entities.actions.map(a => a.config);
|
2019-11-25 09:15:11 +00:00
|
|
|
|
|
|
|
|
const getLastUsedAction = (state: AppState) => state.ui.apiPane.lastUsed;
|
2020-04-28 10:47:59 +00:00
|
|
|
const getLastUsedEditorPage = (state: AppState) =>
|
|
|
|
|
state.ui.apiPane.lastUsedEditorPage;
|
|
|
|
|
const getLastUsedProvider = (state: AppState) =>
|
|
|
|
|
state.ui.providers.lastUsedProviderId;
|
2019-11-25 09:15:11 +00:00
|
|
|
|
|
|
|
|
function* initApiPaneSaga(actionPayload: ReduxAction<{ id?: string }>) {
|
2020-03-24 14:05:19 +00:00
|
|
|
const isInitialized = yield select(getIsEditorInitialized);
|
|
|
|
|
while (!isInitialized) {
|
|
|
|
|
yield take(ReduxActionTypes.INITIALIZE_EDITOR_SUCCESS);
|
2019-11-25 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
const urlId = actionPayload.payload.id;
|
|
|
|
|
const lastUsedId = yield select(getLastUsedAction);
|
2020-04-28 10:47:59 +00:00
|
|
|
const lastUsedProviderId = yield select(getLastUsedProvider);
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
|
|
|
|
const lastUsedEditorPage = yield select(getLastUsedEditorPage);
|
|
|
|
|
let lastSelectedPage = yield select(getLastSelectedPage);
|
|
|
|
|
if (lastSelectedPage === "") {
|
|
|
|
|
lastSelectedPage = pageId;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
let id = "";
|
|
|
|
|
if (urlId) {
|
|
|
|
|
id = urlId;
|
|
|
|
|
} else if (lastUsedId) {
|
|
|
|
|
id = lastUsedId;
|
|
|
|
|
}
|
2020-04-28 10:47:59 +00:00
|
|
|
if (lastUsedProviderId && lastUsedEditorPage.includes("provider")) {
|
|
|
|
|
history.push(
|
|
|
|
|
getProviderTemplatesURL(
|
|
|
|
|
applicationId,
|
|
|
|
|
pageId,
|
|
|
|
|
lastUsedProviderId + `/?importTo=${lastSelectedPage}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
yield put(changeApi(id));
|
|
|
|
|
}
|
2019-11-25 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-23 12:12:58 +00:00
|
|
|
function* syncApiParamsSaga(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const field = actionPayload.meta.field;
|
|
|
|
|
const value = actionPayload.payload;
|
2020-04-14 12:34:14 +00:00
|
|
|
|
2019-12-23 12:12:58 +00:00
|
|
|
if (field === "actionConfiguration.path") {
|
|
|
|
|
if (value.indexOf("?") > -1) {
|
|
|
|
|
const paramsString = value.substr(value.indexOf("?") + 1);
|
|
|
|
|
const params = paramsString.split("&").map(p => {
|
|
|
|
|
const keyValue = p.split("=");
|
|
|
|
|
return { key: keyValue[0], value: keyValue[1] || "" };
|
|
|
|
|
});
|
|
|
|
|
yield put(
|
|
|
|
|
autofill(
|
|
|
|
|
API_EDITOR_FORM_NAME,
|
|
|
|
|
"actionConfiguration.queryParameters",
|
|
|
|
|
params,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
yield put(
|
|
|
|
|
autofill(
|
|
|
|
|
API_EDITOR_FORM_NAME,
|
|
|
|
|
"actionConfiguration.queryParameters",
|
|
|
|
|
[],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (field.includes("actionConfiguration.queryParameters")) {
|
|
|
|
|
const { values } = yield select(getFormData, API_EDITOR_FORM_NAME);
|
2019-12-30 13:05:37 +00:00
|
|
|
const path = values.actionConfiguration.path || "";
|
2019-12-23 12:12:58 +00:00
|
|
|
const pathHasParams = path.indexOf("?") > -1;
|
2019-12-30 13:05:37 +00:00
|
|
|
const currentPath = path.substring(
|
2019-12-23 12:12:58 +00:00
|
|
|
0,
|
|
|
|
|
pathHasParams ? path.indexOf("?") : undefined,
|
|
|
|
|
);
|
|
|
|
|
const paramsString = values.actionConfiguration.queryParameters
|
|
|
|
|
.filter((p: Property) => p.key)
|
|
|
|
|
.map(
|
|
|
|
|
(p: Property, i: number) => `${i === 0 ? "?" : "&"}${p.key}=${p.value}`,
|
|
|
|
|
)
|
|
|
|
|
.join("");
|
|
|
|
|
yield put(
|
|
|
|
|
autofill(
|
|
|
|
|
API_EDITOR_FORM_NAME,
|
|
|
|
|
"actionConfiguration.path",
|
|
|
|
|
`${currentPath}${paramsString}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 07:56:37 +00:00
|
|
|
function* initializeExtraFormDataSaga() {
|
|
|
|
|
const state = yield select();
|
|
|
|
|
const { extraformData } = state.ui.apiPane;
|
|
|
|
|
const formData = yield select(getFormData, API_EDITOR_FORM_NAME);
|
|
|
|
|
const { values } = formData;
|
|
|
|
|
const headers = _.get(values, "actionConfiguration.headers");
|
|
|
|
|
|
|
|
|
|
if (!extraformData[values.id]) {
|
|
|
|
|
if (headers) {
|
|
|
|
|
yield put(
|
|
|
|
|
change(API_EDITOR_FORM_NAME, "actionConfiguration.headers", headers),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
function* changeApiSaga(actionPayload: ReduxAction<{ id: string }>) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
2020-02-24 12:58:16 +00:00
|
|
|
// Typescript says Element does not have blur function but it does;
|
|
|
|
|
document.activeElement &&
|
|
|
|
|
"blur" in document.activeElement &&
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
document.activeElement.blur();
|
2020-02-12 09:36:08 +00:00
|
|
|
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
2020-03-24 14:05:19 +00:00
|
|
|
if (!id) {
|
2019-12-11 15:24:27 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2019-11-25 09:15:11 +00:00
|
|
|
const action = yield select(getAction, id);
|
2020-05-07 07:09:07 +00:00
|
|
|
if (!action) return;
|
2019-11-25 09:15:11 +00:00
|
|
|
const draft = yield select(getApiDraft, id);
|
2020-05-07 07:56:37 +00:00
|
|
|
let data;
|
|
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
if (_.isEmpty(draft)) {
|
2020-05-07 07:56:37 +00:00
|
|
|
data = action;
|
2020-04-14 12:34:14 +00:00
|
|
|
} else {
|
|
|
|
|
data = draft;
|
|
|
|
|
}
|
2020-05-07 07:56:37 +00:00
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
if (data.actionConfiguration.path) {
|
|
|
|
|
if (data.actionConfiguration.path.charAt(0) === "/")
|
|
|
|
|
data.actionConfiguration.path = data.actionConfiguration.path.substring(
|
|
|
|
|
1,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
data.actionConfiguration.httpMethod !== "GET" &&
|
2020-05-07 07:56:37 +00:00
|
|
|
!data.providerId &&
|
|
|
|
|
!Array.isArray(data.actionConfiguration.body)
|
2020-04-14 12:34:14 +00:00
|
|
|
) {
|
|
|
|
|
let contentType;
|
|
|
|
|
const headers = data.actionConfiguration.headers;
|
|
|
|
|
if (headers) {
|
|
|
|
|
contentType = headers.find(
|
2020-05-13 18:20:53 +00:00
|
|
|
(header: any) => header.key.toLowerCase() === CONTENT_TYPE,
|
2020-04-14 12:34:14 +00:00
|
|
|
);
|
|
|
|
|
}
|
2020-05-07 07:56:37 +00:00
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
const actionConfigurationBody = data.actionConfiguration.body;
|
2020-05-07 07:56:37 +00:00
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
data.actionConfiguration.body = [];
|
|
|
|
|
if (contentType) {
|
|
|
|
|
if (
|
|
|
|
|
contentType.value === POST_BODY_FORMAT_OPTIONS[0].value &&
|
|
|
|
|
data.actionConfiguration.body
|
|
|
|
|
) {
|
|
|
|
|
data.actionConfiguration.body[0] = actionConfigurationBody;
|
2020-05-07 10:42:17 +00:00
|
|
|
} else if (
|
|
|
|
|
contentType.value === POST_BODY_FORMAT_OPTIONS[1].value &&
|
|
|
|
|
data.actionConfiguration.body
|
|
|
|
|
) {
|
2020-04-14 12:34:14 +00:00
|
|
|
if (typeof actionConfigurationBody !== "object") {
|
2020-05-07 10:42:17 +00:00
|
|
|
try {
|
|
|
|
|
data.actionConfiguration.body[1] = JSON.parse(
|
|
|
|
|
actionConfigurationBody,
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
data.actionConfiguration.body[2] = actionConfigurationBody;
|
|
|
|
|
}
|
2020-04-14 12:34:14 +00:00
|
|
|
} else {
|
|
|
|
|
data.actionConfiguration.body[1] = actionConfigurationBody;
|
|
|
|
|
}
|
2020-05-07 10:42:17 +00:00
|
|
|
} else {
|
|
|
|
|
data.actionConfiguration.body[2] = actionConfigurationBody;
|
2020-04-14 12:34:14 +00:00
|
|
|
}
|
2020-05-07 10:42:17 +00:00
|
|
|
} else if (!contentType && data.actionConfiguration.body) {
|
|
|
|
|
data.actionConfiguration.body[2] = actionConfigurationBody;
|
2020-04-14 12:34:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
yield put(initialize(API_EDITOR_FORM_NAME, data));
|
|
|
|
|
history.push(API_EDITOR_ID_URL(applicationId, pageId, id));
|
2020-05-07 07:56:37 +00:00
|
|
|
|
|
|
|
|
yield call(initializeExtraFormDataSaga);
|
|
|
|
|
|
2019-12-30 13:05:37 +00:00
|
|
|
if (data.actionConfiguration && data.actionConfiguration.queryParameters) {
|
|
|
|
|
// Sync the api params my mocking a change action
|
|
|
|
|
yield call(syncApiParamsSaga, {
|
|
|
|
|
type: ReduxFormActionTypes.ARRAY_REMOVE,
|
|
|
|
|
payload: data.actionConfiguration.queryParameters,
|
|
|
|
|
meta: {
|
|
|
|
|
field: "actionConfiguration.queryParameters",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-11-25 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* updateDraftsSaga() {
|
2020-05-07 07:56:37 +00:00
|
|
|
const { values } = yield select(getFormData, API_EDITOR_FORM_NAME);
|
2019-11-25 09:15:11 +00:00
|
|
|
if (!values.id) return;
|
2020-05-07 07:56:37 +00:00
|
|
|
const action = yield select(getAction, values.id);
|
2020-04-14 12:34:14 +00:00
|
|
|
|
2020-05-07 07:56:37 +00:00
|
|
|
if (_.isEqual(values, action)) {
|
2019-11-25 09:15:11 +00:00
|
|
|
yield put({
|
2020-05-07 07:56:37 +00:00
|
|
|
type: ReduxActionTypes.DELETE_API_DRAFT,
|
|
|
|
|
payload: { id: values.id },
|
2019-11-25 09:15:11 +00:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
yield put({
|
2020-05-07 07:56:37 +00:00
|
|
|
type: ReduxActionTypes.UPDATE_API_DRAFT,
|
|
|
|
|
payload: { id: values.id, draft: values },
|
2019-11-25 09:15:11 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* validateInputSaga(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string; form: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const errors = {};
|
|
|
|
|
const {
|
|
|
|
|
payload,
|
|
|
|
|
meta: { field },
|
|
|
|
|
} = actionPayload;
|
2020-04-20 08:26:19 +00:00
|
|
|
const actions: RestAction[] = yield select(getActionConfigs);
|
2019-11-25 09:15:11 +00:00
|
|
|
const sameNames = actions.filter(
|
|
|
|
|
(action: RestAction) => action.name === payload && action.id,
|
|
|
|
|
);
|
|
|
|
|
if (field === "name") {
|
|
|
|
|
if (!_.trim(payload)) {
|
|
|
|
|
_.set(errors, field, FIELD_REQUIRED_ERROR);
|
|
|
|
|
} else if (payload.indexOf(" ") !== -1) {
|
|
|
|
|
_.set(errors, field, VALID_FUNCTION_NAME_ERROR);
|
|
|
|
|
} else if (sameNames.length > 0) {
|
|
|
|
|
// TODO Check this
|
|
|
|
|
_.set(errors, field, UNIQUE_NAME_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
_.unset(errors, field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (field === "actionConfiguration.path") {
|
|
|
|
|
if (payload && payload.startsWith("/")) {
|
|
|
|
|
_.set(errors, field, API_PATH_START_WITH_SLASH_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
_.unset(errors, field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxFormActionTypes.UPDATE_FIELD_ERROR,
|
|
|
|
|
meta: {
|
|
|
|
|
form: API_EDITOR_FORM_NAME,
|
|
|
|
|
},
|
|
|
|
|
payload: {
|
|
|
|
|
syncErrors: errors,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-14 12:34:14 +00:00
|
|
|
|
|
|
|
|
function* updateFormFields(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const field = actionPayload.meta.field;
|
|
|
|
|
const value = actionPayload.payload;
|
2020-05-07 07:56:37 +00:00
|
|
|
const formData = yield select(getFormData, API_EDITOR_FORM_NAME);
|
2020-04-14 12:34:14 +00:00
|
|
|
|
|
|
|
|
if (field === "actionConfiguration.httpMethod") {
|
|
|
|
|
if (value !== "GET") {
|
|
|
|
|
const { values } = yield select(getFormData, API_EDITOR_FORM_NAME);
|
|
|
|
|
const { actionConfiguration } = values;
|
|
|
|
|
const actionConfigurationHeaders = actionConfiguration.headers;
|
|
|
|
|
let contentType;
|
|
|
|
|
|
|
|
|
|
if (actionConfigurationHeaders) {
|
|
|
|
|
contentType = actionConfigurationHeaders.find(
|
2020-05-13 18:20:53 +00:00
|
|
|
(header: any) => header.key.toLowerCase() === CONTENT_TYPE,
|
2020-04-14 12:34:14 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!contentType) {
|
|
|
|
|
yield put(
|
2020-05-07 10:42:17 +00:00
|
|
|
change(API_EDITOR_FORM_NAME, "actionConfiguration.headers", [
|
2020-04-14 12:34:14 +00:00
|
|
|
...actionConfigurationHeaders,
|
|
|
|
|
{
|
2020-05-23 05:42:23 +00:00
|
|
|
key: CONTENT_TYPE,
|
2020-04-14 12:34:14 +00:00
|
|
|
value: POST_BODY_FORMAT_OPTIONS[0].value,
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-07 07:56:37 +00:00
|
|
|
} else if (field.includes("actionConfiguration.headers")) {
|
|
|
|
|
const formValues = formData.values;
|
|
|
|
|
const actionConfigurationHeaders = _.get(
|
|
|
|
|
formValues,
|
|
|
|
|
"actionConfiguration.headers",
|
|
|
|
|
);
|
|
|
|
|
const apiId = _.get(formValues, "id");
|
|
|
|
|
let displayFormat;
|
|
|
|
|
|
|
|
|
|
if (actionConfigurationHeaders) {
|
|
|
|
|
const contentType = actionConfigurationHeaders.find(
|
2020-05-13 18:20:53 +00:00
|
|
|
(header: any) => header.key.toLowerCase() === CONTENT_TYPE,
|
2020-05-07 07:56:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (contentType && POST_BODY_FORMATS.includes(contentType.value)) {
|
|
|
|
|
displayFormat = {
|
|
|
|
|
label: contentType.value,
|
|
|
|
|
value: contentType.value,
|
|
|
|
|
};
|
2020-05-07 10:42:17 +00:00
|
|
|
} else {
|
|
|
|
|
displayFormat = {
|
|
|
|
|
label: POST_BODY_FORMATS[2],
|
|
|
|
|
value: POST_BODY_FORMATS[2],
|
|
|
|
|
};
|
2020-05-07 07:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SET_EXTRA_FORMDATA,
|
|
|
|
|
payload: {
|
|
|
|
|
id: apiId,
|
|
|
|
|
values: {
|
|
|
|
|
displayFormat,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-04-14 12:34:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
function* formValueChangeSaga(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string; form: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const { form } = actionPayload.meta;
|
|
|
|
|
if (form !== API_EDITOR_FORM_NAME) return;
|
2019-12-23 12:12:58 +00:00
|
|
|
yield all([
|
|
|
|
|
call(validateInputSaga, actionPayload),
|
|
|
|
|
call(updateDraftsSaga),
|
|
|
|
|
call(syncApiParamsSaga, actionPayload),
|
2020-04-14 12:34:14 +00:00
|
|
|
call(updateFormFields, actionPayload),
|
2019-12-23 12:12:58 +00:00
|
|
|
]);
|
2019-11-25 09:15:11 +00:00
|
|
|
}
|
2019-12-23 12:12:58 +00:00
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
function* handleActionCreatedSaga(actionPayload: ReduxAction<RestAction>) {
|
2020-05-05 07:50:30 +00:00
|
|
|
const { id, pluginType } = actionPayload.payload;
|
2019-11-25 09:15:11 +00:00
|
|
|
const action = yield select(getAction, id);
|
2019-12-23 12:12:58 +00:00
|
|
|
const data = { ...action };
|
2020-05-05 07:50:30 +00:00
|
|
|
|
|
|
|
|
if (pluginType === "API") {
|
|
|
|
|
yield put(initialize(API_EDITOR_FORM_NAME, data));
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
|
|
|
|
history.push(API_EDITOR_ID_URL(applicationId, pageId, id));
|
|
|
|
|
}
|
2019-11-25 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleActionUpdatedSaga(
|
|
|
|
|
actionPayload: ReduxAction<{ data: RestAction }>,
|
|
|
|
|
) {
|
|
|
|
|
const { id } = actionPayload.payload.data;
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.DELETE_API_DRAFT,
|
|
|
|
|
payload: { id },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleActionDeletedSaga(actionPayload: ReduxAction<{ id: string }>) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
2020-02-12 09:36:08 +00:00
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
2019-11-25 09:15:11 +00:00
|
|
|
history.push(API_EDITOR_URL(applicationId, pageId));
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.DELETE_API_DRAFT,
|
|
|
|
|
payload: { id },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 13:53:33 +00:00
|
|
|
function* handleMoveOrCopySaga(actionPayload: ReduxAction<{ id: string }>) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
|
|
|
|
const action = yield select(getAction, id);
|
2020-05-05 07:50:30 +00:00
|
|
|
const pluginType = action?.pluginType ?? "";
|
|
|
|
|
|
|
|
|
|
if (pluginType === "API") {
|
|
|
|
|
const { values }: { values: RestAction } = yield select(
|
|
|
|
|
getFormData,
|
|
|
|
|
API_EDITOR_FORM_NAME,
|
|
|
|
|
);
|
|
|
|
|
if (values.id === id) {
|
|
|
|
|
yield put(initialize(API_EDITOR_FORM_NAME, action));
|
|
|
|
|
} else {
|
|
|
|
|
yield put(changeApi(id));
|
|
|
|
|
}
|
2020-01-27 13:53:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 08:26:19 +00:00
|
|
|
function* handleCreateNewApiActionSaga(
|
|
|
|
|
action: ReduxAction<{ pageId: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const pluginId = yield select(
|
|
|
|
|
getPluginIdOfPackageName,
|
|
|
|
|
REST_PLUGIN_PACKAGE_NAME,
|
|
|
|
|
);
|
|
|
|
|
const { pageId } = action.payload;
|
|
|
|
|
if (pageId && pluginId) {
|
|
|
|
|
const actions = yield select(getActions);
|
|
|
|
|
const pageActions = actions.filter(
|
|
|
|
|
(a: ActionData) => a.config.pageId === pageId,
|
|
|
|
|
);
|
|
|
|
|
const newActionName = createNewApiName(pageActions, pageId);
|
|
|
|
|
yield put(
|
|
|
|
|
createActionRequest({
|
|
|
|
|
...DEFAULT_API_ACTION,
|
|
|
|
|
name: newActionName,
|
|
|
|
|
datasource: {
|
|
|
|
|
name: "DEFAULT_REST_DATASOURCE",
|
|
|
|
|
pluginId,
|
|
|
|
|
},
|
|
|
|
|
pageId,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 09:15:11 +00:00
|
|
|
export default function* root() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(ReduxActionTypes.INIT_API_PANE, initApiPaneSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.API_PANE_CHANGE_API, changeApiSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.CREATE_ACTION_SUCCESS, handleActionCreatedSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.UPDATE_ACTION_SUCCESS, handleActionUpdatedSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.DELETE_ACTION_SUCCESS, handleActionDeletedSaga),
|
2020-01-27 13:53:33 +00:00
|
|
|
takeEvery(ReduxActionTypes.MOVE_ACTION_SUCCESS, handleMoveOrCopySaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.COPY_ACTION_SUCCESS, handleMoveOrCopySaga),
|
2020-04-20 08:26:19 +00:00
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.CREATE_NEW_API_ACTION,
|
|
|
|
|
handleCreateNewApiActionSaga,
|
|
|
|
|
),
|
2019-12-23 12:12:58 +00:00
|
|
|
// Intercepting the redux-form change actionType
|
|
|
|
|
takeEvery(ReduxFormActionTypes.VALUE_CHANGE, formValueChangeSaga),
|
|
|
|
|
takeEvery(ReduxFormActionTypes.ARRAY_REMOVE, formValueChangeSaga),
|
2020-04-14 12:34:14 +00:00
|
|
|
takeEvery(ReduxFormActionTypes.ARRAY_PUSH, formValueChangeSaga),
|
2019-11-25 09:15:11 +00:00
|
|
|
]);
|
|
|
|
|
}
|