Update when empty setting enrichment runs to ensure card blocks properly reset when changing datasource

This commit is contained in:
Andrew Kingston 2024-04-19 10:55:58 +01:00
parent cb3937b5c1
commit e98a9f7f80
1 changed files with 54 additions and 29 deletions

View File

@ -299,39 +299,64 @@ export class ComponentStore extends BudiStore {
// Add default bindings to card blocks // Add default bindings to card blocks
if (component._component.endsWith("/cardsblock")) { if (component._component.endsWith("/cardsblock")) {
const { _id, dataSource } = component // Only proceed if the card is empty, i.e. we just changed datasource or
if (dataSource) { // just created the card
const { schema, table } = getSchemaForDatasource(screen, dataSource) const cardKeys = ["cardTitle", "cardSubtitle", "cardDescription"]
const readableTypes = [ if (cardKeys.every(key => !component[key]) && !component.cardImageURL) {
FieldType.STRING, const { _id, dataSource } = component
FieldType.OPTIONS, if (dataSource) {
FieldType.DATETIME, const { schema, table } = getSchemaForDatasource(screen, dataSource)
FieldType.NUMBER, const findFieldTypes = fieldTypes => {
] if (!Array.isArray(fieldTypes)) {
fieldTypes = [fieldTypes]
}
return Object.entries(schema || {})
.filter(([name, fieldSchema]) => {
return (
fieldTypes.includes(fieldSchema.type) &&
!fieldSchema.autoColumn &&
name !== table?.primaryDisplay
)
})
.map(([name]) => name)
}
// Extract good field candidates to prefil our cards with // Extract good field candidates to prefil our cards with
const fields = Object.entries(schema || {}) const fields = findFieldTypes([
.filter(([name, fieldSchema]) => { FieldType.STRING,
return ( FieldType.OPTIONS,
readableTypes.includes(fieldSchema.type) && FieldType.DATETIME,
!fieldSchema.autoColumn && FieldType.NUMBER,
name !== table?.primaryDisplay ])
)
// Use the primary display as the best field, if it exists
if (schema?.[table?.primaryDisplay]) {
fields.unshift(table.primaryDisplay)
}
// Fill our cards with as many bindings as we can
const prefix = safe(`${_id}-repeater`)
cardKeys.forEach(key => {
if (!fields[0]) return
component[key] = `{{ ${prefix}.${safe(fields[0])} }}`
fields.shift()
}) })
.map(([name]) => name)
// Use the primary display as the best field, if it exists // Attempt to fill the image setting
if (schema?.[table?.primaryDisplay]) { let imgFields = findFieldTypes([FieldType.ATTACHMENT_SINGLE])
fields.unshift(table.primaryDisplay) if (imgFields[0]) {
component.cardImageURL = `{{ ${prefix}.${safe(
imgFields[0]
)}.[url] }}`
} else {
imgFields = findFieldTypes([FieldType.ATTACHMENTS])
if (imgFields[0]) {
component.cardImageURL = `{{ ${prefix}.${safe(
imgFields[0]
)}.[0].[url] }}`
}
}
} }
// Fill our cards with as many bindings as we can
const cardKeys = ["cardTitle", "cardSubtitle", "cardDescription"]
cardKeys.forEach(key => {
if (!fields[0] || component[key]) return
component[key] = `{{ ${safe(`${_id}-repeater`)}.${safe(fields[0])} }}`
fields.shift()
})
} }
} }
} }