2021-01-04 19:57:16 +01:00
|
|
|
import { enrichDataBinding, enrichDataBindings } from "./enrichDataBinding"
|
2020-11-25 10:50:51 +01:00
|
|
|
import { routeStore } from "../store"
|
2021-01-04 19:57:16 +01:00
|
|
|
import { saveRow, deleteRow, executeQuery } from "../api"
|
2020-11-25 10:50:51 +01:00
|
|
|
|
|
|
|
const saveRowHandler = async (action, context) => {
|
|
|
|
let draft = context[`${action.parameters.contextPath}_draft`]
|
|
|
|
if (action.parameters.fields) {
|
|
|
|
Object.entries(action.parameters.fields).forEach(([key, entry]) => {
|
|
|
|
draft[key] = enrichDataBinding(entry.value, context)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await saveRow(draft)
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteRowHandler = async (action, context) => {
|
|
|
|
const { tableId, revId, rowId } = action.parameters
|
|
|
|
await deleteRow({
|
|
|
|
tableId: enrichDataBinding(tableId, context),
|
|
|
|
rowId: enrichDataBinding(rowId, context),
|
|
|
|
revId: enrichDataBinding(revId, context),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const navigationHandler = action => {
|
|
|
|
routeStore.actions.navigate(action.parameters.url)
|
|
|
|
}
|
|
|
|
|
2021-01-04 19:57:16 +01:00
|
|
|
const queryExecutionHandler = async (action, context) => {
|
2021-01-08 13:06:37 +01:00
|
|
|
const { datasourceId, queryId, queryParams } = action.parameters
|
|
|
|
|
2021-01-04 19:57:16 +01:00
|
|
|
// TODO: allow context based bindings for query params
|
2021-01-08 13:06:37 +01:00
|
|
|
const enrichedQueryParameters = enrichDataBindings(queryParams, context)
|
|
|
|
|
|
|
|
await executeQuery({ datasourceId, queryId, params: enrichedQueryParameters })
|
2021-01-04 19:57:16 +01:00
|
|
|
}
|
|
|
|
|
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,
|
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) => {
|
|
|
|
const handlers = actions.map(def => handlerMap[def["##eventHandlerType"]])
|
|
|
|
return async () => {
|
|
|
|
for (let i = 0; i < handlers.length; i++) {
|
|
|
|
await handlers[i](actions[i], context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|