Move and unify code

This commit is contained in:
Adria Navarro 2024-09-25 16:39:13 +02:00
parent 012c7142fc
commit 337ac90906
4 changed files with 59 additions and 56 deletions

View File

@ -6,6 +6,7 @@ import { tick } from "svelte"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import { sleep } from "../../../utils/utils" import { sleep } from "../../../utils/utils"
import { FieldType, RelationshipType } from "@budibase/types" import { FieldType, RelationshipType } from "@budibase/types"
import { getRelatedTableValues } from "../../../utils"
export const createStores = () => { export const createStores = () => {
const rows = writable([]) const rows = writable([])
@ -41,34 +42,6 @@ export const createStores = () => {
} }
} }
function getRelatedTableValues(row, field, isSingle) {
let result = ""
try {
if (isSingle) {
result = row[field.related.field]?.[0]?.[field.related.subField]
} else {
result = Array.from(
new Set(
row[field.related.field].flatMap(r => r[field.related.subField])
)
)
switch (field.type) {
case FieldType.STRING:
case FieldType.NUMBER:
case FieldType.BIGINT:
case FieldType.BARCODEQR:
result = result.join(", ")
break
}
}
} catch (e) {
result = "Not rendable"
console.error(e.message)
}
return result
}
export const deriveStores = context => { export const deriveStores = context => {
const { rows, enrichedSchema } = context const { rows, enrichedSchema } = context

View File

@ -10,4 +10,4 @@ export { createWebsocket } from "./websocket"
export * from "./download" export * from "./download"
export * from "./theme" export * from "./theme"
export * from "./settings" export * from "./settings"
export * from "./schema" export * from "./relatedColumns"

View File

@ -0,0 +1,57 @@
import { FieldType } from "@budibase/types"
export function enrichSchemaWithRelColumns(schema) {
if (!schema) {
return
}
const result = Object.keys(schema).reduce((acc, c) => {
const field = schema[c]
acc[c] = field
if (field.visible !== false && field.columns) {
for (const relColumn of Object.keys(field.columns)) {
const relField = field.columns[relColumn]
if (!relField.visible) {
continue
}
const name = `${field.name}.${relColumn}`
acc[name] = {
...relField,
name,
related: { field: c, subField: relColumn },
}
}
}
return acc
}, {})
return result
}
export function getRelatedTableValues(row, field, isSingle) {
let result = ""
try {
if (isSingle) {
result = row[field.related.field]?.[0]?.[field.related.subField]
} else {
result = Array.from(
new Set(
row[field.related.field].flatMap(r => r[field.related.subField])
)
)
switch (field.type) {
case FieldType.STRING:
case FieldType.NUMBER:
case FieldType.BIGINT:
case FieldType.BARCODEQR:
result = result.join(", ")
break
}
}
} catch (e) {
result = "Not rendable"
console.error(e.message)
}
return result
}

View File

@ -1,27 +0,0 @@
export function enrichSchemaWithRelColumns(schema) {
if (!schema) {
return
}
const result = Object.keys(schema).reduce((acc, c) => {
const field = schema[c]
acc[c] = field
if (field.visible !== false && field.columns) {
for (const relColumn of Object.keys(field.columns)) {
const relField = field.columns[relColumn]
if (!relField.visible) {
continue
}
const name = `${field.name}.${relColumn}`
acc[name] = {
...relField,
name,
related: { field: c, subField: relColumn },
}
}
}
return acc
}, {})
return result
}