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

65 lines
1.7 KiB
TypeScript
Raw Normal View History

import { all, takeEvery, call, put, select } from "redux-saga/effects";
2020-04-29 11:58:02 +00:00
import {
ReduxActionTypes,
ReduxAction,
ReduxActionErrorTypes,
} from "constants/ReduxActionConstants";
import PluginsApi from "api/PluginApi";
import { validateResponse } from "sagas/ErrorSagas";
import { getCurrentOrgId } from "selectors/organizationSelectors";
function* fetchPluginsSaga() {
try {
const orgId = yield select(getCurrentOrgId);
if (!orgId) {
throw Error("Org id does not exist");
}
const pluginsResponse = yield call(PluginsApi.fetchPlugins, orgId);
const isValid = yield validateResponse(pluginsResponse);
if (isValid) {
yield put({
type: ReduxActionTypes.FETCH_PLUGINS_SUCCESS,
payload: pluginsResponse.data,
});
}
} catch (error) {
yield put({
2020-06-04 13:49:22 +00:00
type: ReduxActionErrorTypes.FETCH_PLUGINS_ERROR,
payload: { error },
});
}
}
2020-04-29 09:23:23 +00:00
function* fetchPluginFormSaga(actionPayload: ReduxAction<{ id: string }>) {
try {
const response = yield call(
PluginsApi.fetchFormConfig,
actionPayload.payload.id,
);
const isValid = yield validateResponse(response);
if (isValid) {
yield put({
type: ReduxActionTypes.FETCH_PLUGIN_FORM_SUCCESS,
payload: {
id: actionPayload.payload.id,
...response.data,
2020-04-29 09:23:23 +00:00
},
});
}
} catch (error) {
yield put({
2020-04-29 11:58:02 +00:00
type: ReduxActionErrorTypes.FETCH_PLUGIN_FORM_ERROR,
2020-04-29 09:23:23 +00:00
payload: { error },
});
}
}
function* root() {
yield all([
takeEvery(ReduxActionTypes.FETCH_PLUGINS_REQUEST, fetchPluginsSaga),
2020-04-29 09:23:23 +00:00
takeEvery(ReduxActionTypes.FETCH_PLUGIN_FORM_INIT, fetchPluginFormSaga),
]);
}
export default root;