Move and unify code
This commit is contained in:
parent
012c7142fc
commit
337ac90906
|
@ -6,6 +6,7 @@ import { tick } from "svelte"
|
|||
import { Helpers } from "@budibase/bbui"
|
||||
import { sleep } from "../../../utils/utils"
|
||||
import { FieldType, RelationshipType } from "@budibase/types"
|
||||
import { getRelatedTableValues } from "../../../utils"
|
||||
|
||||
export const createStores = () => {
|
||||
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 => {
|
||||
const { rows, enrichedSchema } = context
|
||||
|
||||
|
|
|
@ -10,4 +10,4 @@ export { createWebsocket } from "./websocket"
|
|||
export * from "./download"
|
||||
export * from "./theme"
|
||||
export * from "./settings"
|
||||
export * from "./schema"
|
||||
export * from "./relatedColumns"
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue