From 5f8d6bbfe4868b621b968aae9370eff524e96e8d Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 17 Jun 2020 16:51:10 +0100 Subject: [PATCH] add new models and fields, delete models --- packages/builder/src/budibase.css | 3 +- .../builder/src/builderStore/store/backend.js | 29 +++-- .../common/LinkedRecordSelector.svelte | 68 ++++++++++++ .../ModelDataTable/ModelDataTable.svelte | 4 +- .../modals/CreateEditRecord.svelte | 15 ++- .../nav/ModelNavigator/ListItem.svelte | 4 +- .../nav/ModelNavigator/ModelNavigator.svelte | 12 +-- .../nav/ModelSetupNav/FieldView.svelte | 29 +++-- .../nav/ModelSetupNav/ModelSetupNav.svelte | 24 +++-- .../builder/src/constants/backend/index.js | 101 +++++++++++++++++- packages/builder/src/global.css | 1 - .../database/[selectedDatabase]/Block.svelte | 9 +- .../database/[selectedDatabase]/index.svelte | 5 +- .../[selectedDatabase]/newmodel.svelte | 51 +++++++-- 14 files changed, 295 insertions(+), 60 deletions(-) create mode 100644 packages/builder/src/components/common/LinkedRecordSelector.svelte diff --git a/packages/builder/src/budibase.css b/packages/builder/src/budibase.css index 3d880cbfa2..14a1be3083 100644 --- a/packages/builder/src/budibase.css +++ b/packages/builder/src/budibase.css @@ -77,7 +77,8 @@ .budibase__input { height: 35px; - width: 220px; + width: 100%; + max-width: 220px; border-radius: 3px; border: 1px solid var(--grey-dark); text-align: left; diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 32533f9420..2dff9db7d2 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -25,9 +25,7 @@ export const getBackendUiStore = () => { store.update(state => { state.selectedDatabase = db if (models && models.length > 0) { - state.selectedModel = models[0] - state.draftModel = models[0] - state.selectedView = `all_${models[0]._id}` + store.actions.models.select(models[0]); } state.breadcrumbs = [db.name] state.models = models @@ -56,14 +54,25 @@ export const getBackendUiStore = () => { models: { select: model => store.update(state => { state.selectedModel = model; - // TODO: prevent pointing to same obj state.draftModel = cloneDeep(model); - state.selectedField = null + state.selectedField = "" + state.selectedView = `all_${model._id}` return state; }), save: async ({ instanceId, model }) => { + const updatedModel = cloneDeep(model); + + // TODO: refactor + for (let key in updatedModel.schema) { + const field = updatedModel.schema[key] + if (field.name && field.name !== key) { + updatedModel.schema[field.name] = field + delete updatedModel.schema[key]; + } + } + const SAVE_MODEL_URL = `/api/${instanceId}/models` - const response = await api.post(SAVE_MODEL_URL, model) + const response = await api.post(SAVE_MODEL_URL, updatedModel) const savedModel = await response.json() store.update(state => { @@ -76,9 +85,7 @@ export const getBackendUiStore = () => { state.models = state.models } - state.selectedModel = savedModel - state.draftModel = savedModel - state.selectedView = `all_${savedModel._id}` + store.actions.models.select(savedModel) return state }) }, @@ -93,11 +100,11 @@ export const getBackendUiStore = () => { [field.name]: field } - state.selectedField = field + state.selectedField = field.name return state }); - } + }, }, views: { select: view => diff --git a/packages/builder/src/components/common/LinkedRecordSelector.svelte b/packages/builder/src/components/common/LinkedRecordSelector.svelte new file mode 100644 index 0000000000..18adfda61e --- /dev/null +++ b/packages/builder/src/components/common/LinkedRecordSelector.svelte @@ -0,0 +1,68 @@ + + +
+ {#each records as record} +
+

{record.name}

+
+ {#each Object.keys(record) as key} +
+ {key} +

{record[key]}

+
+ {/each} +
+
+ {/each} +
+ + \ No newline at end of file diff --git a/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte b/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte index f1d49894a0..ec2d228978 100644 --- a/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte +++ b/packages/builder/src/components/database/ModelDataTable/ModelDataTable.svelte @@ -68,10 +68,10 @@ } } - $: paginatedData = data.slice( + $: paginatedData = data ? data.slice( currentPage * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE + ITEMS_PER_PAGE - ) + ) : [] onMount(() => { if (views.length) { diff --git a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditRecord.svelte b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditRecord.svelte index 62688d58e3..abcc32b002 100644 --- a/packages/builder/src/components/database/ModelDataTable/modals/CreateEditRecord.svelte +++ b/packages/builder/src/components/database/ModelDataTable/modals/CreateEditRecord.svelte @@ -3,6 +3,7 @@ import { store, backendUiStore } from "builderStore" import { compose, map, get, flatten } from "lodash/fp" import ActionButton from "components/common/ActionButton.svelte" + import LinkedRecordSelector from "components/common/LinkedRecordSelector.svelte" import Select from "components/common/Select.svelte" import RecordFieldControl from "./RecordFieldControl.svelte" import * as api from "../api" @@ -73,11 +74,15 @@
{#each modelSchema as [key, meta]}
- + {#if meta.type === "link"} + + {:else} + + {/if}
{/each}
diff --git a/packages/builder/src/components/nav/ModelNavigator/ListItem.svelte b/packages/builder/src/components/nav/ModelNavigator/ListItem.svelte index 6452b3459d..d34f761834 100644 --- a/packages/builder/src/components/nav/ModelNavigator/ListItem.svelte +++ b/packages/builder/src/components/nav/ModelNavigator/ListItem.svelte @@ -13,7 +13,7 @@ \ No newline at end of file