PromucFlow_constructor/app/client/src/sagas/TemplatesSagas.ts
Tanvi Bhakta ac0c872843
chore: migrate toast (#17208)
* Refactor toast to be passed the dispatch hook externally

* Add comments explaining dilemma

* use store.dispatch instead of a hook

* use alpha version

* Change imports

* Refactor DebugButton out

* update release

* fix issue with incorrectly merged package.lock

* fix syntax of alpha version

* bump ds vesion

* copy lock from release

* update lock to have alpha

* make changes

* delete Toast

* DS package version updated

* import change from release

* use new alpha version

* update ds version

* update ds version

* chore: migrate editable text and friends (#17285)

* Delete empty components

* use alpha for ds

* Deleted EditableTextSubComponent, import changes

* Delete EditableText, import changes

* use ds alpha 10

* Delete EditableTextWrapper.tsx

* update ds to use next minor version

* use new alpha

* fix issue with merge

Co-authored-by: Albin <albin@appsmith.com>

* chore: migrate file picker v2 (#17308)

* use alpha ds

* Delete FilePickerV2, import changes

* Delete FilePicker, change imports

* update alpha version

* chore: move copy url form into setting components (#17322)

* move CopyUrlForm to src/pages/settings/formgroup

* update ds version to use next minor release

* feat: Migrate table component to design system (#17329)

* feat: Migrate table component to design system

* removed commented code in ads index file

* fix: table no data hover effect removed

Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>

* feat: Banner message component migrated to design system (#17327)

* feat: Banner image component migrated to design system

* Version update for design system package

* design system version updated

Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>

* feat: Tabs component migrated to design system (#17321)

* feat: Tabs component migrated to design system

* design system package version updated

* Update app/client/src/components/editorComponents/form/FormDialogComponent.tsx

* Update app/client/src/pages/Editor/PropertyPane/PropertyPaneTab.tsx

* Tab component expand issue fix

Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>

Co-authored-by: Albin <albin@appsmith.com>
Co-authored-by: albinAppsmith <87797149+albinAppsmith@users.noreply.github.com>
2022-10-14 01:43:44 +05:30

337 lines
9.3 KiB
TypeScript

import {
ApplicationPayload,
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
} from "@appsmith/constants/ReduxActionConstants";
import { all, put, takeEvery, call, select, take } from "redux-saga/effects";
import TemplatesAPI, {
ImportTemplateResponse,
FetchTemplateResponse,
TemplateFiltersResponse,
} from "api/TemplatesApi";
import history from "utils/history";
import { getDefaultPageId } from "./ApplicationSagas";
import {
getAllTemplates,
setTemplateNotificationSeenAction,
showTemplatesModal,
} from "actions/templateActions";
import {
getTemplateNotificationSeen,
setTemplateNotificationSeen,
} from "utils/storage";
import { validateResponse } from "./ErrorSagas";
import { builderURL } from "RouteBuilder";
import { getCurrentApplicationId } from "selectors/editorSelectors";
import { getCurrentWorkspaceId } from "@appsmith/selectors/workspaceSelectors";
import { fetchApplication } from "actions/applicationActions";
import { APP_MODE } from "entities/App";
import {
executePageLoadActions,
fetchActions,
} from "actions/pluginActionActions";
import { fetchJSCollections } from "actions/jsActionActions";
import { failFastApiCalls } from "./InitSagas";
import { Toaster } from "design-system";
import { Variant } from "components/ads/common";
import { fetchDatasources } from "actions/datasourceActions";
import { fetchPluginFormConfigs } from "actions/pluginActions";
import { fetchAllPageEntityCompletion, saveLayout } from "actions/pageActions";
import { showReconnectDatasourceModal } from "actions/applicationActions";
import { getAllPageIds } from "./selectors";
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);
}
yield put(getAllTemplates());
}
} 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,
},
});
}
}
function* postPageAdditionSaga(applicationId: string) {
const afterActionsFetch: boolean = yield failFastApiCalls(
[
fetchActions({ applicationId }, []),
fetchJSCollections({ applicationId }),
fetchDatasources(),
],
[
ReduxActionTypes.FETCH_ACTIONS_SUCCESS,
ReduxActionTypes.FETCH_JS_ACTIONS_SUCCESS,
ReduxActionTypes.FETCH_DATASOURCES_SUCCESS,
],
[
ReduxActionErrorTypes.FETCH_ACTIONS_ERROR,
ReduxActionErrorTypes.FETCH_JS_ACTIONS_ERROR,
ReduxActionErrorTypes.FETCH_DATASOURCES_ERROR,
],
);
if (!afterActionsFetch) {
throw new Error("Failed importing template");
}
const afterPluginFormsFetch: boolean = yield failFastApiCalls(
[fetchPluginFormConfigs()],
[ReduxActionTypes.FETCH_PLUGIN_FORM_CONFIGS_SUCCESS],
[ReduxActionErrorTypes.FETCH_PLUGIN_FORM_CONFIGS_ERROR],
);
if (!afterPluginFormsFetch) {
throw new Error("Failed importing template");
}
yield put(fetchAllPageEntityCompletion([executePageLoadActions()]));
}
function* forkTemplateToApplicationSaga(
action: ReduxAction<{
templateId: string;
templateName: string;
pageNames?: string[];
}>,
) {
try {
const pagesToImport = action.payload.pageNames
? action.payload.pageNames
: undefined;
const applicationId: string = yield select(getCurrentApplicationId);
const workspaceId: string = yield select(getCurrentWorkspaceId);
const response: ImportTemplateResponse = yield call(
TemplatesAPI.importTemplateToApplication,
action.payload.templateId,
applicationId,
workspaceId,
pagesToImport,
);
// To fetch the new set of pages after merging the template into the existing application
yield put(
fetchApplication({
mode: APP_MODE.EDIT,
applicationId,
}),
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
yield call(postPageAdditionSaga, applicationId);
const pages: string[] = yield select(getAllPageIds);
if (response.data.isPartialImport) {
yield put(
showReconnectDatasourceModal({
application: response.data.application,
unConfiguredDatasourceList:
response.data.unConfiguredDatasourceList,
workspaceId,
pageId: pages[0],
}),
);
}
history.push(
builderURL({
pageId: pages[0],
}),
);
yield put(showTemplatesModal(false));
yield take(ReduxActionTypes.UPDATE_CANVAS_STRUCTURE);
yield put(saveLayout());
yield put({
type: ReduxActionTypes.IMPORT_TEMPLATE_TO_APPLICATION_SUCCESS,
payload: response.data.application,
});
yield put(getAllTemplates());
Toaster.show({
text: `Pages from '${action.payload.templateName}' template added successfully`,
variant: Variant.success,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.IMPORT_TEMPLATE_TO_APPLICATION_ERROR,
payload: {
error,
},
});
}
}
function* getTemplateFiltersSaga() {
try {
const response: TemplateFiltersResponse = yield call(
TemplatesAPI.getTemplateFilters,
);
const isValid: boolean = yield validateResponse(response);
if (isValid) {
yield put({
type: ReduxActionTypes.GET_TEMPLATE_FILTERS_SUCCESS,
payload: response.data,
});
}
} catch (e) {
yield put({
type: ReduxActionErrorTypes.GET_TEMPLATE_FILTERS_ERROR,
payload: {
e,
},
});
}
}
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,
),
takeEvery(
ReduxActionTypes.IMPORT_TEMPLATE_TO_APPLICATION_INIT,
forkTemplateToApplicationSaga,
),
takeEvery(
ReduxActionTypes.GET_TEMPLATE_FILTERS_INIT,
getTemplateFiltersSaga,
),
]);
}