Skip to content

Commit d510133

Browse files
committed
ui: refactor form builders update id function to use immutable built-ins
* closes #2008 Signed-off-by: papadopan <[email protected]>
1 parent 90bfa7f commit d510133

File tree

1 file changed

+38
-55
lines changed

1 file changed

+38
-55
lines changed

ui/cap-react/src/actions/schemaWizard.js

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -334,35 +334,36 @@ export function deleteByPath(item) {
334334
// update the id field of a property
335335
export function renameIdByPath(item, newName) {
336336
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-
345337
const path = item.path;
346338
const uiPath = item.uiPath;
347339

348-
// ********* schema **********
349-
350340
let itemToDelete = path.pop();
351341
// if the last item is items then pop again since it is an array, in order to fetch the proper id
352342
itemToDelete = itemToDelete === "items" ? path.pop() : itemToDelete;
353343

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();
359345

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;
363354
}
364355

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
366367
if (keys.includes(newName)) {
367368
cogoToast.error("The id should be unique, this name already exists", {
368369
position: "top-center",
@@ -372,48 +373,30 @@ export function renameIdByPath(item, newName) {
372373
return;
373374
}
374375

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];
378379

379380
// ********* 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+
}
380389

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];
413392
}
393+
// remove from the uiSchema
394+
delete uiSchema[uiItemToDelete];
414395

415396
// ********* update changes **********
416-
dispatch(updateByPath({ schema: [], uiSchema: [] }, { schema, uiSchema }));
397+
dispatch(
398+
updateByPath({ schema: path, uiSchema: uiPath }, { schema, uiSchema })
399+
);
417400
dispatch(enableCreateMode());
418401
};
419402
}

0 commit comments

Comments
 (0)