2020-06-03 05:40:48 +00:00
|
|
|
import { takeLatest, put, all, select, take } from "redux-saga/effects";
|
2020-04-14 12:34:14 +00:00
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
ReduxAction,
|
|
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
import { validateResponse } from "sagas/ErrorSagas";
|
|
|
|
|
import CurlImportApi, { CurlImportRequest } from "api/ImportApi";
|
|
|
|
|
import { ApiResponse } from "api/ApiResponses";
|
2020-05-05 07:50:30 +00:00
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
2020-04-14 12:34:14 +00:00
|
|
|
import { AppToaster } from "components/editorComponents/ToastComponent";
|
|
|
|
|
import { ToastType } from "react-toastify";
|
|
|
|
|
import { CURL_IMPORT_SUCCESS } from "constants/messages";
|
2020-06-03 05:40:48 +00:00
|
|
|
import { getCurrentApplicationId } from "selectors/editorSelectors";
|
2020-04-14 12:34:14 +00:00
|
|
|
import { fetchActions } from "actions/actionActions";
|
2020-05-05 07:50:30 +00:00
|
|
|
import { CURL } from "constants/ApiConstants";
|
2020-06-03 05:40:48 +00:00
|
|
|
import { changeApi } from "actions/apiPaneActions";
|
2020-06-17 10:19:56 +00:00
|
|
|
import { getCurrentOrgId } from "selectors/organizationSelectors";
|
2020-06-25 10:04:57 +00:00
|
|
|
import transformCurlImport from "transformers/CurlImportTransformer";
|
2020-04-14 12:34:14 +00:00
|
|
|
|
|
|
|
|
export function* curlImportSaga(action: ReduxAction<CurlImportRequest>) {
|
|
|
|
|
const { type, pageId, name } = action.payload;
|
|
|
|
|
let { curl } = action.payload;
|
|
|
|
|
try {
|
2020-06-25 10:04:57 +00:00
|
|
|
curl = transformCurlImport(curl);
|
2020-06-17 10:19:56 +00:00
|
|
|
const organizationId = yield select(getCurrentOrgId);
|
|
|
|
|
const request: CurlImportRequest = {
|
|
|
|
|
type,
|
|
|
|
|
pageId,
|
|
|
|
|
name,
|
|
|
|
|
curl,
|
|
|
|
|
organizationId,
|
|
|
|
|
};
|
2020-04-14 12:34:14 +00:00
|
|
|
|
|
|
|
|
const response: ApiResponse = yield CurlImportApi.curlImport(request);
|
|
|
|
|
const isValidResponse = yield validateResponse(response);
|
|
|
|
|
const applicationId = yield select(getCurrentApplicationId);
|
|
|
|
|
|
|
|
|
|
if (isValidResponse) {
|
2020-05-05 07:50:30 +00:00
|
|
|
AnalyticsUtil.logEvent("IMPORT_API", {
|
|
|
|
|
importSource: CURL,
|
|
|
|
|
});
|
2020-06-03 05:40:48 +00:00
|
|
|
|
|
|
|
|
yield put(fetchActions(applicationId));
|
|
|
|
|
const data = { ...response.data };
|
|
|
|
|
yield take(ReduxActionTypes.FETCH_ACTIONS_SUCCESS);
|
|
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
AppToaster.show({
|
|
|
|
|
message: CURL_IMPORT_SUCCESS,
|
|
|
|
|
type: ToastType.SUCCESS,
|
|
|
|
|
});
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SUBMIT_CURL_FORM_SUCCESS,
|
|
|
|
|
payload: response.data,
|
|
|
|
|
});
|
2020-06-03 05:40:48 +00:00
|
|
|
|
2020-07-06 05:38:39 +00:00
|
|
|
yield put(changeApi(data.id, true));
|
2020-04-14 12:34:14 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.SUBMIT_CURL_FORM_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* curlImportSagas() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeLatest(ReduxActionTypes.SUBMIT_CURL_FORM_INIT, curlImportSaga),
|
|
|
|
|
]);
|
|
|
|
|
}
|