From 3a2c961947381fda5ec4539a4a2742d9ed91ca40 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Tue, 24 Mar 2020 10:57:54 +0000 Subject: [PATCH] bugfix: views duplicated --- .../builder/src/builderStore/store/backend.js | 50 ++++++++++++++----- .../builder/src/builderStore/store/index.js | 2 - 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 42817b7375..1fff326d46 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -12,6 +12,7 @@ import { validate, constructHierarchy, templateApi, + isIndex, } from "../../common/core" export const getBackendUiStore = () => { @@ -81,6 +82,20 @@ export const saveBackend = async state => { }, accessLevels: state.accessLevels, }) + + const instances_currentFirst = state.selectedDatabase + ? [ + state.appInstances.find(i => i.id === state.selectedDatabase.id), + ...state.appInstances.filter(i => i.id !== state.selectedDatabase.id), + ] + : state.appInstances + + for (let instance of instances_currentFirst) { + await api.post( + `/_builder/instance/${state.appname}/${instance.id}/api/upgradeData`, + { newHierarchy: state.hierarchy } + ) + } } export const newRecord = (store, useRoot) => () => { @@ -111,14 +126,16 @@ export const selectExistingNode = store => nodeId => { export const newIndex = (store, useRoot) => () => { store.update(state => { - const shadowHierarchy = createShadowHierarchy(state.hierarchy) + state.shadowHierarchy = createShadowHierarchy(state.hierarchy) state.currentNodeIsNew = true state.errors = [] const parent = useRoot - ? state.hierarchy - : getNode(state.hierarchy, state.currentNode.nodeId) + ? state.shadowHierarchy + : getNode(state.shadowHierarchy, state.currentNode.nodeId) - state.currentNode = templateApi(shadowHierarchy).getNewIndexTemplate(parent) + state.currentNode = templateApi(state.shadowHierarchy).getNewIndexTemplate( + parent + ) return state }) } @@ -130,7 +147,10 @@ export const saveCurrentNode = store => () => { if (errors.length > 0) { return state } - const parentNode = getNode(state.hierarchy, state.currentNode.parent().nodeId) + const parentNode = getNode( + state.hierarchy, + state.currentNode.parent().nodeId + ) const existingNode = getNode(state.hierarchy, state.currentNode.nodeId) @@ -138,21 +158,27 @@ export const saveCurrentNode = store => () => { if (existingNode) { // remove existing index = existingNode.parent().children.indexOf(existingNode) - existingNode.parent().children = existingNode.parent().children.filter(node => node.nodeId !== existingNode.nodeId); + if (isIndex(existingNode)) { + parentNode.indexes = parentNode.indexes.filter( + node => node.nodeId !== existingNode.nodeId + ) + } else { + parentNode.children = parentNode.children.filter( + node => node.nodeId !== existingNode.nodeId + ) + } } // should add node into existing hierarchy const cloned = cloneDeep(state.currentNode) templateApi(state.hierarchy).constructNode(parentNode, cloned) - const newIndexOfChild = child => { - if (child === cloned) return index - const currentIndex = parentNode.children.indexOf(child) - return currentIndex >= index ? currentIndex + 1 : currentIndex + if (isIndex(existingNode)) { + parentNode.children = sortBy("name", parentNode.children) + } else { + parentNode.indexes = sortBy("name", parentNode.indexes) } - parentNode.children = sortBy(newIndexOfChild, parentNode.children) - if (!existingNode && state.currentNode.type === "record") { const defaultIndex = templateApi(state.hierarchy).getNewIndexTemplate( cloned.parent() diff --git a/packages/builder/src/builderStore/store/index.js b/packages/builder/src/builderStore/store/index.js index 07db76353d..4b41db5f44 100644 --- a/packages/builder/src/builderStore/store/index.js +++ b/packages/builder/src/builderStore/store/index.js @@ -192,7 +192,6 @@ const useAnalytics = store => () => { }) } - const importAppDefinition = store => appDefinition => { store.update(s => { s.hierarchy = appDefinition.hierarchy @@ -207,7 +206,6 @@ const importAppDefinition = store => appDefinition => { }) } - const createShadowHierarchy = hierarchy => constructHierarchy(JSON.parse(JSON.stringify(hierarchy)))