Get isDisplay and field type from table

This commit is contained in:
Adria Navarro 2024-08-22 11:19:17 +02:00
parent 2ee356692e
commit cdead18402
2 changed files with 56 additions and 37 deletions

View File

@ -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) {
.map(([name, column]) => { cache.actions.getTable(relationshipField.tableId).then(table => {
return { relationshipPanelColumns = Object.entries(
name: name, relationshipField?.schema || {}
label: name, )
primaryDisplay: column.primaryDisplay, .map(([name, column]) => {
schema: { return {
...column, name: name,
visible: column.visible, label: name,
readonly: column.readonly, primaryDisplay: name === table.primaryDisplay,
}, schema: {
} type: table.schema[name].type,
}) visible: column.visible,
.sort((a, b) => readonly: column.readonly,
a.primaryDisplay === b.primaryDisplay ? 0 : a.primaryDisplay ? -1 : 1 },
) }
})
.sort((a, b) =>
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

View File

@ -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
primaryDisplayCache[tableId] = display
resolve(display)
})
})
} }
// We await the result so that we account for both promises and primitives // We await the result so that we account for both promises and primitives
return await primaryDisplayCache[tableId] return await tableCache[tableId]
}
const getPrimaryDisplayForTableId = async 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)
} }