|
|
|
@ -1,9 +1,12 @@ |
|
|
|
import { writable } from "svelte/store"; |
|
|
|
import api from "../api" |
|
|
|
import { |
|
|
|
filter, |
|
|
|
cloneDeep, |
|
|
|
sortBy, |
|
|
|
find |
|
|
|
map, |
|
|
|
find, |
|
|
|
remove |
|
|
|
} from "lodash/fp" |
|
|
|
import { hierarchy as hierarchyFunctions } from "../../../../core/src" |
|
|
|
import { |
|
|
|
@ -13,10 +16,47 @@ import { |
|
|
|
constructHierarchy, |
|
|
|
templateApi, |
|
|
|
} from "../../common/core" |
|
|
|
import { store } from "../index"; |
|
|
|
|
|
|
|
export const createShadowHierarchy = hierarchy => |
|
|
|
constructHierarchy(JSON.parse(JSON.stringify(hierarchy))) |
|
|
|
export const getBackendUiStore = () => { |
|
|
|
const INITIAL_BACKEND_UI_STATE = { |
|
|
|
leftNavItem: "DATABASE", |
|
|
|
selectedView: { |
|
|
|
records: [], |
|
|
|
name: "" |
|
|
|
}, |
|
|
|
selectedRecord: {}, |
|
|
|
selectedDatabase: {}, |
|
|
|
selectedModel: {}, |
|
|
|
} |
|
|
|
|
|
|
|
const store = writable(INITIAL_BACKEND_UI_STATE) |
|
|
|
|
|
|
|
store.actions = { |
|
|
|
navigate: name => store.update(state => ({ ...state, leftNavItem: name })), |
|
|
|
database: { |
|
|
|
select: db => store.update(state => ({ ...state, selectedDatabase: db })), |
|
|
|
}, |
|
|
|
records: { |
|
|
|
delete: record => store.update(state => { |
|
|
|
state.selectedView.records = remove(state.selectedView.records, { key: record.key }); |
|
|
|
return state |
|
|
|
}), |
|
|
|
}, |
|
|
|
modals: { |
|
|
|
show: modal => store.update(state => ({ ...state, visibleModal: modal })), |
|
|
|
hide: () => store.update(state => ({ ...state, visibleModal: null })) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return store |
|
|
|
}; |
|
|
|
|
|
|
|
// Store Actions
|
|
|
|
export const createShadowHierarchy = hierarchy => { |
|
|
|
const hi = constructHierarchy(JSON.parse(JSON.stringify(hierarchy))) |
|
|
|
console.log(hi) |
|
|
|
return hi |
|
|
|
} |
|
|
|
|
|
|
|
export const saveBackend = async state => { |
|
|
|
await api.post(`/_builder/api/${state.appname}/backend`, { |
|
|
|
@ -30,58 +70,57 @@ export const saveBackend = async state => { |
|
|
|
} |
|
|
|
|
|
|
|
export const newRecord = (store, useRoot) => () => { |
|
|
|
store.update(s => { |
|
|
|
s.currentNodeIsNew = true |
|
|
|
const shadowHierarchy = createShadowHierarchy(s.hierarchy) |
|
|
|
store.update(state => { |
|
|
|
state.currentNodeIsNew = true |
|
|
|
const shadowHierarchy = createShadowHierarchy(state.hierarchy) |
|
|
|
const parent = useRoot |
|
|
|
? shadowHierarchy |
|
|
|
: getNode(shadowHierarchy, s.currentNode.nodeId) |
|
|
|
s.errors = [] |
|
|
|
s.currentNode = templateApi(shadowHierarchy).getNewRecordTemplate( |
|
|
|
: getNode(shadowHierarchy, state.currentNode.nodeId) |
|
|
|
state.errors = [] |
|
|
|
state.currentNode = templateApi(shadowHierarchy).getNewRecordTemplate( |
|
|
|
parent, |
|
|
|
"", |
|
|
|
true |
|
|
|
) |
|
|
|
return s |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
export const selectExistingNode = store => nodeId => { |
|
|
|
store.update(s => { |
|
|
|
const shadowHierarchy = createShadowHierarchy(s.hierarchy) |
|
|
|
s.currentNode = getNode(shadowHierarchy, nodeId) |
|
|
|
s.currentNodeIsNew = false |
|
|
|
s.errors = [] |
|
|
|
return s |
|
|
|
store.update(state => { |
|
|
|
const shadowHierarchy = createShadowHierarchy(state.hierarchy) |
|
|
|
state.currentNode = getNode(shadowHierarchy, nodeId) |
|
|
|
state.currentNodeIsNew = false |
|
|
|
state.errors = [] |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
export const newIndex = (store, useRoot) => () => { |
|
|
|
store.update(s => { |
|
|
|
s.currentNodeIsNew = true |
|
|
|
s.errors = [] |
|
|
|
const shadowHierarchy = createShadowHierarchy(s.hierarchy) |
|
|
|
store.update(state => { |
|
|
|
state.currentNodeIsNew = true |
|
|
|
state.errors = [] |
|
|
|
const shadowHierarchy = createShadowHierarchy(state.hierarchy) |
|
|
|
const parent = useRoot |
|
|
|
? shadowHierarchy |
|
|
|
: getNode(shadowHierarchy, s.currentNode.nodeId) |
|
|
|
: getNode(shadowHierarchy, state.currentNode.nodeId) |
|
|
|
|
|
|
|
s.currentNode = templateApi(shadowHierarchy).getNewIndexTemplate(parent) |
|
|
|
return s |
|
|
|
state.currentNode = templateApi(shadowHierarchy).getNewIndexTemplate(parent) |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// TODO: ONLY SEEMS TO BE CALLED BY THE BACKEND
|
|
|
|
export const saveCurrentNode = store => () => { |
|
|
|
store.update(s => { |
|
|
|
const errors = validate.node(s.currentNode) |
|
|
|
s.errors = errors |
|
|
|
if (errors.length > 0) { |
|
|
|
return s |
|
|
|
store.update(state => { |
|
|
|
const errors = validate.node(state.currentNode) |
|
|
|
state.errors = errors |
|
|
|
if (errorstate.length > 0) { |
|
|
|
return state |
|
|
|
} |
|
|
|
|
|
|
|
const parentNode = getNode(s.hierarchy, s.currentNode.parent().nodeId) |
|
|
|
const parentNode = getNode(state.hierarchy, state.currentNode.parent().nodeId) |
|
|
|
|
|
|
|
const existingNode = getNode(s.hierarchy, s.currentNode.nodeId) |
|
|
|
const existingNode = getNode(state.hierarchy, state.currentNode.nodeId) |
|
|
|
|
|
|
|
let index = parentNode.children.length |
|
|
|
if (existingNode) { |
|
|
|
@ -93,8 +132,8 @@ export const saveCurrentNode = store => () => { |
|
|
|
} |
|
|
|
|
|
|
|
// should add node into existing hierarchy
|
|
|
|
const cloned = cloneDeep(s.currentNode) |
|
|
|
templateApi(s.hierarchy).constructNode(parentNode, cloned) |
|
|
|
const cloned = cloneDeep(state.currentNode) |
|
|
|
templateApi(state.hierarchy).constructNode(parentNode, cloned) |
|
|
|
|
|
|
|
const newIndexOfChild = child => { |
|
|
|
if (child === cloned) return index |
|
|
|
@ -104,29 +143,29 @@ export const saveCurrentNode = store => () => { |
|
|
|
|
|
|
|
parentNode.children = pipe(parentNode.children, [sortBy(newIndexOfChild)]) |
|
|
|
|
|
|
|
if (!existingNode && s.currentNode.type === "record") { |
|
|
|
const defaultIndex = templateApi(s.hierarchy).getNewIndexTemplate( |
|
|
|
if (!existingNode && state.currentNode.type === "record") { |
|
|
|
const defaultIndex = templateApi(state.hierarchy).getNewIndexTemplate( |
|
|
|
cloned.parent() |
|
|
|
) |
|
|
|
defaultIndex.name = `all_${cloned.collectionName}` |
|
|
|
defaultIndex.allowedRecordNodeIds = [cloned.nodeId] |
|
|
|
} |
|
|
|
|
|
|
|
s.currentNodeIsNew = false |
|
|
|
state.currentNodeIsNew = false |
|
|
|
|
|
|
|
saveBackend(s) |
|
|
|
saveBackend(state) |
|
|
|
|
|
|
|
return s |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
export const deleteCurrentNode = store => () => { |
|
|
|
store.update(s => { |
|
|
|
const nodeToDelete = getNode(s.hierarchy, s.currentNode.nodeId) |
|
|
|
s.currentNode = hierarchyFunctions.isRoot(nodeToDelete.parent()) |
|
|
|
? find(n => n != s.currentNode)(s.hierarchy.children) |
|
|
|
store.update(state => { |
|
|
|
const nodeToDelete = getNode(state.hierarchy, state.currentNode.nodeId) |
|
|
|
state.currentNode = hierarchyFunctionstate.isRoot(nodeToDelete.parent()) |
|
|
|
? find(n => n != state.currentNode)(state.hierarchy.children) |
|
|
|
: nodeToDelete.parent() |
|
|
|
if (hierarchyFunctions.isRecord(nodeToDelete)) { |
|
|
|
if (hierarchyFunctionstate.isRecord(nodeToDelete)) { |
|
|
|
nodeToDelete.parent().children = filter( |
|
|
|
c => c.nodeId !== nodeToDelete.nodeId |
|
|
|
)(nodeToDelete.parent().children) |
|
|
|
@ -135,9 +174,9 @@ export const deleteCurrentNode = store => () => { |
|
|
|
c => c.nodeId !== nodeToDelete.nodeId |
|
|
|
)(nodeToDelete.parent().indexes) |
|
|
|
} |
|
|
|
s.errors = [] |
|
|
|
saveBackend(s) |
|
|
|
return s |
|
|
|
state.errors = [] |
|
|
|
saveBackend(state) |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
@ -160,4 +199,41 @@ export const deleteField = databaseStore => field => { |
|
|
|
|
|
|
|
return db |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
const incrementAccessLevelsVersion = state => |
|
|
|
(state.accessLevelstate.version = (state.accessLevelstate.version || 0) + 1) |
|
|
|
|
|
|
|
export const saveLevel = store => (newLevel, isNew, oldLevel = null) => { |
|
|
|
store.update(state => { |
|
|
|
const levels = state.accessLevelstate.levels |
|
|
|
|
|
|
|
const existingLevel = isNew |
|
|
|
? null |
|
|
|
: find(a => a.name === oldLevel.name)(levels) |
|
|
|
|
|
|
|
if (existingLevel) { |
|
|
|
state.accessLevelstate.levels = pipe(levels, [ |
|
|
|
map(a => (a === existingLevel ? newLevel : a)), |
|
|
|
]) |
|
|
|
} else { |
|
|
|
state.accessLevelstate.levelstate.push(newLevel) |
|
|
|
} |
|
|
|
|
|
|
|
incrementAccessLevelsVersion(s) |
|
|
|
|
|
|
|
saveBackend(state) |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
export const deleteLevel = store => level => { |
|
|
|
store.update(state => { |
|
|
|
state.accessLevelstate.levels = filter(t => t.name !== level.name)( |
|
|
|
state.accessLevelstate.levels |
|
|
|
) |
|
|
|
incrementAccessLevelsVersion(s) |
|
|
|
saveBackend(state) |
|
|
|
return state |
|
|
|
}) |
|
|
|
} |