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

392 lines
11 KiB
TypeScript
Raw Normal View History

import { call, takeLatest, put, all, select } from "redux-saga/effects";
import {
ReduxActionTypes,
ReduxAction,
ReduxActionErrorTypes,
2020-02-03 10:37:03 +00:00
ReduxActionWithPromise,
} from "@appsmith/constants/ReduxActionConstants";
2020-02-03 10:37:03 +00:00
import {
validateResponse,
callAPI,
getResponseErrorMessage,
} from "sagas/ErrorSagas";
import WorkspaceApi, {
FetchWorkspaceRolesResponse,
SaveWorkspaceRequest,
FetchWorkspaceRequest,
FetchWorkspaceResponse,
CreateWorkspaceRequest,
FetchAllUsersResponse,
FetchAllUsersRequest,
FetchAllRolesResponse,
DeleteWorkspaceUserRequest,
ChangeUserRoleRequest,
FetchAllRolesRequest,
SaveWorkspaceLogo,
} from "api/WorkspaceApi";
import { ApiResponse } from "api/ApiResponses";
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-13 20:13:44 +00:00
import { Toaster } from "design-system";
2020-11-24 07:01:37 +00:00
import { Variant } from "components/ads/common";
import { getCurrentWorkspace } from "@appsmith/selectors/workspaceSelectors";
import { getCurrentUser } from "selectors/usersSelectors";
import { Workspace } from "constants/workspaceConstants";
2021-01-21 06:19:06 +00:00
import history from "utils/history";
import { APPLICATIONS_URL } from "constants/routes";
2021-01-21 06:19:06 +00:00
import { getAllApplications } from "actions/applicationActions";
import log from "loglevel";
import { User } from "constants/userConstants";
refactor: admin settings (#9906) * refactor admin settings feature * separated save-restart bar to separate component * created new CE dir to facilitate code split * created separate ee dir and exporting everything we have in ce file. * little mod * minor fix * splitting settings types config * using object literals for category types instead of enums * CE: support use of component for each category * minor style fix * authentication page UI changes implemented * github signup doc url added back * removed comments * routing updates * made subcategories listing in left pane optional * added muted saml to auth listing * added breadcrumbs and enabled button * created separate component for auth page and auth config * added callout and disconnect components * updated breadcrumbs component * minor updates to common components * updated warning callout and added icon * ce: test cases fixed * updated test file name * warning banner callout added on auth page * updated callout banner for form login * CE: Split config files * CE: moved the window declaration in EE file as its dependency will be updated in EE * CE: Splitting ApiConstants and SocialLogin constants * CE: split login page * CE: moved getSocialLoginButtonProps func to EE file as it's dependencies will be updated in EE * added key icon * CE: created a factory class to share social auths list * Minor style fix for social btns * Updated the third party auth styles * Small fixes to styling * ce: splitting forms constants * breadcrumbs implemented for all pages in admin settings * Settings breadcrumbs separated * splitted settings breadcrumbs between ce and ee * renamed default import * minor style fix * added login form config. * updated login/signup pages to use form login disabled config * removed common functionality outside * implemented breadcrumb component from scratch without using blueprint * removed unwanted code * Small style update * updated breadcrumb categories file name and breadcrumb icon * added cypress tests for admin settings auth page * added comments * update locator for upgrade button * added link for intercom on upgrade button * removed unnecessary file * minor style fix * style fix for auth option cards * split messages constant * fixed imports for message constants splitting. * added message constants * updated unit test cases * fixed messages import in cypress index * fixed messages import again, cypress fails to read re-exported objs. * added OIDC auth method on authentication page * updated import statements from ee to @appsmith * removed dead code * updated read more link UI * PR comments fixes * some UI fixes * used color and fonts from theme * fixed some imports * fixed some imports * removed warning imports * updated OIDC logo and auth method desc copies * css changes * css changes * css changes * updated cypress test for breadcrumb * moved callout component to ads as calloutv2 * UI changes for form fields * updated css for spacing between form fields * added sub-text on auth pages * added active class for breadcrumb item * added config for disable signup toggle and fixed UI issues of restart banner * fixed admin settings page bugs * assigned true as default state for signup * fixed messages import statements * updated code for PR comments related suggestions * reverted file path change in cypress support * updated cypress test * updated cypress test Co-authored-by: Ankita Kinger <ankita@appsmith.com>
2022-02-11 18:08:46 +00:00
import {
createMessage,
DELETE_WORKSPACE_SUCCESSFUL,
refactor: admin settings (#9906) * refactor admin settings feature * separated save-restart bar to separate component * created new CE dir to facilitate code split * created separate ee dir and exporting everything we have in ce file. * little mod * minor fix * splitting settings types config * using object literals for category types instead of enums * CE: support use of component for each category * minor style fix * authentication page UI changes implemented * github signup doc url added back * removed comments * routing updates * made subcategories listing in left pane optional * added muted saml to auth listing * added breadcrumbs and enabled button * created separate component for auth page and auth config * added callout and disconnect components * updated breadcrumbs component * minor updates to common components * updated warning callout and added icon * ce: test cases fixed * updated test file name * warning banner callout added on auth page * updated callout banner for form login * CE: Split config files * CE: moved the window declaration in EE file as its dependency will be updated in EE * CE: Splitting ApiConstants and SocialLogin constants * CE: split login page * CE: moved getSocialLoginButtonProps func to EE file as it's dependencies will be updated in EE * added key icon * CE: created a factory class to share social auths list * Minor style fix for social btns * Updated the third party auth styles * Small fixes to styling * ce: splitting forms constants * breadcrumbs implemented for all pages in admin settings * Settings breadcrumbs separated * splitted settings breadcrumbs between ce and ee * renamed default import * minor style fix * added login form config. * updated login/signup pages to use form login disabled config * removed common functionality outside * implemented breadcrumb component from scratch without using blueprint * removed unwanted code * Small style update * updated breadcrumb categories file name and breadcrumb icon * added cypress tests for admin settings auth page * added comments * update locator for upgrade button * added link for intercom on upgrade button * removed unnecessary file * minor style fix * style fix for auth option cards * split messages constant * fixed imports for message constants splitting. * added message constants * updated unit test cases * fixed messages import in cypress index * fixed messages import again, cypress fails to read re-exported objs. * added OIDC auth method on authentication page * updated import statements from ee to @appsmith * removed dead code * updated read more link UI * PR comments fixes * some UI fixes * used color and fonts from theme * fixed some imports * fixed some imports * removed warning imports * updated OIDC logo and auth method desc copies * css changes * css changes * css changes * updated cypress test for breadcrumb * moved callout component to ads as calloutv2 * UI changes for form fields * updated css for spacing between form fields * added sub-text on auth pages * added active class for breadcrumb item * added config for disable signup toggle and fixed UI issues of restart banner * fixed admin settings page bugs * assigned true as default state for signup * fixed messages import statements * updated code for PR comments related suggestions * reverted file path change in cypress support * updated cypress test * updated cypress test Co-authored-by: Ankita Kinger <ankita@appsmith.com>
2022-02-11 18:08:46 +00:00
} from "@appsmith/constants/messages";
export function* fetchRolesSaga() {
try {
const response: FetchWorkspaceRolesResponse = yield call(
WorkspaceApi.fetchRoles,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_WORKSPACE_ROLES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
log.error(error);
yield put({
type: ReduxActionErrorTypes.FETCH_WORKSPACE_ROLES_ERROR,
payload: {
error,
},
});
}
}
export function* fetchWorkspaceSaga(
action: ReduxAction<FetchWorkspaceRequest>,
) {
try {
const request: FetchWorkspaceRequest = action.payload;
const response: FetchWorkspaceResponse = yield call(
WorkspaceApi.fetchWorkspace,
request,
);
const isValidResponse: boolean = yield request.skipValidation ||
validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_WORKSPACE_SUCCESS,
payload: response.data || {},
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_WORKSPACE_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllUsersSaga(action: ReduxAction<FetchAllUsersRequest>) {
try {
const request: FetchAllUsersRequest = action.payload;
const response: FetchAllUsersResponse = yield call(
WorkspaceApi.fetchAllUsers,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
2020-12-24 04:32:25 +00:00
const users = response.data.map((user) => ({
...user,
isDeleting: false,
isChangingRole: false,
}));
yield put({
type: ReduxActionTypes.FETCH_ALL_USERS_SUCCESS,
payload: users,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_ALL_USERS_ERROR,
payload: {
error,
},
});
}
}
export function* changeWorkspaceUserRoleSaga(
action: ReduxAction<ChangeUserRoleRequest>,
) {
try {
const request: ChangeUserRoleRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.changeWorkspaceUserRole,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.CHANGE_WORKSPACE_USER_ROLE_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.CHANGE_WORKSPACE_USER_ROLE_ERROR,
payload: {
error,
},
});
}
}
export function* deleteWorkspaceUserSaga(
action: ReduxAction<DeleteWorkspaceUserRequest>,
) {
try {
const request: DeleteWorkspaceUserRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspaceUser,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const currentUser: User | undefined = yield select(getCurrentUser);
if (currentUser?.username == action.payload.username) {
history.replace(APPLICATIONS_URL);
} else {
yield put({
type: ReduxActionTypes.DELETE_WORKSPACE_USER_SUCCESS,
payload: {
username: action.payload.username,
},
});
}
2020-11-24 07:01:37 +00:00
Toaster.show({
//@ts-expect-error: response is of type unknown
2020-11-24 07:01:37 +00:00
text: `${response.data.username} has been removed successfully`,
variant: Variant.success,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_WORKSPACE_USER_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllRolesSaga(action: ReduxAction<FetchAllRolesRequest>) {
try {
const request: FetchAllRolesRequest = action.payload;
const response: FetchAllRolesResponse = yield call(
WorkspaceApi.fetchAllRoles,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_ALL_ROLES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_ALL_ROLES_ERROR,
});
}
}
export function* saveWorkspaceSaga(action: ReduxAction<SaveWorkspaceRequest>) {
try {
const request: SaveWorkspaceRequest = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.saveWorkspace,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: request,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.SAVE_WORKSPACE_ERROR,
payload: {
error: (error as Error).message,
},
});
}
}
export function* deleteWorkspaceSaga(action: ReduxAction<string>) {
try {
yield put({
type: ReduxActionTypes.SAVING_WORKSPACE_INFO,
});
const workspaceId: string = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspace,
workspaceId,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.DELETE_WORKSPACE_SUCCESS,
payload: workspaceId,
});
Toaster.show({
text: createMessage(DELETE_WORKSPACE_SUCCESSFUL),
variant: Variant.success,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_WORKSPACE_ERROR,
payload: {
error: (error as Error).message,
},
});
}
}
export function* createWorkspaceSaga(
action: ReduxActionWithPromise<CreateWorkspaceRequest>,
2020-02-03 10:37:03 +00:00
) {
const { name, reject, resolve } = action.payload;
2020-02-03 10:37:03 +00:00
try {
const request: CreateWorkspaceRequest = { name };
const response: ApiResponse = yield callAPI(
WorkspaceApi.createWorkspace,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
2020-02-03 10:37:03 +00:00
if (!isValidResponse) {
const errorMessage: string | undefined = yield getResponseErrorMessage(
response,
);
2020-02-03 10:37:03 +00:00
yield call(reject, { _error: errorMessage });
} else {
yield put({
type: ReduxActionTypes.CREATE_WORKSPACE_SUCCESS,
2020-02-03 10:37:03 +00:00
payload: response.data,
});
2021-01-21 06:19:06 +00:00
yield put(getAllApplications());
2020-02-03 10:37:03 +00:00
yield call(resolve);
}
2021-01-21 06:19:06 +00:00
2022-07-20 11:54:16 +00:00
// get created workspace in focus
// @ts-expect-error: response is of type unknown
const workspaceId = response.data.id;
history.push(`${window.location.pathname}#${workspaceId}`);
2020-02-03 10:37:03 +00:00
} catch (error) {
yield call(reject, { _error: (error as Error).message });
2020-02-03 10:37:03 +00:00
yield put({
type: ReduxActionErrorTypes.CREATE_WORKSPACE_ERROR,
2020-02-03 10:37:03 +00:00
payload: {
error,
},
});
}
}
export function* uploadWorkspaceLogoSaga(
action: ReduxAction<SaveWorkspaceLogo>,
) {
try {
const request = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.saveWorkspaceLogo,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const allWorkspaces: Workspace[] = yield select(getCurrentWorkspace);
const currentWorkspace = allWorkspaces.filter(
(el: Workspace) => el.id === request.id,
);
if (currentWorkspace.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: {
id: currentWorkspace[0].id,
// @ts-expect-error: response is of type unknown
logoUrl: response.data.logoUrl,
},
});
Toaster.show({
text: "Logo uploaded successfully",
variant: Variant.success,
});
}
}
} catch (error) {
log.error("Error occured while uploading the logo", error);
}
}
export function* deleteWorkspaceLogoSaga(action: ReduxAction<{ id: string }>) {
try {
const request = action.payload;
const response: ApiResponse = yield call(
WorkspaceApi.deleteWorkspaceLogo,
request,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
const allWorkspaces: Workspace[] = yield select(getCurrentWorkspace);
const currentWorkspace = allWorkspaces.filter(
(el: Workspace) => el.id === request.id,
);
if (currentWorkspace.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_WORKSPACE_SUCCESS,
payload: {
id: currentWorkspace[0].id,
// @ts-expect-error: response is of type unknown
logoUrl: response.data.logoUrl,
},
});
Toaster.show({
text: "Logo removed successfully",
variant: Variant.success,
});
}
}
} catch (error) {
log.error("Error occured while removing the logo", error);
}
}
export default function* workspaceSagas() {
yield all([
takeLatest(ReduxActionTypes.FETCH_WORKSPACE_ROLES_INIT, fetchRolesSaga),
takeLatest(ReduxActionTypes.FETCH_CURRENT_WORKSPACE, fetchWorkspaceSaga),
takeLatest(ReduxActionTypes.SAVE_WORKSPACE_INIT, saveWorkspaceSaga),
takeLatest(ReduxActionTypes.CREATE_WORKSPACE_INIT, createWorkspaceSaga),
takeLatest(ReduxActionTypes.FETCH_ALL_USERS_INIT, fetchAllUsersSaga),
takeLatest(ReduxActionTypes.FETCH_ALL_ROLES_INIT, fetchAllRolesSaga),
takeLatest(
ReduxActionTypes.DELETE_WORKSPACE_USER_INIT,
deleteWorkspaceUserSaga,
),
takeLatest(
ReduxActionTypes.CHANGE_WORKSPACE_USER_ROLE_INIT,
changeWorkspaceUserRoleSaga,
),
takeLatest(ReduxActionTypes.DELETE_WORKSPACE_INIT, deleteWorkspaceSaga),
takeLatest(ReduxActionTypes.UPLOAD_WORKSPACE_LOGO, uploadWorkspaceLogoSaga),
takeLatest(ReduxActionTypes.REMOVE_WORKSPACE_LOGO, deleteWorkspaceLogoSaga),
]);
}