2021-01-19 18:38:24 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-07-25 13:09:50 +02:00
|
|
|
import {
|
|
|
|
routeStore,
|
|
|
|
builderStore,
|
|
|
|
confirmationStore,
|
|
|
|
authStore,
|
2021-07-30 15:01:01 +02:00
|
|
|
peekStore,
|
2021-08-26 12:28:44 +02:00
|
|
|
stateStore,
|
2021-07-25 13:09:50 +02:00
|
|
|
} 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-07-22 17:50:35 +02:00
|
|
|
const { fields, providerId, tableId } = action.parameters
|
2021-01-19 18:38:24 +01:00
|
|
|
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
|
|
|
}
|
2021-07-22 17:50:35 +02:00
|
|
|
if (tableId) {
|
|
|
|
draft.tableId = tableId
|
|
|
|
}
|
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-07-30 15:01:01 +02:00
|
|
|
const { url, peek } = action.parameters
|
2021-06-25 17:07:25 +02:00
|
|
|
if (url) {
|
2021-08-24 16:56:23 +02:00
|
|
|
// If we're already peeking, don't peek again
|
|
|
|
const isPeeking = get(routeStore).queryParams?.peek
|
|
|
|
if (peek && !isPeeking) {
|
2021-07-30 15:01:01 +02:00
|
|
|
peekStore.actions.showPeek(url)
|
2021-06-25 17:07:25 +02:00
|
|
|
} else {
|
2021-07-30 15:01:01 +02:00
|
|
|
const external = !url.startsWith("/")
|
|
|
|
if (external) {
|
|
|
|
window.location.href = url
|
|
|
|
} else {
|
|
|
|
routeStore.actions.navigate(action.parameters.url)
|
|
|
|
}
|
2021-06-25 17:07:25 +02:00
|
|
|
}
|
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 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-08-19 13:52:50 +02:00
|
|
|
const executeActionHandler = async (
|
|
|
|
context,
|
|
|
|
componentId,
|
|
|
|
actionType,
|
|
|
|
params
|
|
|
|
) => {
|
2021-02-05 13:54:36 +01:00
|
|
|
const fn = context[`${componentId}_${actionType}`]
|
2021-02-01 19:51:22 +01:00
|
|
|
if (fn) {
|
2021-08-19 13:52:50 +02:00
|
|
|
return await fn(params)
|
2021-02-01 19:51:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:54:36 +01:00
|
|
|
const validateFormHandler = async (action, context) => {
|
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
2021-08-19 13:52:50 +02:00
|
|
|
ActionTypes.ValidateForm,
|
|
|
|
action.parameters.onlyCurrentStep
|
2021-02-05 13:54:36 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshDatasourceHandler = async (action, context) => {
|
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
|
|
|
ActionTypes.RefreshDatasource
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-25 13:07:25 +02:00
|
|
|
const logoutHandler = async () => {
|
|
|
|
await authStore.actions.logOut()
|
|
|
|
}
|
|
|
|
|
2021-07-26 13:58:18 +02:00
|
|
|
const clearFormHandler = async (action, context) => {
|
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
|
|
|
ActionTypes.ClearForm
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-20 10:54:54 +02:00
|
|
|
const changeFormStepHandler = async (action, context) => {
|
2021-08-18 16:21:18 +02:00
|
|
|
return await executeActionHandler(
|
|
|
|
context,
|
|
|
|
action.parameters.componentId,
|
2021-08-20 10:54:54 +02:00
|
|
|
ActionTypes.ChangeFormStep,
|
|
|
|
action.parameters
|
2021-08-18 16:21:18 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-02 16:50:59 +02:00
|
|
|
const closeScreenModalHandler = () => {
|
2021-08-02 16:12:38 +02:00
|
|
|
// Emit this as a window event, so parent screens which are iframing us in
|
|
|
|
// can close the modal
|
2021-08-02 16:50:59 +02:00
|
|
|
window.dispatchEvent(new Event("close-screen-modal"))
|
2021-08-02 16:12:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 12:28:44 +02:00
|
|
|
const updateStateHandler = action => {
|
|
|
|
const { type, key, value } = action.parameters
|
|
|
|
if (type === "set") {
|
|
|
|
stateStore.actions.setValue(key, value)
|
|
|
|
} else if (type === "delete") {
|
|
|
|
stateStore.actions.deleteValue(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-25 13:07:25 +02:00
|
|
|
["Log Out"]: logoutHandler,
|
2021-07-26 15:22:14 +02:00
|
|
|
["Clear Form"]: clearFormHandler,
|
2021-08-02 16:50:59 +02:00
|
|
|
["Close Screen Modal"]: closeScreenModalHandler,
|
2021-08-20 10:54:54 +02:00
|
|
|
["Change Form Step"]: changeFormStepHandler,
|
2021-08-26 12:28:44 +02:00
|
|
|
["Update State"]: updateStateHandler,
|
2021-06-21 10:56:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const confirmTextMap = {
|
|
|
|
["Delete Row"]: "Are you sure you want to delete this row?",
|
|
|
|
["Save Row"]: "Are you sure you want to save this row?",
|
|
|
|
["Execute Query"]: "Are you sure you want to execute this query?",
|
|
|
|
["Trigger Automation"]: "Are you sure you want to trigger this automation?",
|
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.
|
2021-06-21 10:56:46 +02:00
|
|
|
* A handler returning `false` is a flag to stop execution of handlers
|
2020-11-25 10:50:51 +01:00
|
|
|
*/
|
|
|
|
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 {
|
2021-06-21 10:56:46 +02:00
|
|
|
const action = actions[i]
|
|
|
|
const callback = async () => handlers[i](action, context)
|
|
|
|
|
|
|
|
// If this action is confirmable, show confirmation and await a
|
|
|
|
// callback to execute further actions
|
|
|
|
if (action.parameters?.confirm) {
|
|
|
|
const defaultText = confirmTextMap[action["##eventHandlerType"]]
|
|
|
|
const confirmText = action.parameters?.confirmText || defaultText
|
2021-06-21 11:46:55 +02:00
|
|
|
confirmationStore.actions.showConfirmation(
|
|
|
|
action["##eventHandlerType"],
|
|
|
|
confirmText,
|
|
|
|
async () => {
|
|
|
|
// When confirmed, execute this action immediately,
|
|
|
|
// then execute the rest of the actions in the chain
|
|
|
|
const result = await callback()
|
|
|
|
if (result !== false) {
|
|
|
|
const next = enrichButtonActions(actions.slice(i + 1), context)
|
|
|
|
await next()
|
|
|
|
}
|
2021-06-21 10:56:46 +02:00
|
|
|
}
|
2021-06-21 11:46:55 +02:00
|
|
|
)
|
2021-06-21 10:56:46 +02:00
|
|
|
|
2021-06-21 11:11:18 +02:00
|
|
|
// Stop enriching actions when encountering a confirmable action,
|
2021-06-21 10:56:46 +02:00
|
|
|
// as the callback continues the action chain
|
2021-02-01 19:51:22 +01:00
|
|
|
return
|
|
|
|
}
|
2021-06-21 10:56:46 +02:00
|
|
|
|
2021-06-21 11:11:18 +02:00
|
|
|
// For non-confirmable actions, execute the handler immediately
|
2021-06-21 10:56:46 +02:00
|
|
|
else {
|
|
|
|
const result = await callback()
|
|
|
|
if (result === false) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 19:51:22 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error while executing button handler")
|
|
|
|
console.error(error)
|
2021-06-21 10:56:46 +02:00
|
|
|
// Stop executing further actions on error
|
2021-02-01 19:51:22 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|