1
- import { call , put , takeLatest } from ' redux-saga/effects' ;
1
+ import { call , put , takeLatest , all } from " redux-saga/effects" ;
2
2
import { ReduxAction , ReduxActionTypes } from "constants/reduxActionConstants" ;
3
3
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" ;
6
32
7
- function * fetchEnterpriseLicenseSaga ( ) : Generator < any , void , EnterpriseLicenseResponse > {
33
+ function * fetchEnterpriseLicenseSaga ( ) : Generator <
34
+ any ,
35
+ void ,
36
+ EnterpriseLicenseResponse
37
+ > {
8
38
try {
9
39
// Type the result from the API call
10
40
const data : EnterpriseLicenseResponse = yield call ( getEnterpriseLicense ) ;
11
41
yield put ( setEnterpriseLicense ( data ) ) ;
12
42
} catch ( error ) {
13
- console . error ( ' Failed to fetch enterprise license:' , error ) ;
43
+ console . error ( " Failed to fetch enterprise license:" , error ) ;
14
44
}
15
45
}
16
46
17
- function * fetchBrandingSettingSaga ( action : ReduxAction < FetchBrandingSettingPayload > ) {
47
+ function * fetchBrandingSettingSaga (
48
+ action : ReduxAction < FetchBrandingSettingPayload >
49
+ ) {
18
50
try {
19
- const response : BrandingSettingResponse = yield getBranding ( action . payload . orgId ) ;
51
+ const response : BrandingSettingResponse = yield getBranding (
52
+ action . payload . orgId
53
+ ) ;
20
54
if ( response && response . id ) {
21
55
if ( action . payload . orgId ) {
22
56
yield put ( {
@@ -31,11 +65,122 @@ function* fetchBrandingSettingSaga(action: ReduxAction<FetchBrandingSettingPaylo
31
65
} ) ;
32
66
}
33
67
} catch ( error ) {
34
- console . error ( ' Failed to fetch branding setting:' , error ) ;
68
+ console . error ( " Failed to fetch branding setting:" , error ) ;
35
69
}
36
70
}
37
71
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
38
163
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
+ ] ) ;
41
186
}
0 commit comments