Get isDisplay and field type from table
This commit is contained in:
parent
2ee356692e
commit
cdead18402
|
@ -5,7 +5,6 @@
|
||||||
import ToggleActionButtonGroup from "./ToggleActionButtonGroup.svelte"
|
import ToggleActionButtonGroup from "./ToggleActionButtonGroup.svelte"
|
||||||
import { helpers } from "@budibase/shared-core"
|
import { helpers } from "@budibase/shared-core"
|
||||||
import { FieldType } from "@budibase/types"
|
import { FieldType } from "@budibase/types"
|
||||||
import { tables } from "stores/builder"
|
|
||||||
import { FieldPermissions } from "../../../constants"
|
import { FieldPermissions } from "../../../constants"
|
||||||
|
|
||||||
export let permissions = [FieldPermissions.WRITABLE, FieldPermissions.HIDDEN]
|
export let permissions = [FieldPermissions.WRITABLE, FieldPermissions.HIDDEN]
|
||||||
|
@ -13,12 +12,14 @@
|
||||||
export let columns
|
export let columns
|
||||||
export let fromRelationshipField
|
export let fromRelationshipField
|
||||||
|
|
||||||
const { datasource, dispatch } = getContext("grid")
|
const { datasource, dispatch, cache } = getContext("grid")
|
||||||
|
|
||||||
let relationshipPanelAnchor
|
let relationshipPanelAnchor
|
||||||
let relationshipFieldName
|
let relationshipFieldName
|
||||||
|
|
||||||
$: relationshipField = columns.find(c => c.name === relationshipFieldName)
|
$: relationshipField = columns.find(
|
||||||
|
c => c.name === relationshipFieldName
|
||||||
|
)?.schema
|
||||||
$: permissionsObj = permissions.reduce(
|
$: permissionsObj = permissions.reduce(
|
||||||
(acc, c) => ({
|
(acc, c) => ({
|
||||||
...acc,
|
...acc,
|
||||||
|
@ -113,24 +114,37 @@
|
||||||
return { ...c, options }
|
return { ...c, options }
|
||||||
})
|
})
|
||||||
|
|
||||||
$: relationshipPanelColumns = Object.entries(
|
let relationshipPanelColumns = []
|
||||||
relationshipField?.schema?.schema || {}
|
$: {
|
||||||
|
if (relationshipField) {
|
||||||
|
cache.actions.getTable(relationshipField.tableId).then(table => {
|
||||||
|
relationshipPanelColumns = Object.entries(
|
||||||
|
relationshipField?.schema || {}
|
||||||
)
|
)
|
||||||
.map(([name, column]) => {
|
.map(([name, column]) => {
|
||||||
return {
|
return {
|
||||||
name: name,
|
name: name,
|
||||||
label: name,
|
label: name,
|
||||||
primaryDisplay: column.primaryDisplay,
|
primaryDisplay: name === table.primaryDisplay,
|
||||||
schema: {
|
schema: {
|
||||||
...column,
|
type: table.schema[name].type,
|
||||||
visible: column.visible,
|
visible: column.visible,
|
||||||
readonly: column.readonly,
|
readonly: column.readonly,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.sort((a, b) =>
|
.sort((a, b) =>
|
||||||
a.primaryDisplay === b.primaryDisplay ? 0 : a.primaryDisplay ? -1 : 1
|
a.primaryDisplay === b.primaryDisplay
|
||||||
|
? 0
|
||||||
|
: a.primaryDisplay
|
||||||
|
? -1
|
||||||
|
: 1
|
||||||
)
|
)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
relationshipPanelColumns = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function toggleColumn(column, permission) {
|
async function toggleColumn(column, permission) {
|
||||||
const visible = permission !== FieldPermissions.HIDDEN
|
const visible = permission !== FieldPermissions.HIDDEN
|
||||||
|
|
|
@ -4,35 +4,40 @@ export const createActions = context => {
|
||||||
// Cache for the primary display columns of different tables.
|
// Cache for the primary display columns of different tables.
|
||||||
// If we ever need to cache table definitions for other purposes then we can
|
// If we ever need to cache table definitions for other purposes then we can
|
||||||
// expand this to be a more generic cache.
|
// expand this to be a more generic cache.
|
||||||
let primaryDisplayCache = {}
|
let tableCache = {}
|
||||||
|
|
||||||
const resetPrimaryDisplayCache = () => {
|
const resetCache = () => {
|
||||||
primaryDisplayCache = {}
|
tableCache = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getPrimaryDisplayForTableId = async tableId => {
|
const fetchTable = async tableId => {
|
||||||
// If we've never encountered this tableId before then store a promise that
|
// If we've never encountered this tableId before then store a promise that
|
||||||
// resolves to the primary display so that subsequent invocations before the
|
// resolves to the primary display so that subsequent invocations before the
|
||||||
// promise completes can reuse this promise
|
// promise completes can reuse this promise
|
||||||
if (!primaryDisplayCache[tableId]) {
|
if (!tableCache[tableId]) {
|
||||||
primaryDisplayCache[tableId] = new Promise(resolve => {
|
tableCache[tableId] = API.fetchTableDefinition(tableId)
|
||||||
API.fetchTableDefinition(tableId).then(def => {
|
}
|
||||||
const display = def?.primaryDisplay || def?.schema?.[0]?.name
|
// We await the result so that we account for both promises and primitives
|
||||||
primaryDisplayCache[tableId] = display
|
return await tableCache[tableId]
|
||||||
resolve(display)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We await the result so that we account for both promises and primitives
|
const getPrimaryDisplayForTableId = async tableId => {
|
||||||
return await primaryDisplayCache[tableId]
|
const table = await fetchTable(tableId)
|
||||||
|
const display = table?.primaryDisplay || table?.schema?.[0]?.name
|
||||||
|
return display
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTable = async tableId => {
|
||||||
|
const table = await fetchTable(tableId)
|
||||||
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cache: {
|
cache: {
|
||||||
actions: {
|
actions: {
|
||||||
getPrimaryDisplayForTableId,
|
getPrimaryDisplayForTableId,
|
||||||
resetPrimaryDisplayCache,
|
getTable,
|
||||||
|
resetCache,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -43,5 +48,5 @@ export const initialise = context => {
|
||||||
|
|
||||||
// Wipe the caches whenever the datasource changes to ensure we aren't
|
// Wipe the caches whenever the datasource changes to ensure we aren't
|
||||||
// storing any stale information
|
// storing any stale information
|
||||||
datasource.subscribe(cache.actions.resetPrimaryDisplayCache)
|
datasource.subscribe(cache.actions.resetCache)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue