2020-05-05 07:50:30 +00:00
|
|
|
import _, { take } from "lodash";
|
|
|
|
|
import {
|
|
|
|
|
all,
|
|
|
|
|
select,
|
|
|
|
|
put,
|
|
|
|
|
takeEvery,
|
|
|
|
|
call,
|
|
|
|
|
takeLatest,
|
|
|
|
|
} from "redux-saga/effects";
|
|
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionWithMeta,
|
|
|
|
|
ReduxFormActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
import { getFormData } from "selectors/formSelectors";
|
|
|
|
|
import { QUERY_EDITOR_FORM_NAME } from "constants/forms";
|
|
|
|
|
import history from "utils/history";
|
|
|
|
|
import {
|
|
|
|
|
QUERIES_EDITOR_URL,
|
|
|
|
|
QUERIES_EDITOR_ID_URL,
|
|
|
|
|
APPLICATIONS_URL,
|
|
|
|
|
} from "constants/routes";
|
|
|
|
|
import {
|
|
|
|
|
getCurrentApplicationId,
|
|
|
|
|
getCurrentPageId,
|
|
|
|
|
} from "selectors/editorSelectors";
|
2020-06-10 07:49:27 +00:00
|
|
|
import { change, initialize } from "redux-form";
|
2020-05-05 07:50:30 +00:00
|
|
|
import { AppState } from "reducers";
|
2020-07-03 08:58:58 +00:00
|
|
|
import ActionAPI, { Property } from "api/ActionAPI";
|
2020-05-05 07:50:30 +00:00
|
|
|
import { QUERY_CONSTANT } from "constants/QueryEditorConstants";
|
|
|
|
|
import { changeQuery, deleteQuerySuccess } from "actions/queryPaneActions";
|
|
|
|
|
import { AppToaster } from "components/editorComponents/ToastComponent";
|
|
|
|
|
import { ToastType } from "react-toastify";
|
2020-06-25 10:04:57 +00:00
|
|
|
import { isDynamicValue } from "utils/DynamicBindingUtils";
|
2020-05-05 07:50:30 +00:00
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
|
|
|
import { GenericApiResponse } from "api/ApiResponses";
|
|
|
|
|
import { validateResponse } from "./ErrorSagas";
|
2020-07-03 08:58:58 +00:00
|
|
|
import { getAction, getQueryName } from "selectors/entitiesSelector";
|
|
|
|
|
import { RestAction } from "entities/Action";
|
2020-06-29 08:23:10 +00:00
|
|
|
import { updateAction } from "actions/actionActions";
|
2020-05-05 07:50:30 +00:00
|
|
|
|
|
|
|
|
const getActions = (state: AppState) =>
|
|
|
|
|
state.entities.actions.map(a => a.config);
|
|
|
|
|
|
|
|
|
|
const getLastUsedAction = (state: AppState) => state.ui.queryPane.lastUsed;
|
|
|
|
|
|
2020-06-20 05:29:30 +00:00
|
|
|
const getQueryCreationStatus = (state: AppState) =>
|
|
|
|
|
state.ui.queryPane.isCreating;
|
|
|
|
|
|
2020-05-05 07:50:30 +00:00
|
|
|
function* initQueryPaneSaga(
|
|
|
|
|
actionPayload: ReduxAction<{
|
|
|
|
|
pluginType: string;
|
|
|
|
|
id?: string;
|
|
|
|
|
}>,
|
|
|
|
|
) {
|
|
|
|
|
let actions = yield select(getActions);
|
|
|
|
|
while (!actions.length) {
|
|
|
|
|
yield take(ReduxActionTypes.FETCH_ACTIONS_SUCCESS);
|
|
|
|
|
actions = yield select(getActions);
|
|
|
|
|
}
|
|
|
|
|
const urlId = actionPayload.payload.id;
|
|
|
|
|
const lastUsedId = yield select(getLastUsedAction);
|
2020-06-20 05:29:30 +00:00
|
|
|
const isCreating = yield select(getQueryCreationStatus);
|
2020-05-05 07:50:30 +00:00
|
|
|
|
|
|
|
|
let id = "";
|
|
|
|
|
if (urlId) {
|
|
|
|
|
id = urlId;
|
|
|
|
|
} else if (lastUsedId) {
|
|
|
|
|
id = lastUsedId;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 05:29:30 +00:00
|
|
|
if (isCreating) return;
|
|
|
|
|
|
2020-05-05 07:50:30 +00:00
|
|
|
yield put(changeQuery(id, QUERY_CONSTANT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* changeQuerySaga(
|
|
|
|
|
actionPayload: ReduxAction<{ id: string; pluginType: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
|
|
|
|
if (!applicationId || !pageId) {
|
|
|
|
|
history.push(APPLICATIONS_URL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const action = yield select(getAction, id);
|
|
|
|
|
if (!action) {
|
|
|
|
|
history.push(QUERIES_EDITOR_URL(applicationId, pageId));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const URL = QUERIES_EDITOR_ID_URL(applicationId, pageId, id);
|
2020-07-03 08:58:58 +00:00
|
|
|
yield put(initialize(QUERY_EDITOR_FORM_NAME, action));
|
2020-05-05 07:50:30 +00:00
|
|
|
history.push(URL);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 08:23:10 +00:00
|
|
|
function* saveQueryAction() {
|
2020-05-05 07:50:30 +00:00
|
|
|
const { values } = yield select(getFormData, QUERY_EDITOR_FORM_NAME);
|
|
|
|
|
if (!values.id) return;
|
2020-07-03 08:58:58 +00:00
|
|
|
yield put(
|
|
|
|
|
updateAction({
|
|
|
|
|
data: values,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2020-05-05 07:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-10 07:49:27 +00:00
|
|
|
function* updateDynamicBindingsSaga(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string }>,
|
|
|
|
|
) {
|
2020-06-30 09:49:30 +00:00
|
|
|
const field = actionPayload.meta.field.replace("actionConfiguration.", "");
|
2020-06-10 07:49:27 +00:00
|
|
|
if (field === "dynamicBindingPathList") return;
|
|
|
|
|
const value = actionPayload.payload;
|
|
|
|
|
const { values } = yield select(getFormData, QUERY_EDITOR_FORM_NAME);
|
|
|
|
|
if (!values.id) return;
|
|
|
|
|
|
|
|
|
|
const isDynamic = isDynamicValue(value);
|
|
|
|
|
let dynamicBindings: Property[] = values.dynamicBindingPathList || [];
|
|
|
|
|
const fieldExists = _.some(dynamicBindings, { key: field });
|
|
|
|
|
|
|
|
|
|
if (!isDynamic && fieldExists) {
|
|
|
|
|
dynamicBindings = dynamicBindings.filter(d => d.key !== field);
|
|
|
|
|
}
|
|
|
|
|
if (isDynamic && !fieldExists) {
|
|
|
|
|
dynamicBindings.push({ key: field });
|
|
|
|
|
}
|
|
|
|
|
yield put(
|
|
|
|
|
change(QUERY_EDITOR_FORM_NAME, "dynamicBindingPathList", dynamicBindings),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 07:50:30 +00:00
|
|
|
function* formValueChangeSaga(
|
|
|
|
|
actionPayload: ReduxActionWithMeta<string, { field: string; form: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const { form } = actionPayload.meta;
|
|
|
|
|
if (form !== QUERY_EDITOR_FORM_NAME) return;
|
2020-06-10 07:49:27 +00:00
|
|
|
yield all([
|
|
|
|
|
call(updateDynamicBindingsSaga, actionPayload),
|
2020-06-29 08:23:10 +00:00
|
|
|
call(saveQueryAction),
|
2020-06-10 07:49:27 +00:00
|
|
|
]);
|
2020-05-05 07:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleQueryCreatedSaga(actionPayload: ReduxAction<RestAction>) {
|
|
|
|
|
const { id, pluginType } = actionPayload.payload;
|
|
|
|
|
const action = yield select(getAction, id);
|
|
|
|
|
const data = { ...action };
|
|
|
|
|
if (pluginType === "DB") {
|
|
|
|
|
yield put(initialize(QUERY_EDITOR_FORM_NAME, data));
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
2020-07-06 05:38:39 +00:00
|
|
|
history.replace(
|
|
|
|
|
QUERIES_EDITOR_ID_URL(applicationId, pageId, id, {
|
|
|
|
|
new: true,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2020-05-05 07:50:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleQueryDeletedSaga(actionPayload: ReduxAction<{ id: string }>) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
const pageId = yield select(getCurrentPageId);
|
|
|
|
|
history.push(QUERIES_EDITOR_URL(applicationId, pageId));
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.DELETE_API_DRAFT,
|
|
|
|
|
payload: { id },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* handleMoveOrCopySaga(actionPayload: ReduxAction<{ id: string }>) {
|
|
|
|
|
const { id } = actionPayload.payload;
|
|
|
|
|
const action = yield select(getAction, id);
|
|
|
|
|
const pluginType = action?.pluginType ?? "";
|
|
|
|
|
|
|
|
|
|
if (pluginType === "DB") {
|
|
|
|
|
const { values }: { values: RestAction } = yield select(
|
|
|
|
|
getFormData,
|
|
|
|
|
QUERY_EDITOR_FORM_NAME,
|
|
|
|
|
);
|
|
|
|
|
if (values.id === id) {
|
|
|
|
|
yield put(initialize(QUERY_EDITOR_FORM_NAME, action));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 08:58:58 +00:00
|
|
|
function* deleteQuerySaga(actionPayload: ReduxAction<{ id: string }>) {
|
2020-05-05 07:50:30 +00:00
|
|
|
try {
|
|
|
|
|
const id = actionPayload.payload.id;
|
|
|
|
|
const response: GenericApiResponse<RestAction> = yield ActionAPI.deleteAction(
|
|
|
|
|
id,
|
|
|
|
|
);
|
|
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
if (isValidResponse) {
|
|
|
|
|
const queryName = yield select(getQueryName, id);
|
|
|
|
|
AnalyticsUtil.logEvent("DELETE_QUERY", {
|
|
|
|
|
queryName,
|
|
|
|
|
});
|
|
|
|
|
AppToaster.show({
|
|
|
|
|
message: `${response.data.name} Action deleted`,
|
|
|
|
|
type: ToastType.SUCCESS,
|
|
|
|
|
});
|
|
|
|
|
yield put(deleteQuerySuccess({ id }));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.DELETE_QUERY_ERROR,
|
|
|
|
|
payload: { error, id: actionPayload.payload.id },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* root() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(ReduxActionTypes.CREATE_ACTION_SUCCESS, handleQueryCreatedSaga),
|
|
|
|
|
takeLatest(ReduxActionTypes.DELETE_QUERY_INIT, deleteQuerySaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.DELETE_QUERY_SUCCESS, handleQueryDeletedSaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.MOVE_ACTION_SUCCESS, handleMoveOrCopySaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.COPY_ACTION_SUCCESS, handleMoveOrCopySaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.QUERY_PANE_CHANGE, changeQuerySaga),
|
|
|
|
|
takeEvery(ReduxActionTypes.INIT_QUERY_PANE, initQueryPaneSaga),
|
|
|
|
|
// Intercepting the redux-form change actionType
|
|
|
|
|
takeEvery(ReduxFormActionTypes.VALUE_CHANGE, formValueChangeSaga),
|
|
|
|
|
takeEvery(ReduxFormActionTypes.ARRAY_REMOVE, formValueChangeSaga),
|
|
|
|
|
]);
|
|
|
|
|
}
|