2021-01-20 06:38:24 +13:00
|
|
|
import { get } from "svelte/store"
|
|
|
|
import { routeStore, builderStore } from "../store"
|
2021-01-16 02:11:51 +13:00
|
|
|
import { saveRow, deleteRow, executeQuery, triggerAutomation } from "../api"
|
2021-02-02 07:51:22 +13:00
|
|
|
import { ActionTypes } from "../constants"
|
2020-11-25 22:50:51 +13:00
|
|
|
|
|
|
|
const saveRowHandler = async (action, context) => {
|
2021-01-20 06:38:24 +13:00
|
|
|
const { fields, providerId } = action.parameters
|
|
|
|
if (providerId) {
|
2021-02-03 03:32:58 +13:00
|
|
|
let draft = context[providerId]
|
2021-01-20 06:38:24 +13:00
|
|
|
if (fields) {
|
2021-02-19 06:44:56 +13:00
|
|
|
for (let [field, value] of Object.entries(fields)) {
|
|
|
|
draft[field] = value
|
2021-01-22 00:31:45 +13:00
|
|
|
}
|
2021-01-20 06:38:24 +13:00
|
|
|
}
|
|
|
|
await saveRow(draft)
|
2020-11-25 22:50:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 08:18:32 +13:00
|
|
|
const deleteRowHandler = async action => {
|
2020-11-25 22:50:51 +13:00
|
|
|
const { tableId, revId, rowId } = action.parameters
|
2021-01-20 06:38:24 +13:00
|
|
|
if (tableId && revId && rowId) {
|
2021-02-05 08:18:32 +13:00
|
|
|
await deleteRow({ tableId, rowId, revId })
|
2021-01-20 06:38:24 +13:00
|
|
|
}
|
2020-11-25 22:50:51 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 00:51:07 +13:00
|
|
|
const triggerAutomationHandler = async action => {
|
2021-02-05 02:01:49 +13:00
|
|
|
const { fields } = action.parameters
|
2021-01-22 00:31:45 +13:00
|
|
|
if (fields) {
|
2021-02-19 06:44:56 +13:00
|
|
|
await triggerAutomation(action.parameters.automationId, fields)
|
2021-01-09 06:25:06 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 22:50:51 +13:00
|
|
|
const navigationHandler = action => {
|
2021-01-20 06:38:24 +13:00
|
|
|
if (action.parameters.url) {
|
|
|
|
routeStore.actions.navigate(action.parameters.url)
|
|
|
|
}
|
2020-11-25 22:50:51 +13:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:53:13 +13:00
|
|
|
const queryExecutionHandler = async action => {
|
2021-01-09 01:06:37 +13:00
|
|
|
const { datasourceId, queryId, queryParams } = action.parameters
|
2021-01-09 07:22:03 +13:00
|
|
|
await executeQuery({
|
|
|
|
datasourceId,
|
|
|
|
queryId,
|
2021-02-04 03:53:13 +13:00
|
|
|
parameters: queryParams,
|
2021-01-09 07:22:03 +13:00
|
|
|
})
|
2021-01-05 07:57:16 +13:00
|
|
|
}
|
|
|
|
|
2021-02-06 01:54:36 +13:00
|
|
|
const executeActionHandler = async (context, componentId, actionType) => {
|
|
|
|
const fn = context[`${componentId}_${actionType}`]
|
2021-02-02 07:51:22 +13:00
|
|
|
if (fn) {
|
|
|
|
return await fn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:54:36 +13:00
|
|
|
const validateFormHandler = async (action, context) => {
|
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
|
|
|
ActionTypes.ValidateForm
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshDatasourceHandler = async (action, context) => {
|
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
|
|
|
ActionTypes.RefreshDatasource
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-25 22:50:51 +13:00
|
|
|
const handlerMap = {
|
|
|
|
["Save Row"]: saveRowHandler,
|
|
|
|
["Delete Row"]: deleteRowHandler,
|
|
|
|
["Navigate To"]: navigationHandler,
|
2021-01-05 07:57:16 +13:00
|
|
|
["Execute Query"]: queryExecutionHandler,
|
2021-01-09 06:25:06 +13:00
|
|
|
["Trigger Automation"]: triggerAutomationHandler,
|
2021-02-02 07:51:22 +13:00
|
|
|
["Validate Form"]: validateFormHandler,
|
2021-02-06 01:54:36 +13:00
|
|
|
["Refresh Datasource"]: refreshDatasourceHandler,
|
2020-11-25 22:50:51 +13: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-20 06:38:24 +13:00
|
|
|
// Prevent button actions in the builder preview
|
|
|
|
if (get(builderStore).inBuilder) {
|
|
|
|
return () => {}
|
|
|
|
}
|
2020-11-25 22:50:51 +13:00
|
|
|
const handlers = actions.map(def => handlerMap[def["##eventHandlerType"]])
|
|
|
|
return async () => {
|
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
2021-02-02 07:51:22 +13:00
|
|
|
try {
|
|
|
|
const result = await handlers[i](actions[i], context)
|
|
|
|
// A handler returning `false` is a flag to stop execution of handlers
|
|
|
|
if (result === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error while executing button handler")
|
|
|
|
console.error(error)
|
|
|
|
// Stop executing on an error
|
|
|
|
return
|
|
|
|
}
|
2020-11-25 22:50:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|