2019-10-03 07:12:13 +02:00
|
|
|
import {
|
|
|
|
isPlainObject, isArray, cloneDeep
|
|
|
|
} from "lodash/fp";
|
|
|
|
import {
|
2020-01-18 00:06:42 +01:00
|
|
|
getExactComponent
|
2019-10-03 07:12:13 +02:00
|
|
|
} from "./searchComponents";
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
export const rename = (pages, screens, oldname, newname) => {
|
2019-10-03 07:12:13 +02:00
|
|
|
|
|
|
|
pages = cloneDeep(pages);
|
2020-01-18 00:06:42 +01:00
|
|
|
screens = cloneDeep(screens);
|
|
|
|
const changedScreens = [];
|
2019-10-03 07:12:13 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
const existingWithNewName = getExactComponent(screens, newname);
|
2019-10-03 07:12:13 +02:00
|
|
|
if(existingWithNewName) return {
|
2020-01-18 00:06:42 +01:00
|
|
|
components: screens, pages, error: "Component by that name already exists"
|
2019-10-03 07:12:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const traverseProps = (props) => {
|
|
|
|
let hasEdited = false;
|
|
|
|
if(props._component && props._component === oldname) {
|
|
|
|
props._component = newname;
|
|
|
|
hasEdited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(let propName in props) {
|
|
|
|
const prop = props[propName];
|
|
|
|
if(isPlainObject(prop) && prop._component) {
|
|
|
|
hasEdited = traverseProps(prop) || hasEdited;
|
|
|
|
}
|
|
|
|
if(isArray(prop)) {
|
|
|
|
for(let element of prop) {
|
|
|
|
hasEdited = traverseProps(element) || hasEdited;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasEdited;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
for(let screen of screens) {
|
2019-10-03 07:12:13 +02:00
|
|
|
|
|
|
|
let hasEdited = false;
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
if(screen.name === oldname) {
|
|
|
|
screen.name = newname;
|
2019-10-03 07:12:13 +02:00
|
|
|
hasEdited = true;
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
if(screen.props._component === oldname) {
|
|
|
|
screen.props._component = newname;
|
2019-10-03 07:12:13 +02:00
|
|
|
hasEdited = true;
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
hasEdited = traverseProps(screen.props) || hasEdited;
|
2019-10-03 07:12:13 +02:00
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
if(hasEdited && screen.name !== newname)
|
|
|
|
changedScreens.push(screen.name);
|
2019-10-03 07:12:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(let pageName in pages) {
|
|
|
|
const page = pages[pageName];
|
|
|
|
if(page.appBody === oldname) {
|
|
|
|
page.appBody = newname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:06:42 +01:00
|
|
|
return {screens, pages, changedScreens};
|
2019-10-03 07:12:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|