2021-01-19 18:38:24 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-01-04 19:57:16 +01:00
|
|
|
import { enrichDataBinding, enrichDataBindings } from "./enrichDataBinding"
|
2021-01-19 18:38:24 +01:00
|
|
|
import { routeStore, builderStore } from "../store"
|
2021-01-15 14:11:51 +01:00
|
|
|
import { saveRow, deleteRow, executeQuery, triggerAutomation } from "../api"
|
2020-11-25 10:50:51 +01:00
|
|
|
|
|
|
|
const saveRowHandler = async (action, context) => {
|
2021-01-19 18:38:24 +01:00
|
|
|
const { fields, providerId } = action.parameters
|
|
|
|
if (providerId) {
|
|
|
|
let draft = context[`${action.parameters.providerId}_draft`]
|
|
|
|
if (fields) {
|
|
|
|
Object.entries(fields).forEach(([key, entry]) => {
|
|
|
|
draft[key] = enrichDataBinding(entry.value, context)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await saveRow(draft)
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteRowHandler = async (action, context) => {
|
|
|
|
const { tableId, revId, rowId } = action.parameters
|
2021-01-19 18:38:24 +01:00
|
|
|
if (tableId && revId && rowId) {
|
|
|
|
await deleteRow({
|
|
|
|
tableId: enrichDataBinding(tableId, context),
|
|
|
|
rowId: enrichDataBinding(rowId, context),
|
|
|
|
revId: enrichDataBinding(revId, context),
|
|
|
|
})
|
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 18:25:06 +01:00
|
|
|
const triggerAutomationHandler = async (action, context) => {
|
|
|
|
const params = {}
|
2021-01-19 18:38:24 +01:00
|
|
|
if (action.parameters.fields) {
|
|
|
|
for (let field in action.parameters.fields) {
|
|
|
|
params[field] = enrichDataBinding(
|
|
|
|
action.parameters.fields[field].value,
|
|
|
|
context
|
|
|
|
)
|
|
|
|
}
|
2021-01-08 18:25:06 +01:00
|
|
|
}
|
|
|
|
await triggerAutomation(action.parameters.automationId, params)
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
const navigationHandler = action => {
|
2021-01-19 18:38:24 +01:00
|
|
|
if (action.parameters.url) {
|
|
|
|
routeStore.actions.navigate(action.parameters.url)
|
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 19:57:16 +01:00
|
|
|
const queryExecutionHandler = async (action, context) => {
|
2021-01-08 13:06:37 +01:00
|
|
|
const { datasourceId, queryId, queryParams } = action.parameters
|
|
|
|
const enrichedQueryParameters = enrichDataBindings(queryParams, context)
|
2021-01-08 19:22:03 +01:00
|
|
|
await executeQuery({
|
|
|
|
datasourceId,
|
|
|
|
queryId,
|
|
|
|
parameters: enrichedQueryParameters,
|
|
|
|
})
|
2021-01-04 19:57:16 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
const handlerMap = {
|
|
|
|
["Save Row"]: saveRowHandler,
|
|
|
|
["Delete Row"]: deleteRowHandler,
|
|
|
|
["Navigate To"]: navigationHandler,
|
2021-01-04 19:57:16 +01:00
|
|
|
["Execute Query"]: queryExecutionHandler,
|
2021-01-08 18:25:06 +01:00
|
|
|
["Trigger Automation"]: triggerAutomationHandler,
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an array of actions and returns a function which will execute the
|
|
|
|
* actions in the current context.
|
|
|
|
*/
|
|
|
|
export const enrichButtonActions = (actions, context) => {
|
2021-01-19 18:38:24 +01:00
|
|
|
// Prevent button actions in the builder preview
|
|
|
|
if (get(builderStore).inBuilder) {
|
|
|
|
return () => {}
|
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
const handlers = actions.map(def => handlerMap[def["##eventHandlerType"]])
|
|
|
|
return async () => {
|
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
|
|
|
await handlers[i](actions[i], context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|