2020-06-17 10:19:56 +00:00
|
|
|
import { all, takeEvery, call, put, select } from "redux-saga/effects";
|
2020-04-29 11:58:02 +00:00
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
2022-03-17 10:28:54 +00:00
|
|
|
ReduxAction,
|
2020-04-29 11:58:02 +00:00
|
|
|
} from "constants/ReduxActionConstants";
|
2021-03-30 05:29:03 +00:00
|
|
|
import PluginsApi, { PluginFormPayload } from "api/PluginApi";
|
2019-11-29 05:22:49 +00:00
|
|
|
import { validateResponse } from "sagas/ErrorSagas";
|
2020-06-17 10:19:56 +00:00
|
|
|
import { getCurrentOrgId } from "selectors/organizationSelectors";
|
2021-03-30 05:29:03 +00:00
|
|
|
import {
|
|
|
|
|
getDatasources,
|
|
|
|
|
getPlugin,
|
|
|
|
|
getPluginForm,
|
|
|
|
|
getPlugins,
|
|
|
|
|
} from "selectors/entitiesSelector";
|
|
|
|
|
import { Datasource } from "entities/Datasource";
|
|
|
|
|
import { Plugin } from "api/PluginApi";
|
|
|
|
|
import {
|
|
|
|
|
fetchPluginFormConfigsSuccess,
|
|
|
|
|
fetchPluginFormConfigSuccess,
|
2021-07-29 08:13:10 +00:00
|
|
|
fetchPluginFormConfigError,
|
2021-03-30 05:29:03 +00:00
|
|
|
} from "actions/pluginActions";
|
|
|
|
|
import {
|
2021-04-26 05:41:32 +00:00
|
|
|
defaultActionDependenciesConfig,
|
2021-03-30 05:29:03 +00:00
|
|
|
defaultActionEditorConfigs,
|
|
|
|
|
defaultActionSettings,
|
|
|
|
|
} from "constants/AppsmithActionConstants/ActionConstants";
|
|
|
|
|
import { GenericApiResponse } from "api/ApiResponses";
|
|
|
|
|
import PluginApi from "api/PluginApi";
|
|
|
|
|
import log from "loglevel";
|
2021-04-02 09:47:37 +00:00
|
|
|
import { PluginType } from "entities/Action";
|
2021-08-26 05:37:07 +00:00
|
|
|
import {
|
|
|
|
|
FormEditorConfigs,
|
|
|
|
|
FormSettingsConfigs,
|
|
|
|
|
FormDependencyConfigs,
|
|
|
|
|
} from "utils/DynamicBindingUtils";
|
2019-11-29 05:22:49 +00:00
|
|
|
|
2022-03-17 10:28:54 +00:00
|
|
|
function* fetchPluginsSaga(
|
|
|
|
|
action: ReduxAction<{ orgId?: string } | undefined>,
|
|
|
|
|
) {
|
2019-11-29 05:22:49 +00:00
|
|
|
try {
|
2022-03-17 10:28:54 +00:00
|
|
|
let orgId = yield select(getCurrentOrgId);
|
|
|
|
|
if (action.payload?.orgId) orgId = action.payload?.orgId;
|
2022-03-03 10:56:53 +00:00
|
|
|
|
2020-08-26 13:22:10 +00:00
|
|
|
if (!orgId) {
|
|
|
|
|
throw Error("Org id does not exist");
|
|
|
|
|
}
|
2020-06-17 10:19:56 +00:00
|
|
|
const pluginsResponse = yield call(PluginsApi.fetchPlugins, orgId);
|
2019-11-29 05:22:49 +00:00
|
|
|
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,
|
2019-11-29 05:22:49 +00:00
|
|
|
payload: { error },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 05:29:03 +00:00
|
|
|
function* fetchPluginFormConfigsSaga() {
|
2020-04-29 09:23:23 +00:00
|
|
|
try {
|
2021-03-30 05:29:03 +00:00
|
|
|
const datasources: Datasource[] = yield select(getDatasources);
|
|
|
|
|
const plugins: Plugin[] = yield select(getPlugins);
|
2021-04-02 09:47:37 +00:00
|
|
|
const pluginFormRequests = [];
|
|
|
|
|
// Add plugins of all the datasources of their org
|
|
|
|
|
const pluginIdFormsToFetch = new Set(
|
2021-03-30 05:29:03 +00:00
|
|
|
datasources.map((datasource) => datasource.pluginId),
|
2020-04-29 09:23:23 +00:00
|
|
|
);
|
2021-04-02 09:47:37 +00:00
|
|
|
// Add the api plugin id by default because it is the only type of action that
|
|
|
|
|
// can exist without a saved datasource
|
|
|
|
|
const apiPlugin = plugins.find((plugin) => plugin.type === PluginType.API);
|
2021-09-08 17:32:22 +00:00
|
|
|
const jsPlugin = plugins.find((plugin) => plugin.type === PluginType.JS);
|
2021-04-02 09:47:37 +00:00
|
|
|
if (apiPlugin) {
|
|
|
|
|
pluginIdFormsToFetch.add(apiPlugin.id);
|
|
|
|
|
}
|
|
|
|
|
for (const id of pluginIdFormsToFetch) {
|
2021-03-30 05:29:03 +00:00
|
|
|
pluginFormRequests.push(yield call(PluginsApi.fetchFormConfig, id));
|
2020-04-29 09:23:23 +00:00
|
|
|
}
|
2021-03-30 05:29:03 +00:00
|
|
|
const pluginFormData: PluginFormPayload[] = [];
|
|
|
|
|
const pluginFormResponses = yield all(pluginFormRequests);
|
|
|
|
|
for (const response of pluginFormResponses) {
|
|
|
|
|
yield validateResponse(response);
|
|
|
|
|
pluginFormData.push(response.data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-08 17:32:22 +00:00
|
|
|
if (jsPlugin) {
|
|
|
|
|
pluginIdFormsToFetch.add(jsPlugin.id);
|
|
|
|
|
}
|
2021-03-30 05:29:03 +00:00
|
|
|
const formConfigs: Record<string, any[]> = {};
|
2021-08-26 05:37:07 +00:00
|
|
|
const editorConfigs: FormEditorConfigs = {};
|
|
|
|
|
const settingConfigs: FormSettingsConfigs = {};
|
|
|
|
|
const dependencies: FormDependencyConfigs = {};
|
2021-03-30 05:29:03 +00:00
|
|
|
|
2021-04-02 09:47:37 +00:00
|
|
|
Array.from(pluginIdFormsToFetch).forEach((pluginId, index) => {
|
2021-03-30 05:29:03 +00:00
|
|
|
const plugin = plugins.find((plugin) => plugin.id === pluginId);
|
2021-09-08 17:32:22 +00:00
|
|
|
if (plugin && plugin.type === PluginType.JS) {
|
2021-03-30 05:29:03 +00:00
|
|
|
settingConfigs[pluginId] = defaultActionSettings[plugin.type];
|
2021-09-08 17:32:22 +00:00
|
|
|
editorConfigs[pluginId] = defaultActionEditorConfigs[plugin.type];
|
|
|
|
|
formConfigs[pluginId] = [];
|
2021-04-26 05:41:32 +00:00
|
|
|
dependencies[pluginId] = defaultActionDependenciesConfig[plugin.type];
|
|
|
|
|
} else {
|
2021-09-08 17:32:22 +00:00
|
|
|
// Datasource form always use server's copy
|
|
|
|
|
formConfigs[pluginId] = pluginFormData[index].form;
|
|
|
|
|
// Action editor form if not available use default
|
|
|
|
|
if (plugin && !pluginFormData[index].editor) {
|
|
|
|
|
editorConfigs[pluginId] = defaultActionEditorConfigs[plugin.type];
|
|
|
|
|
} else {
|
|
|
|
|
editorConfigs[pluginId] = pluginFormData[index].editor;
|
|
|
|
|
}
|
|
|
|
|
// Action settings form if not available use default
|
|
|
|
|
if (plugin && !pluginFormData[index].setting) {
|
|
|
|
|
settingConfigs[pluginId] = defaultActionSettings[plugin.type];
|
|
|
|
|
} else {
|
|
|
|
|
settingConfigs[pluginId] = pluginFormData[index].setting;
|
|
|
|
|
}
|
|
|
|
|
// Action dependencies config if not available use default
|
|
|
|
|
if (plugin && !pluginFormData[index].dependencies) {
|
|
|
|
|
dependencies[pluginId] = defaultActionDependenciesConfig[plugin.type];
|
|
|
|
|
} else {
|
|
|
|
|
dependencies[pluginId] = pluginFormData[index].dependencies;
|
|
|
|
|
}
|
2021-04-26 05:41:32 +00:00
|
|
|
}
|
2021-03-30 05:29:03 +00:00
|
|
|
});
|
|
|
|
|
yield put(
|
|
|
|
|
fetchPluginFormConfigsSuccess({
|
|
|
|
|
formConfigs,
|
|
|
|
|
editorConfigs,
|
|
|
|
|
settingConfigs,
|
2021-04-26 05:41:32 +00:00
|
|
|
dependencies,
|
2021-03-30 05:29:03 +00:00
|
|
|
}),
|
|
|
|
|
);
|
2020-04-29 09:23:23 +00:00
|
|
|
} catch (error) {
|
2021-04-02 09:47:37 +00:00
|
|
|
log.error(error);
|
2020-04-29 09:23:23 +00:00
|
|
|
yield put({
|
2021-03-30 05:29:03 +00:00
|
|
|
type: ReduxActionErrorTypes.FETCH_PLUGIN_FORM_CONFIGS_ERROR,
|
2020-04-29 09:23:23 +00:00
|
|
|
payload: { error },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 05:29:03 +00:00
|
|
|
export function* checkAndGetPluginFormConfigsSaga(pluginId: string) {
|
2020-10-20 09:00:02 +00:00
|
|
|
try {
|
2021-03-30 05:29:03 +00:00
|
|
|
const plugin: Plugin = yield select(getPlugin, pluginId);
|
|
|
|
|
const formConfig = yield select(getPluginForm, pluginId);
|
|
|
|
|
if (!formConfig) {
|
|
|
|
|
const formConfigResponse: GenericApiResponse<PluginFormPayload> = yield PluginApi.fetchFormConfig(
|
|
|
|
|
pluginId,
|
|
|
|
|
);
|
|
|
|
|
yield validateResponse(formConfigResponse);
|
|
|
|
|
if (!formConfigResponse.data.setting) {
|
|
|
|
|
formConfigResponse.data.setting = defaultActionSettings[plugin.type];
|
|
|
|
|
}
|
|
|
|
|
if (!formConfigResponse.data.editor) {
|
|
|
|
|
formConfigResponse.data.editor =
|
|
|
|
|
defaultActionEditorConfigs[plugin.type];
|
2020-10-20 09:00:02 +00:00
|
|
|
}
|
2021-04-26 05:41:32 +00:00
|
|
|
if (!formConfigResponse.data.dependencies) {
|
|
|
|
|
formConfigResponse.data.dependencies =
|
|
|
|
|
defaultActionDependenciesConfig[plugin.type];
|
|
|
|
|
}
|
2021-03-30 05:29:03 +00:00
|
|
|
yield put(
|
|
|
|
|
fetchPluginFormConfigSuccess({
|
|
|
|
|
id: pluginId,
|
|
|
|
|
...formConfigResponse.data,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2020-10-20 09:00:02 +00:00
|
|
|
}
|
2021-03-30 05:29:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
log.error("Failed to get plugin form");
|
2021-07-29 08:13:10 +00:00
|
|
|
yield put(
|
|
|
|
|
fetchPluginFormConfigError({
|
|
|
|
|
id: pluginId,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2020-10-20 09:00:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-29 08:13:10 +00:00
|
|
|
type GetPluginFormConfigParams = { id: string; type: string };
|
|
|
|
|
|
|
|
|
|
function* getPluginFormConfig({ id }: GetPluginFormConfigParams) {
|
|
|
|
|
yield call(checkAndGetPluginFormConfigsSaga, id);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 10:56:53 +00:00
|
|
|
function* getDefaultPluginsSaga() {
|
|
|
|
|
try {
|
|
|
|
|
const response = yield call(PluginsApi.fetchDefaultPlugins);
|
|
|
|
|
const isValid = yield validateResponse(response);
|
|
|
|
|
if (isValid) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.GET_DEFAULT_PLUGINS_SUCCESS,
|
|
|
|
|
payload: response.data,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.GET_DEFAULT_PLUGINS_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 05:22:49 +00:00
|
|
|
function* root() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(ReduxActionTypes.FETCH_PLUGINS_REQUEST, fetchPluginsSaga),
|
2020-10-20 09:00:02 +00:00
|
|
|
takeEvery(
|
2021-03-30 05:29:03 +00:00
|
|
|
ReduxActionTypes.FETCH_PLUGIN_FORM_CONFIGS_REQUEST,
|
|
|
|
|
fetchPluginFormConfigsSaga,
|
2020-10-20 09:00:02 +00:00
|
|
|
),
|
2021-07-29 08:13:10 +00:00
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.GET_PLUGIN_FORM_CONFIG_INIT,
|
|
|
|
|
getPluginFormConfig,
|
|
|
|
|
),
|
2022-03-03 10:56:53 +00:00
|
|
|
takeEvery(
|
|
|
|
|
ReduxActionTypes.GET_DEFAULT_PLUGINS_REQUEST,
|
|
|
|
|
getDefaultPluginsSaga,
|
|
|
|
|
),
|
2019-11-29 05:22:49 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default root;
|