Create new component for selecting relationships

This commit is contained in:
Peter Clement 2023-09-25 13:25:23 +01:00
parent d0097273b9
commit 678afc134e
2 changed files with 131 additions and 24 deletions

View File

@ -34,6 +34,7 @@
import { getBindings } from "components/backend/DataTable/formula" import { getBindings } from "components/backend/DataTable/formula"
import JSONSchemaModal from "./JSONSchemaModal.svelte" import JSONSchemaModal from "./JSONSchemaModal.svelte"
import { ValidColumnNameRegex } from "@budibase/shared-core" import { ValidColumnNameRegex } from "@budibase/shared-core"
import RelationshipSelector from "components/common/RelationshipSelector.svelte"
const AUTO_TYPE = "auto" const AUTO_TYPE = "auto"
const FORMULA_TYPE = FIELDS.FORMULA.type const FORMULA_TYPE = FIELDS.FORMULA.type
@ -57,6 +58,10 @@
let indexes = [...($tables.selected.indexes || [])] let indexes = [...($tables.selected.indexes || [])]
let isCreating = undefined let isCreating = undefined
let relationshipPart1 = "Many rows"
let relationshipPart2 = "To one row"
let relationshipTableIdSecondary = null
let table = $tables.selected let table = $tables.selected
let confirmDeleteDialog let confirmDeleteDialog
let savingColumn let savingColumn
@ -74,6 +79,32 @@
editableColumn.constraints.presence = { allowEmpty: false } editableColumn.constraints.presence = { allowEmpty: false }
} }
$: {
console.log("test")
if (editableColumn.type === LINK_TYPE) {
// Determine the relationship type based on the selected values of both parts
if (
relationshipPart1 === "Many rows" &&
relationshipPart2 === "To one row"
) {
editableColumn.relationshipType = RelationshipType.MANY_TO_ONE
} else if (
relationshipPart1 === "Many rows" &&
relationshipPart2 === "To many rows"
) {
editableColumn.relationshipType = RelationshipType.MANY_TO_MANY
} else if (
relationshipPart1 === "One row" &&
relationshipPart2 === "To many rows"
) {
editableColumn.relationshipType = RelationshipType.ONE_TO_MANY
}
// Set the tableId based on the selected table
editableColumn.tableId = relationshipTableIdSecondary
}
}
const initialiseField = (field, savingColumn) => { const initialiseField = (field, savingColumn) => {
isCreating = !field isCreating = !field
if (field && !savingColumn) { if (field && !savingColumn) {
@ -100,6 +131,28 @@
} }
} }
allowedTypes = getAllowedTypes() allowedTypes = getAllowedTypes()
if (editableColumn.type === LINK_TYPE && 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) {
relationshipPart1 = "Many rows"
relationshipPart2 = "To one row"
} else if (
editableColumn.relationshipType === RelationshipType.MANY_TO_MANY
) {
console.log("asdasdasd?")
relationshipPart1 = "Many rows"
relationshipPart2 = "To many rows"
} else if (
editableColumn.relationshipType === RelationshipType.ONE_TO_MANY
) {
relationshipPart1 = "One row"
relationshipPart2 = "To many rows"
}
}
} }
$: initialiseField(field, savingColumn) $: initialiseField(field, savingColumn)
@ -546,30 +599,15 @@
</div> </div>
</div> </div>
{:else if editableColumn.type === "link"} {:else if editableColumn.type === "link"}
<Select <RelationshipSelector
label="Table" bind:relationshipPart1
disabled={linkEditDisabled} bind:relationshipPart2
bind:value={editableColumn.tableId} bind:relationshipTableIdPrimary={table.name}
options={tableOptions} bind:relationshipTableIdSecondary
getOptionLabel={table => table.name} bind:editableColumn
getOptionValue={table => table._id} {linkEditDisabled}
/> {tableOptions}
{#if relationshipOptions && relationshipOptions.length > 0} {errors}
<RadioGroup
disabled={linkEditDisabled}
label="Define the relationship"
bind:value={editableColumn.relationshipType}
options={relationshipOptions}
getOptionLabel={option => option.name}
getOptionValue={option => option.value}
getOptionTitle={option => option.alt}
/>
{/if}
<Input
disabled={linkEditDisabled}
label={`Column name in other table`}
bind:value={editableColumn.fieldName}
error={errors.relatedName}
/> />
{:else if editableColumn.type === FORMULA_TYPE} {:else if editableColumn.type === FORMULA_TYPE}
{#if !table.sql} {#if !table.sql}

View File

@ -0,0 +1,69 @@
<script>
import { Select, Input } from "@budibase/bbui"
import { RelationshipType } from "constants/backend"
export let relationshipPart1
export let relationshipPart2
export let relationshipTableIdPrimary
export let relationshipTableIdSecondary
export let editableColumn
export let linkEditDisabled
export let tableOptions
export let errors
</script>
<div class="relationship-container">
<div class="relationship-part">
<Select
disabled={linkEditDisabled}
bind:value={relationshipPart1}
options={["Many rows", "One row"]}
/>
</div>
<div class="relationship-label">in</div>
<div class="relationship-part">
<Select
disabled
options={[relationshipTableIdPrimary]}
value={relationshipTableIdPrimary}
/>
</div>
</div>
<div class="relationship-container">
<div class="relationship-part">
<Select
disabled={linkEditDisabled}
bind:value={relationshipPart2}
options={["To one row", "To many rows"]}
getOptionLabel={option => option}
/>
</div>
<div class="relationship-label">in</div>
<div class="relationship-part">
<Select
disabled={linkEditDisabled}
bind:value={relationshipTableIdSecondary}
options={tableOptions}
getOptionLabel={table => table.name}
getOptionValue={table => table._id}
/>
</div>
</div>
<Input
disabled={linkEditDisabled}
label={`Column name in other table`}
bind:value={editableColumn.fieldName}
error={errors.relatedName}
/>
<style>
.relationship-container {
display: flex;
align-items: center;
gap: 20px;
}
.relationship-part {
flex-basis: 45%;
}
</style>