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

39 lines
979 B
JavaScript
Raw Normal View History

import { setState } from "./setState"
import { getState } from "./getState"
import { isArray, isUndefined } from "lodash/fp"
import { createApi } from "../api"
export const EVENT_TYPE_MEMBER_NAME = "##eventHandlerType"
export const eventHandlers = (store, rootPath, routeTo) => {
const handler = (parameters, execute) => ({
execute,
parameters,
})
let currentState
2020-05-07 11:53:34 +02:00
store.subscribe(state => {
currentState = state
})
const api = createApi({
2020-02-20 21:19:24 +01:00
rootPath,
2020-05-30 01:14:41 +02:00
setState,
2020-06-01 11:41:28 +02:00
getState: (path, fallback) => getState(path, fallback),
})
2020-05-30 01:14:41 +02:00
const setStateHandler = ({ path, value }) => setState(path, value)
return {
"Set State": handler(["path", "value"], setStateHandler),
"Navigate To": handler(["url"], param => routeTo(param && param.url)),
2020-06-01 11:41:28 +02:00
"Trigger Workflow": handler(["workflow"], api.triggerWorkflow),
}
}
export const isEventType = prop =>
isArray(prop) &&
prop.length > 0 &&
!isUndefined(prop[0][EVENT_TYPE_MEMBER_NAME])