diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 59893a0eff..c4c1e19f99 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -34,7 +34,7 @@ export const getBackendUiStore = () => { store.actions = { navigate: name => store.update(state => ({ ...state, leftNavItem: name })), database: { - select: db => store.update(state => ({ ...state, selectedDatabase: db })), + select: state => store.update(state => ({ ...state, selectedDatabase: state })), }, records: { delete: record => store.update(state => { @@ -96,8 +96,7 @@ export const newRecord = (store, useRoot) => () => { export const selectExistingNode = store => nodeId => { store.update(state => { - const shadowHierarchy = createShadowHierarchy(state.hierarchy) - state.currentNode = getNode(shadowHierarchy, nodeId) + state.currentNode = getNode(state.hierarchy, nodeId) state.currentNodeIsNew = false state.errors = [] return state @@ -179,42 +178,32 @@ export const deleteCurrentNode = store => () => { node => node.nodeId === nodeToDelete.nodeId ) - // if (hierarchyFunctions.isRecord(nodeToDelete)) { - // nodeToDelete.parent().children = filter( - // c => c.nodeId !== nodeToDelete.nodeId - // )(nodeToDelete.parent().children) - // } else { - // nodeToDelete.parent().indexes = remove( - // nodeToDelete.parent().indexes, - // node => node.nodeId === nodeToDelete.nodeId - // ) - // } state.errors = [] saveBackend(state) return state }) } -export const saveField = databaseStore => field => { - databaseStore.update(db => { - db.currentNode.fields = filter(f => f.name !== field.name)( - db.currentNode.fields - ) +export const saveField = store => field => { + store.update(state => { + state.currentNode.fields = state.currentNode.fields.filter(f => f.name !== field.name) - templateApi(db.hierarchy).addField(db.currentNode, field) - return db + templateApi(state.hierarchy).addField(state.currentNode, field) + return state }) } -export const deleteField = databaseStore => field => { - databaseStore.update(db => { - db.currentNode.fields = db.currentNode.fields.filter(f => f.name !== field.name) - return db +export const deleteField = store => field => { + store.update(state => { + state.currentNode.fields = state.currentNode.fields.filter(f => f.name !== field.name) + return state }) } -const incrementAccessLevelsVersion = state => - (state.accessLevels.version = (state.accessLevels.version || 0) + 1) +const incrementAccessLevelsVersion = state => { + state.accessLevels.version = state.accessLevels.version ? state.accessLevels.version + 1 : 1 + return state +} export const saveLevel = store => (newLevel, isNew, oldLevel = null) => { store.update(state => { @@ -246,4 +235,53 @@ export const deleteLevel = store => level => { saveBackend(state) return state }) +} + +export const saveAction = store => (newAction, isNew, oldAction = null) => { + store.update(s => { + const existingAction = isNew + ? null + : find(a => a.name === oldAction.name)(s.actions) + + if (existingAction) { + s.actions = s.actions.map(action => action === existingAction ? newAction : action) + } else { + s.actions.push(newAction) + } + saveBackend(s) + return s + }) +} + +export const deleteAction = store => action => { + store.update(state => { + state.actions = state.actions.filter(a => a.name !== action.name); + saveBackend(state); + return state; + }) +} + +export const saveTrigger = store => (newTrigger, isNew, oldTrigger = null) => { + store.update(s => { + const existingTrigger = isNew + ? null + : find(a => a.name === oldTrigger.name)(s.triggers) + + if (existingTrigger) { + s.triggers = pipe(s.triggers, [ + map(a => (a === existingTrigger ? newTrigger : a)), + ]) + } else { + s.triggers.push(newTrigger) + } + saveBackend(s) + return s + }) +} + +export const deleteTrigger = store => trigger => { + store.update(s => { + s.triggers = filter(t => t.name !== trigger.name)(s.triggers) + return s + }) } \ No newline at end of file diff --git a/packages/builder/src/builderStore/store/index.js b/packages/builder/src/builderStore/store/index.js index 52f824debc..bf41abb668 100644 --- a/packages/builder/src/builderStore/store/index.js +++ b/packages/builder/src/builderStore/store/index.js @@ -1,10 +1,8 @@ import { filter, cloneDeep, - map, last, concat, - find, isEmpty, values, } from "lodash/fp" @@ -76,12 +74,12 @@ export const getStore = () => { store.saveLevel = backendStoreActions.saveLevel(store) store.deleteLevel = backendStoreActions.deleteLevel(store) store.createDatabaseForApp = backendStoreActions.createDatabaseForApp(store) + store.saveAction = backendStoreActions.saveAction(store) + store.deleteAction = backendStoreActions.deleteAction(store) + store.saveTrigger = backendStoreActions.saveTrigger(store) + store.deleteTrigger = backendStoreActions.deleteTrigger(store) store.importAppDefinition = importAppDefinition(store) - store.saveAction = saveAction(store) - store.deleteAction = deleteAction(store) - store.saveTrigger = saveTrigger(store) - store.deleteTrigger = deleteTrigger(store) store.saveScreen = saveScreen(store) store.addComponentLibrary = addComponentLibrary(store) store.renameScreen = renameScreen(store) @@ -154,10 +152,6 @@ const initialise = (store, initial) => async () => { } initial.appname = appname initial.pages = pkg.pages - initial.currentInstanceId = - pkg.application.instances && pkg.application.instances.length > 0 - ? pkg.application.instances[0].id - : "" initial.hasAppPackage = true initial.hierarchy = pkg.appDefinition.hierarchy initial.accessLevels = pkg.accessLevels @@ -174,8 +168,9 @@ const initialise = (store, initial) => async () => { if (!!initial.hierarchy && !isEmpty(initial.hierarchy)) { initial.hierarchy = constructHierarchy(initial.hierarchy) const shadowHierarchy = createShadowHierarchy(initial.hierarchy) - if (initial.currentNode !== null) + if (initial.currentNode !== null) { initial.currentNode = getNode(shadowHierarchy, initial.currentNode.nodeId) + } } store.set(initial) @@ -191,7 +186,7 @@ const showSettings = store => () => { const useAnalytics = store => () => { store.update(state => { - state.useAnalytics = !s.useAnalytics + state.useAnalytics = !state.useAnalytics return state }) } @@ -211,56 +206,6 @@ const importAppDefinition = store => appDefinition => { }) } -const saveAction = store => (newAction, isNew, oldAction = null) => { - store.update(s => { - const existingAction = isNew - ? null - : find(a => a.name === oldAction.name)(s.actions) - - if (existingAction) { - s.actions = pipe(s.actions, [ - map(a => (a === existingAction ? newAction : a)), - ]) - } else { - s.actions.push(newAction) - } - saveBackend(s) - return s - }) -} - -const deleteAction = store => action => { - store.update(state => { - state.actions = state.actions.filter(a => a.name !== action.name); - saveBackend(state); - return state; - }) -} - -const saveTrigger = store => (newTrigger, isNew, oldTrigger = null) => { - store.update(s => { - const existingTrigger = isNew - ? null - : find(a => a.name === oldTrigger.name)(s.triggers) - - if (existingTrigger) { - s.triggers = pipe(s.triggers, [ - map(a => (a === existingTrigger ? newTrigger : a)), - ]) - } else { - s.triggers.push(newTrigger) - } - saveBackend(s) - return s - }) -} - -const deleteTrigger = store => trigger => { - store.update(s => { - s.triggers = filter(t => t.name !== trigger.name)(s.triggers) - return s - }) -} const createShadowHierarchy = hierarchy => constructHierarchy(JSON.parse(JSON.stringify(hierarchy))) diff --git a/packages/builder/src/common/Table.svelte b/packages/builder/src/common/Table.svelte deleted file mode 100644 index 23f05a13fa..0000000000 --- a/packages/builder/src/common/Table.svelte +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - {#each headers as header} - - {/each} - - - - {#each data as row} - - - {#each headers as header} - - {/each} - - {/each} - -
Edit{header}
-
- -
-
    -
  • -
    View
    -
  • -
  • -
    Edit
    -
  • -
  • -
    (deleteRecordModal = true)}>Delete
    -
  • -
  • -
    Duplicate
    -
  • -
-
-
-
{row[header]}
diff --git a/packages/builder/src/common/ValuesList.svelte b/packages/builder/src/common/ValuesList.svelte index 96e7dcac87..1d1e5ce33c 100644 --- a/packages/builder/src/common/ValuesList.svelte +++ b/packages/builder/src/common/ValuesList.svelte @@ -25,6 +25,6 @@ diff --git a/packages/builder/src/database/DatabaseRoot.svelte b/packages/builder/src/database/DatabaseRoot.svelte index 6f0f750675..1439d4da46 100644 --- a/packages/builder/src/database/DatabaseRoot.svelte +++ b/packages/builder/src/database/DatabaseRoot.svelte @@ -31,6 +31,7 @@ $: viewOpen = $backendUiStore.visibleModal === "VIEW" $: databaseOpen = $backendUiStore.visibleModal === "DATABASE" $: deleteRecordOpen = $backendUiStore.visibleModal === "DELETE_RECORD" + $: breadcrumbs = $store.currentNode @@ -47,7 +48,7 @@ {/if} {#if deleteRecordOpen} - + {/if} @@ -57,17 +58,19 @@
{#if $backendUiStore.selectedDatabase.name} - {$backendUiStore.selectedDatabase.name} / {$store.currentNode} + {$backendUiStore.selectedDatabase.name} {breadcrumbs} {/if}
- { - selectedRecord = null - backendUiStore.actions.modals.show("RECORD") - }}> - Create new record - + {#if $backendUiStore.selectedDatabase.id} + { + selectedRecord = null + backendUiStore.actions.modals.show("RECORD") + }}> + Create new record + + {/if}
diff --git a/packages/builder/src/database/IndexView.svelte b/packages/builder/src/database/IndexView.svelte index d3af05a9a8..b60c4b4cb4 100644 --- a/packages/builder/src/database/IndexView.svelte +++ b/packages/builder/src/database/IndexView.svelte @@ -27,9 +27,9 @@ hierarchyFunctions.getFlattenedHierarchy, filter(hierarchyFunctions.isDecendant(index.parent())), filter(hierarchyFunctions.isRecord), - map(n => ({ - node: n, - isallowed: some(id => n.nodeId === id)(index.allowedRecordNodeIds), + map(node => ({ + node, + isallowed: index.allowedRecordNodeIds.some(id => node.nodeId === id), })), ] ) @@ -37,19 +37,17 @@ const toggleAllowedRecord = record => { if (record.isallowed) { - index.allowedRecordNodeIds = filter(id => id !== record.node.nodeId)( - index.allowedRecordNodeIds - ) + index.allowedRecordNodeIds = index.allowedRecordNodeIds.filter(id => id !== record.node.nodeId) } else { index.allowedRecordNodeIds.push(record.node.nodeId) } } -

- - Create / Edit View -

+ + +

Create / Edit View

+

Settings

@@ -70,10 +68,11 @@
{#each indexableRecords as rec} toggleAllowedRecord(rec)} /> - {rec.node.name} + {rec.node.name} {/each} @@ -118,4 +117,17 @@ .highlighted { opacity: 1; } + + .checkbox-model-label { + text-transform: capitalize; + } + + h3 { + margin: 0 0 0 10px; + } + + heading { + display: flex; + align-items: center; + } diff --git a/packages/builder/src/database/ModelDataTable/ModelDataTable.svelte b/packages/builder/src/database/ModelDataTable/ModelDataTable.svelte index 7a2080147f..e4996e9883 100644 --- a/packages/builder/src/database/ModelDataTable/ModelDataTable.svelte +++ b/packages/builder/src/database/ModelDataTable/ModelDataTable.svelte @@ -10,7 +10,7 @@ export let selectRecord - const ITEMS_PER_PAGE = 2 + const ITEMS_PER_PAGE = 10 let selectedView = "" let modalOpen = false @@ -21,12 +21,13 @@ $: views = $store.hierarchy.indexes $: currentAppInfo = { appname: $store.appname, - instanceId: $store.currentInstanceId, + instanceId: $backendUiStore.selectedDatabase.id } $: data = $backendUiStore.selectedView.records.slice( currentPage * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE + ITEMS_PER_PAGE ) + $: showTable = currentAppInfo.instanceId && views.length > 0 const getSchema = getIndexSchema($store.hierarchy) @@ -42,13 +43,13 @@ } onMount(async () => { - if (views.length > 0) { + if (showTable) { await fetchRecordsForView(views[0].name, currentAppInfo) } }) -{#if views.length > 0} +{#if showTable}

{$backendUiStore.selectedDatabase.name || ""}

@@ -80,6 +81,14 @@
    +
  • +
    { + // fetch the child records for that particular row + }}> + View +
    +
  • { selectRecord(row) @@ -98,8 +107,8 @@
  • { - console.log("DUPLICATION") + on:click={async () => { + const response = await api.saveRecord(row) }}> Duplicate
    @@ -118,7 +127,7 @@
{:else} - Please create a model to get started. + Please select a database. {/if} diff --git a/packages/builder/src/database/ModelDataTable/api.js b/packages/builder/src/database/ModelDataTable/api.js index 5fb3568324..1318eab71f 100644 --- a/packages/builder/src/database/ModelDataTable/api.js +++ b/packages/builder/src/database/ModelDataTable/api.js @@ -21,10 +21,7 @@ if (record.collectionName) { const collectionKey = `/${record.collectionName}` recordBase = getNewRecord(recordBase, collectionKey) - // overwrite the new record template values - for (let key in recordBase) { - if (record[key]) recordBase[key] = record[key] - } + recordBase = overwritePresentProperties(recordBase, record) } const SAVE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record/` @@ -32,11 +29,32 @@ return await response.json() } + export async function duplicateRecord(record, { appname, instanceId }) { + let recordBase = { ...record } + + delete recordBase.id + + recordBase = getNewRecord(recordBase, recordBase.key) + recordBase = overwritePresentProperties(recordBase, record) + + const SAVE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record/` + const response = await api.post(SAVE_RECORDS_URL, recordBase) + return await response.json() + } + export async function fetchDataForView(viewName, { appname, instanceId }) { const FETCH_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/listRecords/${viewName}`; - // TODO: Error handling const response = await api.get(FETCH_RECORDS_URL); return await response.json(); + } + + function overwritePresentProperties(baseObj, overwrites) { + const base = { ...baseObj } + + for (let key in base) { + if (overwrites[key]) base[key] = overwrites[key] + } + return base; } \ No newline at end of file diff --git a/packages/builder/src/database/ModelDataTable/modals/CreateEditRecord.svelte b/packages/builder/src/database/ModelDataTable/modals/CreateEditRecord.svelte index a13dbcb532..4b2b04e9d1 100644 --- a/packages/builder/src/database/ModelDataTable/modals/CreateEditRecord.svelte +++ b/packages/builder/src/database/ModelDataTable/modals/CreateEditRecord.svelte @@ -14,8 +14,8 @@ let selectedModel $: currentAppInfo = { - instanceId: $store.currentInstanceId, appname: $store.appname, + instanceId: $backendUiStore.selectedDatabase.id } $: recordFields = record ? Object.keys(record) : [] $: models = $store.hierarchy.children @@ -66,7 +66,6 @@
Cancel { const recordResponse = await api.saveRecord(record || selectedModel, currentAppInfo) backendUiStore.update(state => { diff --git a/packages/builder/src/database/ModelDataTable/modals/DeleteRecord.svelte b/packages/builder/src/database/ModelDataTable/modals/DeleteRecord.svelte index 08fcc66a64..419d0176cd 100644 --- a/packages/builder/src/database/ModelDataTable/modals/DeleteRecord.svelte +++ b/packages/builder/src/database/ModelDataTable/modals/DeleteRecord.svelte @@ -7,8 +7,8 @@ export let record $: currentAppInfo = { - instanceId: $store.currentInstanceId, appname: $store.appname, + instanceId: $backendUiStore.selectedDatabase.id } function onClosed() { diff --git a/packages/builder/src/database/ModelView.svelte b/packages/builder/src/database/ModelView.svelte index d6e1be99b6..9b4743a178 100644 --- a/packages/builder/src/database/ModelView.svelte +++ b/packages/builder/src/database/ModelView.svelte @@ -91,15 +91,15 @@ {#if !editingField} -

Create / Edit Model

+

Create / Edit Model

{:else} -

Create / Edit Field

+

Create / Edit Field

{/if}
{#if !editingField} +

Settings

-

Settings

@@ -204,7 +204,7 @@ align-items: center; } - h4 { + h3 { margin: 0 0 0 10px; } diff --git a/packages/builder/src/nav/SchemaManagementDrawer.svelte b/packages/builder/src/nav/SchemaManagementDrawer.svelte index ff1c461eec..fa18364295 100644 --- a/packages/builder/src/nav/SchemaManagementDrawer.svelte +++ b/packages/builder/src/nav/SchemaManagementDrawer.svelte @@ -6,47 +6,19 @@ import NavItem from "./NavItem.svelte" import getIcon from "../common/icon" - const defaultNewChildActions = [ - { - label: "New Root Record", - onclick: store.newRootRecord, - }, - { - label: "New Root Index", - onclick: store.newRootIndex, - }, - ] - - let newChildActions = defaultNewChildActions - - const setActiveNav = name => () => { - store.setActiveNav(name) + function newModel() { + if ($store.currentNode) { + store.newChildRecord() + } else { + store.newRootRecord() + } + backendUiStore.actions.modals.show("MODEL") } - store.subscribe(db => { - if (!db.currentNode || hierarchyFunctions.isIndex(db.currentNode)) { - newChildActions = defaultNewChildActions - } else { - newChildActions = [ - { - label: "New Root Record", - onclick: store.newRootRecord, - }, - { - label: "New Root Index", - onclick: store.newRootIndex, - }, - { - label: `New Child Record of ${db.currentNode.name}`, - onclick: store.newChildRecord, - }, - { - label: `New Index on ${db.currentNode.name}`, - onclick: store.newChildIndex, - }, - ] - } - }) + function newView() { + store.newRootIndex() + backendUiStore.actions.modals.show("VIEW") + }
@@ -60,18 +32,12 @@
  • { - store.newRootRecord() - backendUiStore.actions.modals.show('MODEL') - }}> + on:click={newModel}> Model
  • { - store.newRootIndex() - backendUiStore.actions.modals.show('VIEW') - }}> + on:click={newView}> View