From f959ea0adda2cdcaf01f0481756ba0b014c99817 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 24 Nov 2021 14:55:14 +0000 Subject: [PATCH] Adding more error tracking around column creation, to reduce confusion/users not understanding required settings. --- .../DataTable/modals/CreateEditColumn.svelte | 60 +- packages/builder/src/stores/backend/tables.js | 3 + packages/server/yarn.lock | 787 +----------------- 3 files changed, 62 insertions(+), 788 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index 7902212b7e..3d352877fa 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -9,6 +9,7 @@ DatePicker, ModalContent, Context, + notifications, } from "@budibase/bbui" import { createEventDispatcher } from "svelte" import { cloneDeep } from "lodash/fp" @@ -25,7 +26,6 @@ SWITCHABLE_TYPES, } from "constants/backend" import { getAutoColumnInformation, buildAutoColumn } from "builderStore/utils" - import { notifications } from "@budibase/bbui" import ValuesList from "components/common/ValuesList.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte" import { truncate } from "lodash" @@ -72,13 +72,8 @@ $: invalid = !field.name || (field.type === LINK_TYPE && !field.tableId) || - Object.keys($tables.draft?.schema ?? {}).some( - key => key !== originalName && key === field.name - ) || - columnNameInvalid - $: columnNameInvalid = PROHIBITED_COLUMN_NAMES.some( - name => field.name === name - ) + Object.keys(errors).length !== 0 + $: errors = checkErrors(field) // used to select what different options can be displayed for column type $: canBeSearched = @@ -106,13 +101,17 @@ if (field.type === AUTO_TYPE) { field = buildAutoColumn($tables.draft.name, field.name, field.subtype) } - await tables.saveField({ - originalName, - field, - primaryDisplay, - indexes, - }) - dispatch("updatecolumns") + try { + await tables.saveField({ + originalName, + field, + primaryDisplay, + indexes, + }) + dispatch("updatecolumns") + } catch (err) { + notifications.error(err) + } } function deleteColumn() { @@ -258,6 +257,32 @@ fieldToCheck.constraints.numericality = {} } } + + function checkErrors(fieldInfo) { + function inUse(tbl, column, ogName = null) { + return Object.keys(tbl?.schema || {}).some( + key => key !== ogName && key === column + ) + } + const newError = {} + if (PROHIBITED_COLUMN_NAMES.some(name => fieldInfo.name === name)) { + newError.name = `${PROHIBITED_COLUMN_NAMES.join( + ", " + )} are not allowed as column names` + } else if (inUse($tables.draft, fieldInfo.name, originalName)) { + newError.name = `Column name already in use.` + } + if (fieldInfo.fieldName && fieldInfo.tableId) { + const relatedTable = $tables.list.find( + tbl => tbl._id === fieldInfo.tableId + ) + console.log(relatedTable) + if (inUse(relatedTable, fieldInfo.fieldName)) { + newError.relatedName = `Column name already in use in table ${relatedTable.name}` + } + } + return newError + }