2020-02-03 10:24:25 +01:00
|
|
|
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"
|
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
export const eventHandlers = (store, rootPath, routeTo) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const handler = (parameters, execute) => ({
|
|
|
|
execute,
|
|
|
|
parameters,
|
|
|
|
})
|
|
|
|
|
|
|
|
const setStateWithStore = (path, value) => setState(store, path, value)
|
|
|
|
|
|
|
|
let currentState
|
2020-05-07 11:53:34 +02:00
|
|
|
store.subscribe(state => {
|
|
|
|
currentState = state
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const api = createApi({
|
2020-02-20 21:19:24 +01:00
|
|
|
rootPath,
|
2020-02-03 10:24:25 +01:00
|
|
|
setState: setStateWithStore,
|
|
|
|
getState: (path, fallback) => getState(currentState, path, fallback),
|
|
|
|
})
|
|
|
|
|
|
|
|
const setStateHandler = ({ path, value }) => setState(store, path, value)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"Set State": handler(["path", "value"], setStateHandler),
|
|
|
|
"Load Record": handler(["recordKey", "statePath"], api.loadRecord),
|
|
|
|
"List Records": handler(["indexKey", "statePath"], api.listRecords),
|
|
|
|
"Save Record": handler(["statePath"], api.saveRecord),
|
2020-02-21 15:44:48 +01:00
|
|
|
"Navigate To": handler(["url"], param => routeTo(param && param.url)),
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
Authenticate: handler(["username", "password"], api.authenticate),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isEventType = prop =>
|
|
|
|
isArray(prop) &&
|
|
|
|
prop.length > 0 &&
|
|
|
|
!isUndefined(prop[0][EVENT_TYPE_MEMBER_NAME])
|