@@ -4183,48 +4183,70 @@ async function handleGatewayFormSubmit(e) {
4183
4183
}
4184
4184
}
4185
4185
4186
- function handleResourceFormSubmit ( e ) {
4186
+ async function handleResourceFormSubmit ( e ) {
4187
4187
e . preventDefault ( ) ;
4188
4188
const form = e . target ;
4189
4189
const formData = new FormData ( form ) ;
4190
+ const status = safeGetElement ( "status-resources" ) ;
4191
+ const loading = safeGetElement ( "add-gateway-loading" ) ;
4192
+ try {
4193
+ // Validate inputs
4194
+ const name = formData . get ( "name" ) ;
4195
+ const uri = formData . get ( "uri" ) ;
4196
+ const nameValidation = validateInputName ( name , "resource" ) ;
4197
+ const uriValidation = validateInputName ( uri , "resource URI" ) ;
4190
4198
4191
- // Validate inputs
4192
- const name = formData . get ( "name" ) ;
4193
- const uri = formData . get ( "uri" ) ;
4199
+ if ( ! nameValidation . valid ) {
4200
+ showErrorMessage ( nameValidation . error ) ;
4201
+ return ;
4202
+ }
4194
4203
4195
- const nameValidation = validateInputName ( name , "resource" ) ;
4196
- const uriValidation = validateInputName ( uri , "resource URI" ) ;
4204
+ if ( ! uriValidation . valid ) {
4205
+ showErrorMessage ( uriValidation . error ) ;
4206
+ return ;
4207
+ }
4197
4208
4198
- if ( ! nameValidation . valid ) {
4199
- showErrorMessage ( nameValidation . error ) ;
4200
- return ;
4201
- }
4209
+ if ( loading ) {
4210
+ loading . style . display = "block" ;
4211
+ }
4212
+ if ( status ) {
4213
+ status . textContent = "" ;
4214
+ status . classList . remove ( "error-status" ) ;
4215
+ }
4216
+
4217
+ const isInactiveCheckedBool = isInactiveChecked ( "resources" ) ;
4218
+ formData . append ( "is_inactive_checked" , isInactiveCheckedBool ) ;
4202
4219
4203
- if ( ! uriValidation . valid ) {
4204
- showErrorMessage ( uriValidation . error ) ;
4205
- return ;
4206
- }
4220
+ const response = await fetchWithTimeout (
4221
+ `${ window . ROOT_PATH } /admin/resources` ,
4222
+ {
4223
+ method : "POST" ,
4224
+ body : formData ,
4225
+ } ,
4226
+ ) ;
4207
4227
4208
- fetchWithTimeout ( `${ window . ROOT_PATH } /admin/resources` , {
4209
- method : "POST" ,
4210
- body : formData ,
4211
- } )
4212
- . then ( ( response ) => {
4213
- if ( ! response . ok ) {
4214
- const status = safeGetElement ( "status-resources" ) ;
4215
- if ( status ) {
4216
- status . textContent = "Connection failed!" ;
4217
- status . classList . add ( "error-status" ) ;
4218
- }
4219
- throw new Error ( "Network response was not ok" ) ;
4220
- } else {
4221
- location . reload ( ) ;
4228
+ const result = await response . json ( ) ;
4229
+ if ( ! result . success ) {
4230
+ throw new Error ( result . message || "An error occurred" ) ;
4231
+ } else {
4232
+ const redirectUrl = isInactiveCheckedBool
4233
+ ? `${ window . ROOT_PATH } /admin?include_inactive=true#resources`
4234
+ : `${ window . ROOT_PATH } /admin#resources` ;
4235
+ window . location . href = redirectUrl ;
4222
4236
}
4223
- } )
4224
- . catch ( ( error ) => {
4237
+ } catch ( error ) {
4225
4238
console . error ( "Error:" , error ) ;
4226
- showErrorMessage ( "Failed to create resource" ) ;
4227
- } ) ;
4239
+ if ( status ) {
4240
+ status . textContent = error . message || "An error occurred!" ;
4241
+ status . classList . add ( "error-status" ) ;
4242
+ }
4243
+ showErrorMessage ( error . message ) ;
4244
+ } finally {
4245
+ // location.reload();
4246
+ if ( loading ) {
4247
+ loading . style . display = "none" ;
4248
+ }
4249
+ }
4228
4250
}
4229
4251
4230
4252
async function handleToolFormSubmit ( event ) {
0 commit comments