2021-01-19 18:38:24 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-02-26 10:57:06 +01:00
|
|
|
import { routeStore, builderStore, authStore } from "../store"
|
2021-01-15 14:11:51 +01:00
|
|
|
import { saveRow, deleteRow, executeQuery, triggerAutomation } from "../api"
|
2021-02-01 19:51:22 +01:00
|
|
|
import { ActionTypes } from "../constants"
|
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) {
|
2021-02-02 15:32:58 +01:00
|
|
|
let draft = context[providerId]
|
2021-01-19 18:38:24 +01:00
|
|
|
if (fields) {
|
2021-02-18 18:44:56 +01:00
|
|
|
for (let [field, value] of Object.entries(fields)) {
|
|
|
|
draft[field] = value
|
2021-01-21 12:31:45 +01:00
|
|
|
}
|
2021-01-19 18:38:24 +01:00
|
|
|
}
|
|
|
|
await saveRow(draft)
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
const deleteRowHandler = async action => {
|
2020-11-25 10:50:51 +01:00
|
|
|
const { tableId, revId, rowId } = action.parameters
|
2021-01-19 18:38:24 +01:00
|
|
|
if (tableId && revId && rowId) {
|
2021-02-04 20:18:32 +01:00
|
|
|
await deleteRow({ tableId, rowId, revId })
|
2021-01-19 18:38:24 +01:00
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
const triggerAutomationHandler = async action => {
|
2021-02-04 14:01:49 +01:00
|
|
|
const { fields } = action.parameters
|
2021-01-21 12:31:45 +01:00
|
|
|
if (fields) {
|
2021-02-18 18:44:56 +01:00
|
|
|
await triggerAutomation(action.parameters.automationId, fields)
|
2021-01-08 18:25:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02: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-05-04 12:32:22 +02:00
|
|
|
const queryExecutionHandler = async action => {
|
2021-01-08 13:06:37 +01:00
|
|
|
const { datasourceId, queryId, queryParams } = action.parameters
|
2021-01-08 19:22:03 +01:00
|
|
|
await executeQuery({
|
|
|
|
datasourceId,
|
|
|
|
queryId,
|
2021-02-03 15:53:13 +01:00
|
|
|
parameters: queryParams,
|
2021-01-08 19:22:03 +01:00
|
|
|
})
|
2021-01-04 19:57:16 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:54:36 +01:00
|
|
|
const executeActionHandler = async (context, componentId, actionType) => {
|
|
|
|
const fn = context[`${componentId}_${actionType}`]
|
2021-02-01 19:51:22 +01:00
|
|
|
if (fn) {
|
|
|
|
return await fn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:54:36 +01: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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
const loginHandler = async action => {
|
2021-02-26 10:57:06 +01:00
|
|
|
const { email, password } = action.parameters
|
|
|
|
await authStore.actions.logIn({ email, password })
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:04:12 +01:00
|
|
|
const logoutHandler = async () => {
|
|
|
|
await authStore.actions.logOut()
|
|
|
|
}
|
|
|
|
|
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,
|
2021-02-01 19:51:22 +01:00
|
|
|
["Validate Form"]: validateFormHandler,
|
2021-02-05 13:54:36 +01:00
|
|
|
["Refresh Datasource"]: refreshDatasourceHandler,
|
2021-02-26 10:57:06 +01:00
|
|
|
["Log In"]: loginHandler,
|
2021-02-26 15:04:12 +01:00
|
|
|
["Log Out"]: logoutHandler,
|
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 () => {}
|
|
|
|
}
|
2021-05-04 12:32:22 +02:00
|
|
|
const handlers = actions.map(def => handlerMap[def["##eventHandlerType"]])
|
2020-11-25 10:50:51 +01:00
|
|
|
return async () => {
|
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
2021-02-01 19:51:22 +01: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 10:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|