Allow forms to generate query schemas. Fix query execution action

This commit is contained in:
Andrew Kingston 2021-02-03 14:53:13 +00:00
parent a1ed420c73
commit d85665d21c
9 changed files with 40 additions and 44 deletions

View File

@ -6,7 +6,7 @@ import {
makeMainForm, makeMainForm,
makeTitleContainer, makeTitleContainer,
makeSaveButton, makeSaveButton,
makeSchemaFormComponents, makeTableFormComponents,
} from "./utils/commonComponents" } from "./utils/commonComponents"
export default function(tables) { export default function(tables) {
@ -51,7 +51,7 @@ const createScreen = table => {
}) })
// Add all form fields from this schema to the field group // Add all form fields from this schema to the field group
makeSchemaFormComponents(table._id).forEach(component => { makeTableFormComponents(table._id).forEach(component => {
fieldGroup.addChild(component) fieldGroup.addChild(component)
}) })

View File

@ -7,7 +7,7 @@ import {
makeBreadcrumbContainer, makeBreadcrumbContainer,
makeTitleContainer, makeTitleContainer,
makeSaveButton, makeSaveButton,
makeSchemaFormComponents, makeTableFormComponents,
makeMainForm, makeMainForm,
} from "./utils/commonComponents" } from "./utils/commonComponents"
@ -106,7 +106,7 @@ const createScreen = table => {
}) })
// Add all form fields from this schema to the field group // Add all form fields from this schema to the field group
makeSchemaFormComponents(table._id).forEach(component => { makeTableFormComponents(table._id).forEach(component => {
fieldGroup.addChild(component) fieldGroup.addChild(component)
}) })

View File

@ -43,25 +43,6 @@ export function makeMainForm() {
.instanceName("Form") .instanceName("Form")
} }
export function makeMainContainer() {
return new Component("@budibase/standard-components/container")
.type("div")
.normalStyle({
width: "700px",
padding: "0px",
"border-radius": "0.5rem",
"box-shadow": "0 1px 2px 0 rgba(0, 0, 0, 0.05)",
margin: "auto",
"margin-top": "20px",
"padding-top": "48px",
"padding-bottom": "48px",
"padding-right": "48px",
"padding-left": "48px",
"margin-bottom": "20px",
})
.instanceName("Form")
}
export function makeBreadcrumbContainer(tableName, text, capitalise = false) { export function makeBreadcrumbContainer(tableName, text, capitalise = false) {
const link = makeLinkComponent(tableName).instanceName("Back Link") const link = makeLinkComponent(tableName).instanceName("Back Link")
@ -180,11 +161,30 @@ const fieldTypeToComponentMap = {
link: "relationshipfield", link: "relationshipfield",
} }
export function makeSchemaFormComponents(tableId) { export function makeTableFormComponents(tableId) {
const tables = get(backendUiStore).tables const tables = get(backendUiStore).tables
const schema = tables.find(table => table._id === tableId)?.schema ?? {} const schema = tables.find(table => table._id === tableId)?.schema ?? {}
return makeSchemaFormComponents(schema)
}
export function makeQueryFormComponents(queryId) {
const queries = get(backendUiStore).queries
const schema = queries.find(query => query._id === queryId)?.schema ?? []
return makeSchemaFormComponents(schema)
}
export function makeDatasourceFormComponents(datasource) {
if (!datasource) {
return []
}
return datasource.type === "table"
? makeTableFormComponents(datasource.tableId)
: makeQueryFormComponents(datasource._id)
}
function makeSchemaFormComponents(schema) {
let components = [] let components = []
let fields = Object.keys(schema) let fields = Object.keys(schema || {})
fields.forEach(field => { fields.forEach(field => {
const fieldSchema = schema[field] const fieldSchema = schema[field]
const componentType = fieldTypeToComponentMap[fieldSchema.type] const componentType = fieldTypeToComponentMap[fieldSchema.type]

View File

@ -2,7 +2,6 @@
import groupBy from "lodash/fp/groupBy" import groupBy from "lodash/fp/groupBy"
import { import {
TextArea, TextArea,
Label,
Input, Input,
Heading, Heading,
Body, Body,

View File

@ -5,7 +5,7 @@
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { currentAsset } from "builderStore" import { currentAsset } from "builderStore"
import { findClosestMatchingComponent } from "builderStore/storeUtils" import { findClosestMatchingComponent } from "builderStore/storeUtils"
import { makeSchemaFormComponents } from "builderStore/store/screenTemplates/utils/commonComponents" import { makeDatasourceFormComponents } from "builderStore/store/screenTemplates/utils/commonComponents"
import PropertyControl from "./PropertyControls/PropertyControl.svelte" import PropertyControl from "./PropertyControls/PropertyControl.svelte"
import Input from "./PropertyControls/Input.svelte" import Input from "./PropertyControls/Input.svelte"
import LayoutSelect from "./PropertyControls/LayoutSelect.svelte" import LayoutSelect from "./PropertyControls/LayoutSelect.svelte"
@ -106,9 +106,12 @@
componentInstance._id, componentInstance._id,
component => component._component.endsWith("/form") component => component._component.endsWith("/form")
) )
const tableId = form?.datasource?.tableId const datasource = form?.datasource
const fields = makeSchemaFormComponents(tableId).map(field => field.json()) const fields = makeDatasourceFormComponents(datasource)
onChange("_children", fields) onChange(
"_children",
fields.map(field => field.json())
)
} }
</script> </script>

View File

@ -4,7 +4,7 @@ import API from "./api"
/** /**
* Executes a query against an external data connector. * Executes a query against an external data connector.
*/ */
export const executeQuery = async ({ queryId, parameters }) => { export const executeQuery = async ({ queryId, parameters, notify = false }) => {
const res = await API.post({ const res = await API.post({
url: `/api/queries/${queryId}`, url: `/api/queries/${queryId}`,
body: { body: {
@ -13,6 +13,8 @@ export const executeQuery = async ({ queryId, parameters }) => {
}) })
if (res.error) { if (res.error) {
notificationStore.danger("An error has occurred") notificationStore.danger("An error has occurred")
} else if (notify) {
notificationStore.success("Query executed successfully")
} }
return res return res
} }

View File

@ -50,13 +50,13 @@ const navigationHandler = action => {
} }
} }
const queryExecutionHandler = async (action, context) => { const queryExecutionHandler = async action => {
const { datasourceId, queryId, queryParams } = action.parameters const { datasourceId, queryId, queryParams } = action.parameters
const enrichedQueryParameters = enrichDataBindings(queryParams || {}, context)
await executeQuery({ await executeQuery({
datasourceId, datasourceId,
queryId, queryId,
parameters: enrichedQueryParameters, parameters: queryParams,
notify: true,
}) })
} }

View File

@ -46,10 +46,7 @@ export const enrichProps = async (props, context, user) => {
// Enrich button actions if they exist // Enrich button actions if they exist
if (props._component.endsWith("/button") && enrichedProps.onClick) { if (props._component.endsWith("/button") && enrichedProps.onClick) {
enrichedProps.onClick = enrichButtonActions( enrichedProps.onClick = enrichButtonActions(enrichedProps.onClick)
enrichedProps.onClick,
totalContext
)
} }
return enrichedProps return enrichedProps

View File

@ -133,14 +133,9 @@
} else { } else {
table = await API.fetchTableDefinition(datasource?.tableId) table = await API.fetchTableDefinition(datasource?.tableId)
if (table) { if (table) {
if (datasource.type === "query") {
console.log("No implementation for queries yet")
schema = {}
} else {
schema = table.schema || {} schema = table.schema || {}
} }
} }
}
loaded = true loaded = true
} }