@@ -334,35 +334,36 @@ export function deleteByPath(item) {
334
334
// update the id field of a property
335
335
export function renameIdByPath ( item , newName ) {
336
336
return function ( dispatch , getState ) {
337
- let schema = getState ( )
338
- . schemaWizard . getIn ( [ "current" , "schema" ] )
339
- . toJS ( ) ;
340
-
341
- let uiSchema = getState ( )
342
- . schemaWizard . getIn ( [ "current" , "uiSchema" ] )
343
- . toJS ( ) ;
344
-
345
337
const path = item . path ;
346
338
const uiPath = item . uiPath ;
347
339
348
- // ********* schema **********
349
-
350
340
let itemToDelete = path . pop ( ) ;
351
341
// if the last item is items then pop again since it is an array, in order to fetch the proper id
352
342
itemToDelete = itemToDelete === "items" ? path . pop ( ) : itemToDelete ;
353
343
354
- if ( newName === itemToDelete || newName === "" ) return ;
355
-
356
- // shallow copy schema object in order to navigate through the object
357
- // but the changes will reflect to the original one --> schema
358
- let tempSchema = Object . assign ( { } , schema ) ;
344
+ const uiItemToDelete = uiPath . pop ( ) ;
359
345
360
- // schema update
361
- for ( let p in path ) {
362
- tempSchema = tempSchema [ path [ p ] ] ;
346
+ // check if the new id is empty or exact same with the current id
347
+ if ( newName === itemToDelete || newName === "" ) {
348
+ cogoToast . warn ( "Make sure that the new id is different and not empty" , {
349
+ position : "top-center" ,
350
+ bar : { size : "0" } ,
351
+ hideAfter : 3
352
+ } ) ;
353
+ return ;
363
354
}
364
355
365
- let keys = Object . keys ( tempSchema ) ;
356
+ // navigate to the correct path
357
+ let schema = getState ( )
358
+ . schemaWizard . getIn ( [ "current" , "schema" , ...path ] )
359
+ . toJS ( ) ;
360
+ let uiSchema = getState ( )
361
+ . schemaWizard . getIn ( [ "current" , "uiSchema" , ...uiPath ] )
362
+ . toJS ( ) ;
363
+
364
+ // ********* schema **********
365
+ let keys = Object . keys ( schema ) ;
366
+ // make sure that the new name is unique among sibling widgets
366
367
if ( keys . includes ( newName ) ) {
367
368
cogoToast . error ( "The id should be unique, this name already exists" , {
368
369
position : "top-center" ,
@@ -372,48 +373,30 @@ export function renameIdByPath(item, newName) {
372
373
return ;
373
374
}
374
375
375
- tempSchema [ newName ] = tempSchema [ itemToDelete ] ;
376
-
377
- delete tempSchema [ itemToDelete ] ;
376
+ // create new obj with the information and then delete the old one
377
+ schema [ newName ] = schema [ itemToDelete ] ;
378
+ delete schema [ itemToDelete ] ;
378
379
379
380
// ********* uiSchema **********
381
+ if ( ! uiSchema [ "ui:order" ] ) {
382
+ uiSchema [ "ui:order" ] = [ ] ;
383
+ }
384
+ // update the uiOrder array
385
+ let pos = uiSchema [ "ui:order" ] . indexOf ( uiItemToDelete ) ;
386
+ if ( pos > - 1 ) {
387
+ uiSchema [ "ui:order" ] [ pos ] = newName ;
388
+ }
380
389
381
- if ( uiPath . length === 1 ) {
382
- // remove from the uiSchema
383
- uiSchema [ newName ] = uiSchema [ uiPath [ 0 ] ] ;
384
- // update the uiOrder array
385
- let pos = uiSchema [ "ui:order" ] . indexOf ( uiPath [ 0 ] ) ;
386
-
387
- if ( pos > - 1 ) {
388
- uiSchema [ "ui:order" ] [ pos ] = newName ;
389
- }
390
- delete uiSchema [ uiPath [ 0 ] ] ;
391
- } else {
392
- let tempUiSchema = Object . assign ( { } , uiSchema ) ;
393
- const uiItemToDelete = uiPath . pop ( ) ;
394
-
395
- for ( let i in uiPath ) {
396
- tempUiSchema = tempUiSchema [ uiPath [ i ] ] ;
397
- }
398
-
399
- if ( ! tempUiSchema [ "ui:order" ] ) {
400
- tempUiSchema [ "ui:order" ] = [ ] ;
401
- }
402
- // update the uiOrder array
403
- let pos = tempUiSchema [ "ui:order" ] . indexOf ( uiItemToDelete ) ;
404
- if ( pos > - 1 ) {
405
- tempUiSchema [ "ui:order" ] [ pos ] = newName ;
406
- }
407
-
408
- if ( tempUiSchema [ uiItemToDelete ] ) {
409
- tempUiSchema [ newName ] = tempUiSchema [ uiItemToDelete ] ;
410
- delete tempUiSchema [ uiItemToDelete ] ;
411
- }
412
- // remove from the uiSchema
390
+ if ( uiSchema [ uiItemToDelete ] ) {
391
+ uiSchema [ newName ] = uiSchema [ uiItemToDelete ] ;
413
392
}
393
+ // remove from the uiSchema
394
+ delete uiSchema [ uiItemToDelete ] ;
414
395
415
396
// ********* update changes **********
416
- dispatch ( updateByPath ( { schema : [ ] , uiSchema : [ ] } , { schema, uiSchema } ) ) ;
397
+ dispatch (
398
+ updateByPath ( { schema : path , uiSchema : uiPath } , { schema, uiSchema } )
399
+ ) ;
417
400
dispatch ( enableCreateMode ( ) ) ;
418
401
} ;
419
402
}
0 commit comments