Create a seperate action for datasource generated from datasource pane

This commit is contained in:
Akash N 2020-05-07 10:14:52 +05:30
parent 0c053c49a6
commit 1b15a8e7f6
5 changed files with 115 additions and 69 deletions

View File

@ -8,6 +8,13 @@ export const createDatasource = (payload: CreateDatasourceConfig) => {
};
};
export const createDatasourceFromForm = (payload: CreateDatasourceConfig) => {
return {
type: ReduxActionTypes.CREATE_DATASOURCE_FROM_FORM_INIT,
payload,
};
};
export const updateDatasource = (payload: Datasource) => {
return {
type: ReduxActionTypes.UPDATE_DATASOURCE_INIT,

View File

@ -64,6 +64,7 @@ export const ReduxActionTypes: { [key: string]: string } = {
FETCH_DATASOURCES_SUCCESS: "FETCH_DATASOURCES_SUCCESS",
CREATE_DATASOURCE_INIT: "CREATE_DATASOURCE_INIT",
CREATE_DATASOURCE_SUCCESS: "CREATE_DATASOURCE_SUCCESS",
CREATE_DATASOURCE_FROM_FORM_INIT: "CREATE_DATASOURCE_FROM_FORM_INIT",
UPDATE_DATASOURCE_INIT: "UPDATE_DATASOURCE_INIT",
UPDATE_DATASOURCE_SUCCESS: "UPDATE_DATASOURCE_SUCCESS",
SELECT_PLUGIN: "SELECT_PLUGIN",

View File

@ -19,7 +19,10 @@ import {
PLUGIN_PACKAGE_POSTGRES,
PLUGIN_PACKAGE_MONGO,
} from "constants/QueryEditorConstants";
import { selectPlugin, createDatasource } from "actions/datasourceActions";
import {
selectPlugin,
createDatasourceFromForm,
} from "actions/datasourceActions";
import { AppState } from "reducers";
import AnalyticsUtil from "utils/AnalyticsUtil";
import { getCurrentApplication } from "selectors/applicationSelectors";
@ -229,7 +232,7 @@ const mapDispatchToProps = (dispatch: any) => {
selectPlugin: (pluginId: string) => dispatch(selectPlugin(pluginId)),
initializeForm: (data: Record<string, any>) =>
dispatch(initialize(DATASOURCE_DB_FORM, data)),
createDatasource: (data: any) => dispatch(createDatasource(data)),
createDatasource: (data: any) => dispatch(createDatasourceFromForm(data)),
};
};

View File

@ -27,6 +27,11 @@ const datasourceReducer = createReducer(initialState, {
[ReduxActionTypes.CREATE_DATASOURCE_INIT]: (state: DatasourceDataState) => {
return { ...state, loading: true };
},
[ReduxActionTypes.CREATE_DATASOURCE_FROM_FORM_INIT]: (
state: DatasourceDataState,
) => {
return { ...state, loading: true };
},
[ReduxActionTypes.UPDATE_DATASOURCE_INIT]: (state: DatasourceDataState) => {
return { ...state, loading: true };
},

View File

@ -51,52 +51,8 @@ function* createDatasourceSaga(
actionPayload: ReduxAction<CreateDatasourceConfig>,
) {
try {
let formConfig;
const initialValues = {};
const parseConfig = (section: any): any => {
return _.map(section.children, (subSection: any) => {
if ("children" in subSection) {
return parseConfig(subSection);
} else {
if (subSection.initialValue) {
_.set(
initialValues,
subSection.configProperty,
subSection.initialValue,
);
}
}
});
};
formConfig = yield select(getPluginForm, actionPayload.payload.pluginId);
if (!formConfig) {
const formConfigResponse: GenericApiResponse<DatasourceForm> = yield PluginApi.fetchFormConfig(
actionPayload.payload.pluginId,
);
yield validateResponse(formConfigResponse);
yield put({
type: ReduxActionTypes.FETCH_PLUGIN_FORM_SUCCESS,
payload: {
id: actionPayload.payload.pluginId,
form: formConfigResponse.data.form,
},
});
formConfig = yield select(getPluginForm, actionPayload.payload.pluginId);
}
formConfig.map((section: any) => {
parseConfig(section);
});
const payload = {
...initialValues,
...actionPayload.payload,
};
const response: GenericApiResponse<Datasource> = yield DatasourcesApi.createDatasource(
payload,
actionPayload.payload,
);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
@ -104,35 +60,13 @@ function* createDatasourceSaga(
dataSourceName: actionPayload.payload.name,
appName: actionPayload.payload.appName,
});
yield put({
type: ReduxActionTypes.UPDATE_DATASOURCE_REFS,
payload: response.data,
});
yield put({
type: ReduxActionTypes.CREATE_DATASOURCE_SUCCESS,
payload: response.data,
});
const applicationId = yield select(getCurrentApplicationId);
const pageId = yield select(getCurrentPageId);
yield put(initialize(DATASOURCE_DB_FORM, response.data));
history.push(
DATA_SOURCES_EDITOR_ID_URL(applicationId, pageId, response.data.id),
);
AppToaster.show({
message: `${actionPayload.payload.name} Datasource created`,
type: ToastType.SUCCESS,
});
yield put(
change(API_EDITOR_FORM_NAME, "datasource.id", response.data.id),
);
const datasourceRefs = yield select(getDatasourceRefs);
datasourceRefs[response.data.id].current.scrollIntoView({
behavior: "smooth",
});
}
} catch (error) {
yield put({
@ -243,10 +177,106 @@ function* testDatasourceSaga(actionPayload: ReduxAction<Datasource>) {
}
}
function* createDatasourceFromFormSaga(
actionPayload: ReduxAction<CreateDatasourceConfig>,
) {
try {
let formConfig;
const initialValues = {};
const parseConfig = (section: any): any => {
return _.map(section.children, (subSection: any) => {
if ("children" in subSection) {
return parseConfig(subSection);
} else {
if (subSection.initialValue) {
_.set(
initialValues,
subSection.configProperty,
subSection.initialValue,
);
}
}
});
};
formConfig = yield select(getPluginForm, actionPayload.payload.pluginId);
if (!formConfig) {
const formConfigResponse: GenericApiResponse<DatasourceForm> = yield PluginApi.fetchFormConfig(
actionPayload.payload.pluginId,
);
yield validateResponse(formConfigResponse);
yield put({
type: ReduxActionTypes.FETCH_PLUGIN_FORM_SUCCESS,
payload: {
id: actionPayload.payload.pluginId,
form: formConfigResponse.data.form,
},
});
formConfig = yield select(getPluginForm, actionPayload.payload.pluginId);
}
formConfig.map((section: any) => {
parseConfig(section);
});
const payload = {
...initialValues,
...actionPayload.payload,
};
const response: GenericApiResponse<Datasource> = yield DatasourcesApi.createDatasource(
payload,
);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
AnalyticsUtil.logEvent("SAVE_DATA_SOURCE", {
dataSourceName: actionPayload.payload.name,
appName: actionPayload.payload.appName,
});
yield put({
type: ReduxActionTypes.UPDATE_DATASOURCE_REFS,
payload: response.data,
});
yield put({
type: ReduxActionTypes.CREATE_DATASOURCE_SUCCESS,
payload: response.data,
});
const applicationId = yield select(getCurrentApplicationId);
const pageId = yield select(getCurrentPageId);
yield put(initialize(DATASOURCE_DB_FORM, response.data));
history.push(
DATA_SOURCES_EDITOR_ID_URL(applicationId, pageId, response.data.id),
);
AppToaster.show({
message: `${actionPayload.payload.name} Datasource created`,
type: ToastType.SUCCESS,
});
const datasourceRefs = yield select(getDatasourceRefs);
datasourceRefs[response.data.id].current.scrollIntoView({
behavior: "smooth",
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.CREATE_DATASOURCE_ERROR,
payload: { error },
});
}
}
export function* watchDatasourcesSagas() {
yield all([
takeEvery(ReduxActionTypes.FETCH_DATASOURCES_INIT, fetchDatasourcesSaga),
takeEvery(ReduxActionTypes.CREATE_DATASOURCE_INIT, createDatasourceSaga),
takeEvery(
ReduxActionTypes.CREATE_DATASOURCE_FROM_FORM_INIT,
createDatasourceFromFormSaga,
),
takeEvery(ReduxActionTypes.UPDATE_DATASOURCE_INIT, updateDatasourceSaga),
takeEvery(ReduxActionTypes.TEST_DATASOURCE_INIT, testDatasourceSaga),
takeEvery(ReduxActionTypes.DELETE_DATASOURCE_INIT, deleteDatasourceSaga),