Skip to content

Commit bf5e552

Browse files
committed
add environments sagas
1 parent 056abbf commit bf5e552

File tree

1 file changed

+155
-10
lines changed

1 file changed

+155
-10
lines changed
Lines changed: 155 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
1-
import { call, put, takeLatest } from 'redux-saga/effects';
1+
import { call, put, takeLatest, all } from "redux-saga/effects";
22
import { ReduxAction, ReduxActionTypes } from "constants/reduxActionConstants";
33
import { setEnterpriseLicense } from "redux/reduxActions/enterpriseActions";
4-
import { BrandingSettingResponse, EnterpriseLicenseResponse, FetchBrandingSettingPayload, getBranding, getEnterpriseLicense } from "api/enterpriseApi";
5-
import { AxiosResponse } from 'axios';
4+
import {
5+
BrandingSettingResponse,
6+
EnterpriseLicenseResponse,
7+
FetchBrandingSettingPayload,
8+
getBranding,
9+
getEnterpriseLicense,
10+
} from "api/enterpriseApi";
11+
import { AxiosResponse } from "axios";
12+
import {
13+
fetchEnvironmentFailure,
14+
fetchEnvironmentSuccess,
15+
fetchEnvironmentsFailure,
16+
fetchEnvironmentsSuccess,
17+
createEnvironmentSuccess,
18+
updateEnvironmentSuccess,
19+
deleteEnvironmentSuccess,
20+
updateEnvironmentApiKeySuccess,
21+
} from "redux/reduxActions/enterpriseActions";
22+
import {
23+
getEnvironments,
24+
getEnvironment,
25+
createEnvironment,
26+
updateEnvironment,
27+
deleteEnvironment,
28+
Environment,
29+
CreateEnvironmentRequest,
30+
UpdateEnvironmentRequest,
31+
} from "api/enterpriseApi";
632

7-
function* fetchEnterpriseLicenseSaga(): Generator<any, void, EnterpriseLicenseResponse> {
33+
function* fetchEnterpriseLicenseSaga(): Generator<
34+
any,
35+
void,
36+
EnterpriseLicenseResponse
37+
> {
838
try {
939
// Type the result from the API call
1040
const data: EnterpriseLicenseResponse = yield call(getEnterpriseLicense);
1141
yield put(setEnterpriseLicense(data));
1242
} catch (error) {
13-
console.error('Failed to fetch enterprise license:', error);
43+
console.error("Failed to fetch enterprise license:", error);
1444
}
1545
}
1646

17-
function* fetchBrandingSettingSaga(action: ReduxAction<FetchBrandingSettingPayload>) {
47+
function* fetchBrandingSettingSaga(
48+
action: ReduxAction<FetchBrandingSettingPayload>
49+
) {
1850
try {
19-
const response: BrandingSettingResponse = yield getBranding(action.payload.orgId);
51+
const response: BrandingSettingResponse = yield getBranding(
52+
action.payload.orgId
53+
);
2054
if (response && response.id) {
2155
if (action.payload.orgId) {
2256
yield put({
@@ -31,11 +65,122 @@ function* fetchBrandingSettingSaga(action: ReduxAction<FetchBrandingSettingPaylo
3165
});
3266
}
3367
} catch (error) {
34-
console.error('Failed to fetch branding setting:', error);
68+
console.error("Failed to fetch branding setting:", error);
3569
}
3670
}
3771

72+
// ENVIRONMENTS SAGAS
73+
74+
function* fetchEnvironmentsSaga(): Generator<any, void, any> {
75+
try {
76+
const response = yield call(getEnvironments);
77+
yield put(fetchEnvironmentsSuccess(response));
78+
} catch (error) {
79+
yield put(fetchEnvironmentsFailure(error));
80+
console.error("Failed to fetch environments:", error);
81+
}
82+
}
83+
84+
function* fetchEnvironmentSaga(
85+
action: ReduxAction<{ environmentId: string }>
86+
): Generator<any, void, any> {
87+
try {
88+
const { environmentId } = action.payload;
89+
const response = yield call(getEnvironment, environmentId);
90+
yield put(fetchEnvironmentSuccess(response));
91+
} catch (error) {
92+
yield put(fetchEnvironmentFailure(error));
93+
console.error("Failed to fetch environment:", error);
94+
}
95+
}
96+
97+
function* createEnvironmentSaga(
98+
action: ReduxAction<CreateEnvironmentRequest>
99+
): Generator<any, void, any> {
100+
try {
101+
const response = yield call(createEnvironment, action.payload);
102+
yield put(createEnvironmentSuccess(response));
103+
} catch (error) {
104+
yield put({
105+
type: ReduxActionTypes.CREATE_ENVIRONMENT_FAILURE,
106+
payload: error,
107+
});
108+
console.error("Failed to create environment:", error);
109+
}
110+
}
111+
112+
function* updateEnvironmentSaga(
113+
action: ReduxAction<UpdateEnvironmentRequest>
114+
): Generator<any, void, any> {
115+
try {
116+
const response = yield call(updateEnvironment, action.payload);
117+
yield put(updateEnvironmentSuccess(response));
118+
} catch (error) {
119+
yield put({
120+
type: ReduxActionTypes.UPDATE_ENVIRONMENT_FAILURE,
121+
payload: error,
122+
});
123+
console.error("Failed to update environment:", error);
124+
}
125+
}
126+
127+
function* deleteEnvironmentSaga(
128+
action: ReduxAction<{ environmentId: string }>
129+
): Generator<any, void, any> {
130+
try {
131+
const { environmentId } = action.payload;
132+
yield call(deleteEnvironment, environmentId);
133+
yield put(deleteEnvironmentSuccess(environmentId));
134+
} catch (error) {
135+
yield put({
136+
type: ReduxActionTypes.DELETE_ENVIRONMENT_FAILURE,
137+
payload: error,
138+
});
139+
console.error("Failed to delete environment:", error);
140+
}
141+
}
142+
143+
function* updateEnvironmentApiKeySaga(
144+
action: ReduxAction<{ environmentId: string; apiKey: string }>
145+
): Generator<any, void, any> {
146+
try {
147+
const { environmentId, apiKey } = action.payload;
148+
yield call(updateEnvironment, {
149+
environmentId,
150+
environmentApikey: apiKey,
151+
});
152+
yield put(updateEnvironmentApiKeySuccess(environmentId, true));
153+
} catch (error) {
154+
yield put({
155+
type: ReduxActionTypes.UPDATE_ENVIRONMENT_API_KEY_FAILURE,
156+
payload: error,
157+
});
158+
console.error("Failed to update environment API key:", error);
159+
}
160+
}
161+
162+
// Extend your existing enterpriseSagas function
38163
export default function* enterpriseSagas() {
39-
yield takeLatest(ReduxActionTypes.FETCH_ENTERPRISE_LICENSE, fetchEnterpriseLicenseSaga);
40-
yield takeLatest(ReduxActionTypes.FETCH_BRANDING_SETTING, fetchBrandingSettingSaga);
164+
yield all([
165+
// Existing sagas
166+
takeLatest(
167+
ReduxActionTypes.FETCH_ENTERPRISE_LICENSE,
168+
fetchEnterpriseLicenseSaga
169+
),
170+
takeLatest(
171+
ReduxActionTypes.FETCH_BRANDING_SETTING,
172+
fetchBrandingSettingSaga
173+
),
174+
175+
// Environment sagas
176+
takeLatest(ReduxActionTypes.FETCH_ENVIRONMENTS, fetchEnvironmentsSaga),
177+
takeLatest(ReduxActionTypes.FETCH_ENVIRONMENT, fetchEnvironmentSaga),
178+
takeLatest(ReduxActionTypes.CREATE_ENVIRONMENT, createEnvironmentSaga),
179+
takeLatest(ReduxActionTypes.UPDATE_ENVIRONMENT, updateEnvironmentSaga),
180+
takeLatest(ReduxActionTypes.DELETE_ENVIRONMENT, deleteEnvironmentSaga),
181+
takeLatest(
182+
ReduxActionTypes.UPDATE_ENVIRONMENT_API_KEY,
183+
updateEnvironmentApiKeySaga
184+
),
185+
]);
41186
}

0 commit comments

Comments
 (0)