code clean up

This commit is contained in:
Peter Clement 2023-09-25 14:38:36 +01:00
parent 678afc134e
commit 85f4141e46
3 changed files with 25 additions and 56 deletions

View File

@ -5,7 +5,6 @@
Label, Label,
Select, Select,
Toggle, Toggle,
RadioGroup,
Icon, Icon,
DatePicker, DatePicker,
Modal, Modal,
@ -26,10 +25,10 @@
ALLOWABLE_STRING_TYPES, ALLOWABLE_STRING_TYPES,
ALLOWABLE_NUMBER_TYPES, ALLOWABLE_NUMBER_TYPES,
SWITCHABLE_TYPES, SWITCHABLE_TYPES,
PrettyRelationshipDefinitions,
} from "constants/backend" } from "constants/backend"
import { getAutoColumnInformation, buildAutoColumn } from "builderStore/utils" import { getAutoColumnInformation, buildAutoColumn } from "builderStore/utils"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { truncate } from "lodash"
import ModalBindableInput from "components/common/bindings/ModalBindableInput.svelte" import ModalBindableInput from "components/common/bindings/ModalBindableInput.svelte"
import { getBindings } from "components/backend/DataTable/formula" import { getBindings } from "components/backend/DataTable/formula"
import JSONSchemaModal from "./JSONSchemaModal.svelte" import JSONSchemaModal from "./JSONSchemaModal.svelte"
@ -58,8 +57,8 @@
let indexes = [...($tables.selected.indexes || [])] let indexes = [...($tables.selected.indexes || [])]
let isCreating = undefined let isCreating = undefined
let relationshipPart1 = "Many rows" let relationshipPart1 = PrettyRelationshipDefinitions.Many
let relationshipPart2 = "To one row" let relationshipPart2 = PrettyRelationshipDefinitions.One
let relationshipTableIdSecondary = null let relationshipTableIdSecondary = null
let table = $tables.selected let table = $tables.selected
@ -80,22 +79,21 @@
} }
$: { $: {
console.log("test") if (editableColumn.type === LINK_TYPE && editableColumn.tableId) {
if (editableColumn.type === LINK_TYPE) {
// Determine the relationship type based on the selected values of both parts // Determine the relationship type based on the selected values of both parts
if ( if (
relationshipPart1 === "Many rows" && relationshipPart1 === PrettyRelationshipDefinitions.Many &&
relationshipPart2 === "To one row" relationshipPart2 === PrettyRelationshipDefinitions.One
) { ) {
editableColumn.relationshipType = RelationshipType.MANY_TO_ONE editableColumn.relationshipType = RelationshipType.MANY_TO_ONE
} else if ( } else if (
relationshipPart1 === "Many rows" && relationshipPart1 === PrettyRelationshipDefinitions.Many &&
relationshipPart2 === "To many rows" relationshipPart2 === PrettyRelationshipDefinitions.MANY
) { ) {
editableColumn.relationshipType = RelationshipType.MANY_TO_MANY editableColumn.relationshipType = RelationshipType.MANY_TO_MANY
} else if ( } else if (
relationshipPart1 === "One row" && relationshipPart1 === PrettyRelationshipDefinitions.One &&
relationshipPart2 === "To many rows" relationshipPart2 === PrettyRelationshipDefinitions.Many
) { ) {
editableColumn.relationshipType = RelationshipType.ONE_TO_MANY editableColumn.relationshipType = RelationshipType.ONE_TO_MANY
} }
@ -134,23 +132,19 @@
if (editableColumn.type === LINK_TYPE && editableColumn.tableId) { if (editableColumn.type === LINK_TYPE && editableColumn.tableId) {
relationshipTableIdSecondary = editableColumn.tableId relationshipTableIdSecondary = editableColumn.tableId
console.log("test?")
console.log(editableColumn.relationshipType)
console.log(RelationshipType.MANY_TO_MANY)
if (editableColumn.relationshipType === RelationshipType.MANY_TO_ONE) { if (editableColumn.relationshipType === RelationshipType.MANY_TO_ONE) {
relationshipPart1 = "Many rows" relationshipPart1 = PrettyRelationshipDefinitions.Many
relationshipPart2 = "To one row" relationshipPart2 = PrettyRelationshipDefinitions.One
} else if ( } else if (
editableColumn.relationshipType === RelationshipType.MANY_TO_MANY editableColumn.relationshipType === RelationshipType.MANY_TO_MANY
) { ) {
console.log("asdasdasd?") relationshipPart1 = PrettyRelationshipDefinitions.Many
relationshipPart1 = "Many rows" relationshipPart2 = PrettyRelationshipDefinitions.Many
relationshipPart2 = "To many rows"
} else if ( } else if (
editableColumn.relationshipType === RelationshipType.ONE_TO_MANY editableColumn.relationshipType === RelationshipType.ONE_TO_MANY
) { ) {
relationshipPart1 = "One row" relationshipPart1 = PrettyRelationshipDefinitions.One
relationshipPart2 = "To many rows" relationshipPart2 = PrettyRelationshipDefinitions.Many
} }
} }
} }
@ -210,7 +204,6 @@
!uneditable && !uneditable &&
editableColumn?.type !== AUTO_TYPE && editableColumn?.type !== AUTO_TYPE &&
!editableColumn.autocolumn !editableColumn.autocolumn
$: relationshipOptions = getRelationshipOptions(editableColumn)
$: external = table.type === "external" $: external = table.type === "external"
// in the case of internal tables the sourceId will just be undefined // in the case of internal tables the sourceId will just be undefined
$: tableOptions = $tables.list.filter( $: tableOptions = $tables.list.filter(
@ -341,35 +334,6 @@
return match ? parseInt(match[1]) : 0 return match ? parseInt(match[1]) : 0
} }
function getRelationshipOptions(field) {
if (!field || !field.tableId) {
return null
}
const linkTable = tableOptions?.find(table => table._id === field.tableId)
if (!linkTable) {
return null
}
const thisName = truncate(table.name, { length: 14 }),
linkName = truncate(linkTable.name, { length: 14 })
return [
{
name: `Many ${thisName} rows → many ${linkName} rows`,
alt: `Many ${table.name} rows → many ${linkTable.name} rows`,
value: RelationshipType.MANY_TO_MANY,
},
{
name: `One ${linkName} row → many ${thisName} rows`,
alt: `One ${linkTable.name} rows → many ${table.name} rows`,
value: RelationshipType.ONE_TO_MANY,
},
{
name: `One ${thisName} row → many ${linkName} rows`,
alt: `One ${table.name} rows → many ${linkTable.name} rows`,
value: RelationshipType.MANY_TO_ONE,
},
]
}
function getAllowedTypes() { function getAllowedTypes() {
if ( if (
originalName && originalName &&

View File

@ -1,6 +1,6 @@
<script> <script>
import { Select, Input } from "@budibase/bbui" import { Select, Input } from "@budibase/bbui"
import { RelationshipType } from "constants/backend" import { PrettyRelationshipDefinitions } from "constants/backend"
export let relationshipPart1 export let relationshipPart1
export let relationshipPart2 export let relationshipPart2
@ -17,7 +17,7 @@
<Select <Select
disabled={linkEditDisabled} disabled={linkEditDisabled}
bind:value={relationshipPart1} bind:value={relationshipPart1}
options={["Many rows", "One row"]} options={Object.values(PrettyRelationshipDefinitions)}
/> />
</div> </div>
<div class="relationship-label">in</div> <div class="relationship-label">in</div>
@ -34,8 +34,8 @@
<Select <Select
disabled={linkEditDisabled} disabled={linkEditDisabled}
bind:value={relationshipPart2} bind:value={relationshipPart2}
options={["To one row", "To many rows"]} options={Object.values(PrettyRelationshipDefinitions)}
getOptionLabel={option => option} getOptionLabel={option => "To " + option.toLowerCase()}
/> />
</div> </div>
<div class="relationship-label">in</div> <div class="relationship-label">in</div>

View File

@ -170,6 +170,11 @@ export const RelationshipType = {
MANY_TO_ONE: "many-to-one", MANY_TO_ONE: "many-to-one",
} }
export const PrettyRelationshipDefinitions = {
MANY: "Many rows",
ONE: "One row",
}
export const ALLOWABLE_STRING_OPTIONS = [ export const ALLOWABLE_STRING_OPTIONS = [
FIELDS.STRING, FIELDS.STRING,
FIELDS.OPTIONS, FIELDS.OPTIONS,