2020-10-02 15:52:45 +02:00
|
|
|
import api from "../api"
|
2020-09-10 22:11:05 +02:00
|
|
|
import renderTemplateString from "./renderTemplateString"
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
export const EVENT_TYPE_MEMBER_NAME = "##eventHandlerType"
|
|
|
|
|
2020-06-12 21:42:55 +02:00
|
|
|
export const eventHandlers = routeTo => {
|
2020-09-10 22:11:05 +02:00
|
|
|
const handlers = {
|
|
|
|
"Navigate To": param => routeTo(param && param.url),
|
2020-10-13 10:58:08 +02:00
|
|
|
"Update Row": api.updateRow,
|
|
|
|
"Save Row": api.saveRow,
|
2020-10-02 15:52:45 +02:00
|
|
|
"Trigger Workflow": api.triggerWorkflow,
|
2020-09-10 22:11:05 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
|
2020-09-10 22:11:05 +02:00
|
|
|
// when an event is called, this is what gets run
|
|
|
|
const runEventActions = async (actions, state) => {
|
|
|
|
if (!actions) return
|
2020-09-11 10:53:02 +02:00
|
|
|
// calls event handlers sequentially
|
2020-09-10 22:11:05 +02:00
|
|
|
for (let action of actions) {
|
|
|
|
const handler = handlers[action[EVENT_TYPE_MEMBER_NAME]]
|
|
|
|
const parameters = createParameters(action.parameters, state)
|
|
|
|
if (handler) {
|
2020-10-08 23:06:44 +02:00
|
|
|
await handler(parameters, state)
|
2020-09-10 22:11:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2020-09-10 22:11:05 +02:00
|
|
|
|
|
|
|
return runEventActions
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
2020-09-11 10:53:02 +02:00
|
|
|
// this will take a parameters obj, iterate all keys, and do a mustache render
|
|
|
|
// for every string. It will work recursively if it encounnters an {}
|
2020-09-10 22:11:05 +02:00
|
|
|
const createParameters = (parameterTemplateObj, state) => {
|
|
|
|
const parameters = {}
|
|
|
|
for (let key in parameterTemplateObj) {
|
|
|
|
if (typeof parameterTemplateObj[key] === "string") {
|
|
|
|
parameters[key] = renderTemplateString(parameterTemplateObj[key], state)
|
|
|
|
} else if (typeof parameterTemplateObj[key] === "object") {
|
|
|
|
parameters[key] = createParameters(parameterTemplateObj[key], state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parameters
|
|
|
|
}
|