@@ -147,43 +147,42 @@ document.addEventListener("DOMContentLoaded", function () {
147
147
}
148
148
} ) ;
149
149
150
- document
151
- . getElementById ( "add-gateway-form" )
152
- . addEventListener ( "submit" , ( e ) => {
153
- e . preventDefault ( ) ;
150
+ document . getElementById ( "add-gateway-form" )
151
+ . addEventListener ( "submit" , async ( e ) => {
152
+ e . preventDefault ( ) ;
154
153
155
- const form = e . target ;
156
- const formData = new FormData ( form ) ;
154
+ const form = e . target ;
155
+ const formData = new FormData ( form ) ;
157
156
158
- const status = document . getElementById ( "status-gateways" ) ;
159
- const loading = document . getElementById ( "add-gateway-loading" ) ;
157
+ const status = document . getElementById ( "status-gateways" ) ;
158
+ const loading = document . getElementById ( "add-gateway-loading" ) ;
160
159
161
- // Show loading and clear previous status
162
- loading . style . display = "block" ;
163
- status . textContent = "" ;
164
- status . classList . remove ( "error-status" ) ;
160
+ // Show loading and clear previous status
161
+ loading . style . display = "block" ;
162
+ status . textContent = "" ;
163
+ status . classList . remove ( "error-status" ) ;
165
164
166
- fetch ( `${ window . ROOT_PATH } /admin/gateways` , {
167
- method : "POST" ,
168
- body : formData ,
169
- } )
170
- . then ( ( response ) => {
171
- if ( ! response . ok ) {
172
- status . textContent = "Connection failed!" ;
165
+ try {
166
+ const response = await fetch ( `${ window . ROOT_PATH } /admin/gateways` , {
167
+ method : "POST" ,
168
+ body : formData ,
169
+ } ) ;
170
+
171
+ let result = await response . json ( ) ;
172
+ if ( ! result . success ) {
173
+ alert ( result . message || "An error occurred" ) ;
174
+ } else {
175
+ window . location . href = `${ window . ROOT_PATH } /admin#gateways` ; // Redirect on success
176
+ }
177
+
178
+ } catch ( error ) {
179
+ console . error ( "Error:" , error ) ;
180
+ status . textContent = error . message || "An error occurred!" ;
173
181
status . classList . add ( "error-status" ) ;
174
- } else {
175
- location . reload ( ) ; // Will exit before hiding spinner
182
+ } finally {
183
+ loading . style . display = "none" ; // Hide loading spinner
176
184
}
177
- } )
178
- . catch ( ( error ) => {
179
- console . error ( "Error:" , error ) ;
180
- status . textContent = "An error occurred!" ;
181
- status . classList . add ( "error-status" ) ;
182
- } )
183
- . finally ( ( ) => {
184
- loading . style . display = "none" ; // Hide loading
185
185
} ) ;
186
- } ) ;
187
186
188
187
189
188
document
@@ -305,7 +304,7 @@ document.addEventListener("DOMContentLoaded", function () {
305
304
if ( ! result . success ) {
306
305
alert ( result . message || "An error occurred" ) ;
307
306
} else {
308
- window . location . href = " /admin#tools" ; // Redirect on success
307
+ window . location . href = ` ${ window . ROOT_PATH } /admin#tools` ; // Redirect on success
309
308
}
310
309
} catch ( error ) {
311
310
console . error ( "Fetch error:" , error ) ;
0 commit comments