PromucFlow_constructor/app/client/src/sagas/OrgSagas.ts
Pranav Kanade b778b83ac4
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 23:38:46 +05:30

346 lines
9.8 KiB
TypeScript

import { call, takeLatest, put, all, select } from "redux-saga/effects";
import {
ReduxActionTypes,
ReduxAction,
ReduxActionErrorTypes,
ReduxActionWithPromise,
} from "constants/ReduxActionConstants";
import {
validateResponse,
callAPI,
getResponseErrorMessage,
} from "sagas/ErrorSagas";
import OrgApi, {
FetchOrgRolesResponse,
SaveOrgRequest,
FetchOrgRequest,
FetchOrgResponse,
CreateOrgRequest,
FetchAllUsersResponse,
FetchAllUsersRequest,
FetchAllRolesResponse,
DeleteOrgUserRequest,
ChangeUserRoleRequest,
FetchAllRolesRequest,
SaveOrgLogo,
} from "api/OrgApi";
import { ApiResponse } from "api/ApiResponses";
import { Toaster } from "components/ads/Toast";
import { Variant } from "components/ads/common";
import { getCurrentOrg } from "selectors/organizationSelectors";
import { getCurrentUser } from "selectors/usersSelectors";
import { Org } from "constants/orgConstants";
import history from "utils/history";
import { APPLICATIONS_URL } from "constants/routes";
import { getAllApplications } from "actions/applicationActions";
import log from "loglevel";
import {
createMessage,
DELETE_ORG_SUCCESSFUL,
} from "@appsmith/constants/messages";
export function* fetchRolesSaga() {
try {
const response: FetchOrgRolesResponse = yield call(OrgApi.fetchRoles);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_ORG_ROLES_SUCCESS,
payload: response.data,
});
}
} catch (error) {
log.error(error);
yield put({
type: ReduxActionErrorTypes.FETCH_ORG_ROLES_ERROR,
payload: {
error,
},
});
}
}
export function* fetchOrgSaga(action: ReduxAction<FetchOrgRequest>) {
try {
const request: FetchOrgRequest = action.payload;
const response: FetchOrgResponse = yield call(OrgApi.fetchOrg, request);
const isValidResponse = yield request.skipValidation ||
validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_ORG_SUCCESS,
payload: response.data || {},
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_ORG_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllUsersSaga(action: ReduxAction<FetchAllUsersRequest>) {
try {
const request: FetchAllUsersRequest = action.payload;
const response: FetchAllUsersResponse = yield call(
OrgApi.fetchAllUsers,
request,
);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
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* changeOrgUserRoleSaga(
action: ReduxAction<ChangeUserRoleRequest>,
) {
try {
const request: ChangeUserRoleRequest = action.payload;
const response: ApiResponse = yield call(OrgApi.changeOrgUserRole, request);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.CHANGE_ORG_USER_ROLE_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.CHANGE_ORG_USER_ROLE_ERROR,
payload: {
error,
},
});
}
}
export function* deleteOrgUserSaga(action: ReduxAction<DeleteOrgUserRequest>) {
try {
const request: DeleteOrgUserRequest = action.payload;
const response: ApiResponse = yield call(OrgApi.deleteOrgUser, request);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
const currentUser = yield select(getCurrentUser);
if (currentUser?.username == action.payload.username) {
history.replace(APPLICATIONS_URL);
} else {
yield put({
type: ReduxActionTypes.DELETE_ORG_USER_SUCCESS,
payload: {
username: action.payload.username,
},
});
}
Toaster.show({
text: `${response.data.username} has been removed successfully`,
variant: Variant.success,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_ORG_USER_ERROR,
payload: {
error,
},
});
}
}
export function* fetchAllRolesSaga(action: ReduxAction<FetchAllRolesRequest>) {
try {
const request: FetchAllRolesRequest = action.payload;
const response: FetchAllRolesResponse = yield call(
OrgApi.fetchAllRoles,
request,
);
const isValidResponse = 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* saveOrgSaga(action: ReduxAction<SaveOrgRequest>) {
try {
const request: SaveOrgRequest = action.payload;
const response: ApiResponse = yield call(OrgApi.saveOrg, request);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.SAVE_ORG_SUCCESS,
payload: request,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.SAVE_ORG_ERROR,
payload: {
error: error.message,
},
});
}
}
export function* deleteOrgSaga(action: ReduxAction<string>) {
try {
yield put({
type: ReduxActionTypes.SAVING_ORG_INFO,
});
const orgId: string = action.payload;
const response: ApiResponse = yield call(OrgApi.deleteOrg, orgId);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.DELETE_ORG_SUCCESS,
payload: orgId,
});
Toaster.show({
text: createMessage(DELETE_ORG_SUCCESSFUL),
variant: Variant.success,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.DELETE_ORG_ERROR,
payload: {
error: error.message,
},
});
}
}
export function* createOrgSaga(
action: ReduxActionWithPromise<CreateOrgRequest>,
) {
const { name, reject, resolve } = action.payload;
try {
const request: CreateOrgRequest = { name };
const response: ApiResponse = yield callAPI(OrgApi.createOrg, request);
const isValidResponse = yield validateResponse(response);
if (!isValidResponse) {
const errorMessage = yield getResponseErrorMessage(response);
yield call(reject, { _error: errorMessage });
} else {
yield put({
type: ReduxActionTypes.CREATE_ORGANIZATION_SUCCESS,
payload: response.data,
});
yield put(getAllApplications());
yield call(resolve);
}
// get created org in focus
const slug = response.data.slug;
history.push(`${window.location.pathname}#${slug}`);
} catch (error) {
yield call(reject, { _error: error.message });
yield put({
type: ReduxActionErrorTypes.CREATE_ORGANIZATION_ERROR,
payload: {
error,
},
});
}
}
export function* uploadOrgLogoSaga(action: ReduxAction<SaveOrgLogo>) {
try {
const request = action.payload;
const response: ApiResponse = yield call(OrgApi.saveOrgLogo, request);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
const allOrgs = yield select(getCurrentOrg);
const currentOrg = allOrgs.filter((el: Org) => el.id === request.id);
if (currentOrg.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_ORG_SUCCESS,
payload: {
id: currentOrg[0].id,
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* deleteOrgLogoSaga(action: ReduxAction<{ id: string }>) {
try {
const request = action.payload;
const response: ApiResponse = yield call(OrgApi.deleteOrgLogo, request);
const isValidResponse = yield validateResponse(response);
if (isValidResponse) {
const allOrgs = yield select(getCurrentOrg);
const currentOrg = allOrgs.filter((el: Org) => el.id === request.id);
if (currentOrg.length > 0) {
yield put({
type: ReduxActionTypes.SAVE_ORG_SUCCESS,
payload: {
id: currentOrg[0].id,
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* orgSagas() {
yield all([
takeLatest(ReduxActionTypes.FETCH_ORG_ROLES_INIT, fetchRolesSaga),
takeLatest(ReduxActionTypes.FETCH_CURRENT_ORG, fetchOrgSaga),
takeLatest(ReduxActionTypes.SAVE_ORG_INIT, saveOrgSaga),
takeLatest(ReduxActionTypes.CREATE_ORGANIZATION_INIT, createOrgSaga),
takeLatest(ReduxActionTypes.FETCH_ALL_USERS_INIT, fetchAllUsersSaga),
takeLatest(ReduxActionTypes.FETCH_ALL_ROLES_INIT, fetchAllRolesSaga),
takeLatest(ReduxActionTypes.DELETE_ORG_USER_INIT, deleteOrgUserSaga),
takeLatest(
ReduxActionTypes.CHANGE_ORG_USER_ROLE_INIT,
changeOrgUserRoleSaga,
),
takeLatest(ReduxActionTypes.DELETE_ORG_INIT, deleteOrgSaga),
takeLatest(ReduxActionTypes.UPLOAD_ORG_LOGO, uploadOrgLogoSaga),
takeLatest(ReduxActionTypes.REMOVE_ORG_LOGO, deleteOrgLogoSaga),
]);
}