2020-03-19 03:25:52 +00:00
|
|
|
import {
|
|
|
|
|
ActionData,
|
|
|
|
|
ActionDataState,
|
|
|
|
|
} from "reducers/entityReducers/actionsReducer";
|
2020-02-18 10:41:52 +00:00
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { ActionResponse } from "api/ActionAPI";
|
2020-03-19 03:25:52 +00:00
|
|
|
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
|
|
|
|
|
import { MetaState } from "reducers/entityReducers/metaReducer";
|
2020-02-18 10:41:52 +00:00
|
|
|
|
|
|
|
|
export type ActionDescription<T> = {
|
|
|
|
|
type: string;
|
|
|
|
|
payload: T;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ActionDispatcher<T, A extends string[]> = (
|
|
|
|
|
...args: A
|
|
|
|
|
) => ActionDescription<T>;
|
|
|
|
|
|
|
|
|
|
export enum ENTITY_TYPE {
|
|
|
|
|
ACTION = "ACTION",
|
|
|
|
|
WIDGET = "WIDGET",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type RunActionPayload = {
|
|
|
|
|
actionId: string;
|
|
|
|
|
onSuccess: string;
|
|
|
|
|
onError: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export interface DataTreeAction extends Omit<ActionData, "data"> {
|
|
|
|
|
data: ActionResponse["body"];
|
|
|
|
|
run: ActionDispatcher<RunActionPayload, [string, string]>;
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.ACTION;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 08:09:57 +00:00
|
|
|
export interface DataTreeUrl {
|
|
|
|
|
queryParams: Record<string, string>;
|
|
|
|
|
protocol: string;
|
|
|
|
|
host: string;
|
|
|
|
|
hostname: string;
|
|
|
|
|
port: string;
|
|
|
|
|
pathname: string;
|
|
|
|
|
hash: string;
|
|
|
|
|
href: string;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export interface DataTreeWidget extends WidgetProps {
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.WIDGET;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 12:44:56 +00:00
|
|
|
export type DataTreeEntity =
|
|
|
|
|
| DataTreeAction
|
|
|
|
|
| DataTreeWidget
|
2020-04-01 08:09:57 +00:00
|
|
|
| DataTreeUrl
|
2020-02-26 12:44:56 +00:00
|
|
|
| ActionDispatcher<any, any>;
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export type DataTree = {
|
2020-02-26 12:44:56 +00:00
|
|
|
[entityName: string]: DataTreeEntity;
|
2020-02-18 10:41:52 +00:00
|
|
|
} & { actionPaths?: string[] };
|
|
|
|
|
|
2020-03-19 03:25:52 +00:00
|
|
|
type DataTreeSeed = {
|
|
|
|
|
actions: ActionDataState;
|
|
|
|
|
widgets: CanvasWidgetsReduxState;
|
|
|
|
|
widgetsMeta: MetaState;
|
2020-04-07 08:01:34 +00:00
|
|
|
url?: DataTreeUrl;
|
2020-03-19 03:25:52 +00:00
|
|
|
};
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
export class DataTreeFactory {
|
2020-04-07 08:01:34 +00:00
|
|
|
static create({ actions, widgets, widgetsMeta }: DataTreeSeed): DataTree {
|
2020-02-18 10:41:52 +00:00
|
|
|
const dataTree: DataTree = {};
|
2020-03-27 09:02:11 +00:00
|
|
|
dataTree.actionPaths = [
|
|
|
|
|
"navigateTo",
|
|
|
|
|
"navigateToUrl",
|
|
|
|
|
"showAlert",
|
|
|
|
|
"showModal",
|
|
|
|
|
"closeModal",
|
|
|
|
|
];
|
2020-03-19 03:25:52 +00:00
|
|
|
actions.forEach(a => {
|
2020-02-18 10:41:52 +00:00
|
|
|
dataTree[a.config.name] = {
|
|
|
|
|
...a,
|
|
|
|
|
data: a.data ? a.data.body : {},
|
|
|
|
|
run: function(onSuccess: string, onError: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "RUN_ACTION",
|
|
|
|
|
payload: {
|
|
|
|
|
actionId: this.config.id,
|
|
|
|
|
onSuccess: onSuccess ? `{{${onSuccess.toString()}}}` : "",
|
|
|
|
|
onError: onError ? `{{${onError.toString()}}}` : "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.ACTION,
|
|
|
|
|
};
|
|
|
|
|
dataTree.actionPaths && dataTree.actionPaths.push(`${a.config.name}.run`);
|
|
|
|
|
});
|
2020-03-19 03:25:52 +00:00
|
|
|
Object.keys(widgets).forEach(w => {
|
|
|
|
|
const widget = widgets[w];
|
|
|
|
|
const widgetMetaProps = widgetsMeta[w];
|
2020-02-18 10:41:52 +00:00
|
|
|
dataTree[widget.widgetName] = {
|
|
|
|
|
...widget,
|
|
|
|
|
...widgetMetaProps,
|
|
|
|
|
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
|
|
|
|
|
};
|
|
|
|
|
});
|
2020-04-01 08:09:57 +00:00
|
|
|
dataTree.navigateTo = function(pageName: string, params: object) {
|
2020-02-18 10:41:52 +00:00
|
|
|
return {
|
|
|
|
|
type: "NAVIGATE_TO",
|
2020-04-01 08:09:57 +00:00
|
|
|
payload: { pageName, params },
|
2020-02-18 10:41:52 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dataTree.navigateToUrl = function(url: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "NAVIGATE_TO_URL",
|
|
|
|
|
payload: { url },
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dataTree.showAlert = function(message: string, style: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "SHOW_ALERT",
|
|
|
|
|
payload: { message, style },
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-07 08:01:34 +00:00
|
|
|
// dataTree.url = url;
|
2020-03-27 09:02:11 +00:00
|
|
|
dataTree.showModal = function(modalName: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "SHOW_MODAL_BY_NAME",
|
|
|
|
|
payload: { modalName },
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
dataTree.closeModal = function(modalName: string) {
|
|
|
|
|
return {
|
|
|
|
|
type: "CLOSE_MODAL",
|
|
|
|
|
payload: { modalName },
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-18 10:41:52 +00:00
|
|
|
return dataTree;
|
|
|
|
|
}
|
|
|
|
|
}
|