budibase/packages/client/src/state/eventHandlers.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-06 22:12:35 +02:00
import api from "../api"
2020-09-10 22:11:05 +02:00
import renderTemplateString from "./renderTemplateString"
export const EVENT_TYPE_MEMBER_NAME = "##eventHandlerType"
export const eventHandlers = routeTo => {
2020-09-10 22:11:05 +02:00
const handlers = {
"Navigate To": param => routeTo(param && param.url),
"Create Record": api.createRecord,
"Update Record": api.updateRecord,
"Trigger Workflow": api.triggerWorkflow,
}
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
for (let action of actions) {
const handler = handlers[action[EVENT_TYPE_MEMBER_NAME]]
const parameters = createParameters(action.parameters, state)
if (handler) {
await handler(parameters)
}
}
}
2020-09-10 22:11:05 +02:00
return runEventActions
}
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
}