feat: Integrating with tenant API on the client to fetch tenant configurations from the server (#17577)

This commit is contained in:
Sangeeth Sivan 2022-10-15 20:21:25 +05:30 committed by GitHub
parent 6e2ebbe2fd
commit 545f3d1c4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 138 additions and 1 deletions

View File

@ -60,6 +60,7 @@ import TemplatesListLoader from "pages/Templates/loader";
import { fetchFeatureFlagsInit } from "actions/userActions";
import FeatureFlags from "entities/FeatureFlags";
import WDSPage from "components/wds/Showcase";
import { getCurrentTenant } from "@appsmith/actions/tenantActions";
const SentryRoute = Sentry.withSentryRouting(Route);
@ -83,12 +84,13 @@ function AppRouter(props: {
safeCrash: boolean;
getCurrentUser: () => void;
getFeatureFlags: () => void;
getCurrentTenant: () => void;
currentTheme: Theme;
safeCrashCode?: ERROR_CODES;
featureFlags: FeatureFlags;
setTheme: (theme: ThemeMode) => void;
}) {
const { getCurrentUser, getFeatureFlags } = props;
const { getCurrentTenant, getCurrentUser, getFeatureFlags } = props;
useEffect(() => {
AnalyticsUtil.logEvent("ROUTE_CHANGE", { path: window.location.pathname });
const stopListener = history.listen((location: any) => {
@ -97,6 +99,7 @@ function AppRouter(props: {
});
getCurrentUser();
getFeatureFlags();
getCurrentTenant();
return stopListener;
}, []);
@ -192,6 +195,7 @@ const mapDispatchToProps = (dispatch: any) => ({
},
getCurrentUser: () => dispatch(getCurrentUser()),
getFeatureFlags: () => dispatch(fetchFeatureFlagsInit()),
getCurrentTenant: () => dispatch(getCurrentTenant()),
});
export default connect(mapStateToProps, mapDispatchToProps)(AppRouter);

View File

@ -0,0 +1,5 @@
import { ReduxActionTypes } from "@appsmith/constants/ReduxActionConstants";
export const getCurrentTenant = () => ({
type: ReduxActionTypes.FETCH_CURRENT_TENANT_CONFIG,
});

View File

@ -0,0 +1,21 @@
import { AxiosPromise } from "axios";
import Api from "api/Api";
import { ApiResponse } from "api/ApiResponses";
export type FetchCurrentTenantConfigResponse = ApiResponse<{
userPermissions: string[];
tenantConfiguration: Record<string, string>;
new: boolean;
}>;
export class TenantApi extends Api {
static tenantsUrl = "v1/tenants";
static fetchCurrentTenantConfig(): AxiosPromise<
FetchCurrentTenantConfigResponse
> {
return Api.get(TenantApi.tenantsUrl + "/current");
}
}
export default TenantApi;

View File

@ -670,6 +670,8 @@ export const ReduxActionTypes = {
INIT_TRIGGER_VALUES: "INIT_TRIGGER_VALUES",
FETCH_TRIGGER_VALUES_INIT: "FETCH_TRIGGER_VALUES_INIT",
FETCH_TRIGGER_VALUES_SUCCESS: "FETCH_TRIGGER_VALUES_SUCCESS",
FETCH_CURRENT_TENANT_CONFIG: "FETCH_CURRENT_TENANT_CONFIG",
FETCH_CURRENT_TENANT_CONFIG_SUCCESS: "FETCH_CURRENT_TENANT_CONFIG_SUCCESS",
};
export type ReduxActionType = typeof ReduxActionTypes[keyof typeof ReduxActionTypes];
@ -840,6 +842,7 @@ export const ReduxActionErrorTypes = {
GET_TEMPLATE_ERROR: "GET_TEMPLATE_ERROR",
GET_TEMPLATE_FILTERS_ERROR: "GET_TEMPLATE_FILTERS_ERROR",
UPDATE_CUSTOM_SLUG_ERROR: "UPDATE_CUSTOM_SLUG_ERROR",
FETCH_CURRENT_TENANT_CONFIG_ERROR: "FETCH_CURRENT_TENANT_CONFIG_ERROR",
};
export const ReduxFormActionTypes = {

View File

@ -61,6 +61,9 @@ import SettingsReducer, {
import { GuidedTourState } from "reducers/uiReducers/guidedTourReducer";
import { TriggerValuesEvaluationState } from "reducers/evaluationReducers/triggerReducer";
import { CanvasWidgetStructure } from "widgets/constants";
import tenantReducer, {
TenantReduxState,
} from "@appsmith/reducers/tenantReducer";
export const reducerObject = {
entities: entityReducer,
@ -68,6 +71,7 @@ export const reducerObject = {
evaluations: evaluationsReducer,
form: formReducer,
settings: SettingsReducer,
tenant: tenantReducer,
};
export interface AppState {
@ -137,4 +141,5 @@ export interface AppState {
[key: string]: any;
};
settings: SettingsReduxState;
tenant: TenantReduxState;
}

View File

@ -0,0 +1,35 @@
import {
ReduxAction,
ReduxActionErrorTypes,
ReduxActionTypes,
} from "@appsmith/constants/ReduxActionConstants";
import { createReducer } from "utils/ReducerUtils";
export interface TenantReduxState {
userPermissions: string[];
tenantConfiguration: Record<string, string>;
new: boolean;
}
export const initialState: TenantReduxState = {
userPermissions: [],
tenantConfiguration: {},
new: false,
};
export const handlers = {
[ReduxActionTypes.FETCH_CURRENT_TENANT_CONFIG_SUCCESS]: (
state: TenantReduxState,
action: ReduxAction<TenantReduxState>,
) => ({
...state,
...action.payload,
}),
[ReduxActionErrorTypes.FETCH_CURRENT_TENANT_CONFIG_ERROR]: (
state: TenantReduxState,
) => ({
...state,
}),
};
export default createReducer(initialState, handlers);

View File

@ -39,6 +39,7 @@ import appThemingSaga from "sagas/AppThemingSaga";
import formEvaluationChangeListener from "sagas/FormEvaluationSaga";
import SuperUserSagas from "@appsmith/sagas/SuperUserSagas";
import PageVisibilitySaga from "sagas/PageVisibilitySagas";
import tenantSagas from "@appsmith/sagas/tenantSagas";
export const sagas = [
initSagas,
@ -82,4 +83,5 @@ export const sagas = [
SuperUserSagas,
appThemingSaga,
PageVisibilitySaga,
tenantSagas,
];

View File

@ -0,0 +1,31 @@
import TenantApi from "@appsmith/api/TenantApi";
import {
ReduxActionErrorTypes,
ReduxActionTypes,
} from "@appsmith/constants/ReduxActionConstants";
import { ApiResponse } from "api/ApiResponses";
import { call, put } from "redux-saga/effects";
import { validateResponse } from "sagas/ErrorSagas";
export function* fetchCurrentTenantConfigSaga() {
try {
const response: ApiResponse = yield call(
TenantApi.fetchCurrentTenantConfig,
);
const isValidResponse: boolean = yield validateResponse(response);
if (isValidResponse) {
yield put({
type: ReduxActionTypes.FETCH_CURRENT_TENANT_CONFIG_SUCCESS,
payload: response.data,
});
}
} catch (error) {
yield put({
type: ReduxActionErrorTypes.FETCH_CURRENT_TENANT_CONFIG_ERROR,
payload: {
error,
},
});
}
}

View File

@ -0,0 +1,5 @@
import { AppState } from "@appsmith/reducers";
export const getTenantPermissions = (state: AppState) => {
return state.tenant.userPermissions;
};

View File

@ -0,0 +1 @@
export * from "ce/actions/tenantActions";

View File

@ -0,0 +1,6 @@
export * from "ce/api/TenantApi";
import { TenantApi as CE_TenantApi } from "ce/api/TenantApi";
class TenantApi extends CE_TenantApi {}
export default TenantApi;

View File

@ -0,0 +1,5 @@
export * from "ce/reducers/tenantReducer";
import { handlers, initialState } from "ce/reducers/tenantReducer";
import { createReducer } from "utils/ReducerUtils";
export default createReducer(initialState, handlers);

View File

@ -0,0 +1,13 @@
export * from "ce/sagas/tenantSagas";
import { ReduxActionTypes } from "ce/constants/ReduxActionConstants";
import { fetchCurrentTenantConfigSaga } from "ce/sagas/tenantSagas";
import { all, takeLatest } from "redux-saga/effects";
export default function* tenantSagas() {
yield all([
takeLatest(
ReduxActionTypes.FETCH_CURRENT_TENANT_CONFIG,
fetchCurrentTenantConfigSaga,
),
]);
}

View File

@ -0,0 +1 @@
export * from "ce/selectors/tenantSelectors";