PromucFlow_constructor/app/client/src/sagas/TemplatesSagas.ts
Anagh Hegde 82a9d720ae
feat: Added unconfigured datasources to the template API response (#15606)
Co-authored-by: Akash N <akash@codemonk.in>
2022-08-04 09:50:54 +05:30

175 lines
4.7 KiB
TypeScript

import {
ApplicationPayload,
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
} from "@appsmith/constants/ReduxActionConstants";
import { all, put, takeEvery, call } from "redux-saga/effects";
import TemplatesAPI, {
FetchTemplateResponse,
ImportTemplateResponse,
} from "api/TemplatesApi";
import history from "utils/history";
import { getDefaultPageId } from "./ApplicationSagas";
import { setTemplateNotificationSeenAction } from "actions/templateActions";
import {
getTemplateNotificationSeen,
setTemplateNotificationSeen,
} from "utils/storage";
import { validateResponse } from "./ErrorSagas";
import { builderURL } from "RouteBuilder";
import { showReconnectDatasourceModal } from "actions/applicationActions";
function* getAllTemplatesSaga() {
try {
const response: FetchTemplateResponse = yield call(
TemplatesAPI.getAllTemplates,
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
yield put({
type: ReduxActionTypes.GET_ALL_TEMPLATES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.GET_ALL_TEMPLATES_ERROR,
payload: {
error,
},
});
}
}
function* importTemplateToWorkspaceSaga(
action: ReduxAction<{ templateId: string; workspaceId: string }>,
) {
try {
const response: ImportTemplateResponse = yield call(
TemplatesAPI.importTemplate,
action.payload.templateId,
action.payload.workspaceId,
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
const application: ApplicationPayload = {
...response.data.application,
defaultPageId: getDefaultPageId(
response.data.application.pages,
) as string,
};
yield put({
type: ReduxActionTypes.IMPORT_TEMPLATE_TO_WORKSPACE_SUCCESS,
payload: response.data.application,
});
if (response.data.isPartialImport) {
yield put(
showReconnectDatasourceModal({
application: response.data.application,
unConfiguredDatasourceList:
response.data.unConfiguredDatasourceList,
workspaceId: action.payload.workspaceId,
}),
);
} else {
const pageURL = builderURL({
pageId: application.defaultPageId,
});
history.push(pageURL);
}
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.IMPORT_TEMPLATE_TO_WORKSPACE_ERROR,
payload: {
error,
},
});
}
}
function* getSimilarTemplatesSaga(action: ReduxAction<string>) {
try {
const response: FetchTemplateResponse = yield call(
TemplatesAPI.getSimilarTemplates,
action.payload,
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
yield put({
type: ReduxActionTypes.GET_SIMILAR_TEMPLATES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.GET_SIMILAR_TEMPLATES_ERROR,
payload: {
error,
},
});
}
}
function* setTemplateNotificationSeenSaga(action: ReduxAction<boolean>) {
yield setTemplateNotificationSeen(action.payload);
}
function* getTemplateNotificationSeenSaga() {
const showTemplateNotification: unknown = yield getTemplateNotificationSeen();
if (showTemplateNotification) {
yield put(setTemplateNotificationSeenAction(true));
} else {
yield put(setTemplateNotificationSeenAction(false));
}
}
function* getTemplateSaga(action: ReduxAction<string>) {
try {
const response: FetchTemplateResponse = yield call(
TemplatesAPI.getTemplateInformation,
action.payload,
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
yield put({
type: ReduxActionTypes.GET_TEMPLATE_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.GET_TEMPLATE_ERROR,
payload: {
error,
},
});
}
}
export default function* watchActionSagas() {
yield all([
takeEvery(ReduxActionTypes.GET_ALL_TEMPLATES_INIT, getAllTemplatesSaga),
takeEvery(ReduxActionTypes.GET_TEMPLATE_INIT, getTemplateSaga),
takeEvery(
ReduxActionTypes.GET_SIMILAR_TEMPLATES_INIT,
getSimilarTemplatesSaga,
),
takeEvery(
ReduxActionTypes.IMPORT_TEMPLATE_TO_WORKSPACE_INIT,
importTemplateToWorkspaceSaga,
),
takeEvery(
ReduxActionTypes.GET_TEMPLATE_NOTIFICATION_SEEN,
getTemplateNotificationSeenSaga,
),
takeEvery(
ReduxActionTypes.SET_TEMPLATE_NOTIFICATION_SEEN,
setTemplateNotificationSeenSaga,
),
]);
}