PromucFlow_constructor/app/client/src/entities/DataTree/dataTreeFactory.ts

202 lines
5.6 KiB
TypeScript
Raw Normal View History

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";
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
import { MetaState } from "reducers/entityReducers/metaReducer";
2020-04-20 05:42:46 +00:00
import { PageListPayload } from "constants/ReduxActionConstants";
2020-04-17 16:15:09 +00:00
import WidgetFactory from "utils/WidgetFactory";
import { ActionConfig, PluginType, Property } from "entities/Action";
import { AppDataState } from "reducers/entityReducers/appReducer";
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",
APPSMITH = "APPSMITH",
2020-02-18 10:41:52 +00:00
}
export type RunActionPayload = {
actionId: string;
onSuccess: string;
onError: string;
params: Record<string, any>;
2020-02-18 10:41:52 +00:00
};
export interface DataTreeAction extends Omit<ActionData, "data" | "config"> {
2020-02-18 10:41:52 +00:00
data: ActionResponse["body"];
actionId: string;
config: Partial<ActionConfig>;
pluginType: PluginType;
name: string;
run: ActionDispatcher<RunActionPayload, [string, string, string]> | {};
2020-06-04 13:49:22 +00:00
dynamicBindingPathList: Property[];
2020-02-18 10:41:52 +00:00
ENTITY_TYPE: ENTITY_TYPE.ACTION;
}
export interface DataTreeWidget extends WidgetProps {
ENTITY_TYPE: ENTITY_TYPE.WIDGET;
}
export interface DataTreeAppsmith extends AppDataState {
ENTITY_TYPE: ENTITY_TYPE.APPSMITH;
}
export type DataTreeEntity =
| DataTreeAction
| DataTreeWidget
2020-04-20 05:42:46 +00:00
| PageListPayload
| DataTreeAppsmith
| ActionDispatcher<any, any>;
2020-02-18 10:41:52 +00:00
export type DataTree = {
[entityName: string]: DataTreeEntity;
2020-02-18 10:41:52 +00:00
} & { actionPaths?: string[] };
type DataTreeSeed = {
actions: ActionDataState;
widgets: CanvasWidgetsReduxState;
widgetsMeta: MetaState;
2020-04-20 05:42:46 +00:00
pageList: PageListPayload;
appData: AppDataState;
};
2020-02-18 10:41:52 +00:00
export class DataTreeFactory {
2020-06-19 13:06:45 +00:00
static create(
{ actions, widgets, widgetsMeta, pageList, appData }: DataTreeSeed,
2020-06-19 13:06:45 +00:00
// TODO(hetu)
// temporary fix for not getting functions while normal evals which crashes the app
// need to remove this after we get a proper solve
withFunctions?: boolean,
): DataTree {
2020-02-18 10:41:52 +00:00
const dataTree: DataTree = {};
2020-06-19 13:06:45 +00:00
const actionPaths = [];
actions.forEach(a => {
const config = a.config;
2020-06-04 13:49:22 +00:00
let dynamicBindingPathList: Property[] = [];
// update paths
if (
config.dynamicBindingPathList &&
config.dynamicBindingPathList.length
) {
dynamicBindingPathList = config.dynamicBindingPathList.map(d => ({
...d,
key: `config.${d.key}`,
}));
}
dataTree[config.name] = {
2020-02-18 10:41:52 +00:00
...a,
actionId: config.id,
name: config.name,
pluginType: config.pluginType,
config: config.actionConfiguration,
2020-06-04 13:49:22 +00:00
dynamicBindingPathList,
2020-02-18 10:41:52 +00:00
data: a.data ? a.data.body : {},
2020-06-19 13:06:45 +00:00
run: withFunctions
? function(
this: DataTreeAction,
onSuccess: string,
onError: string,
params = "",
) {
2020-06-19 13:06:45 +00:00
return {
type: "RUN_ACTION",
payload: {
actionId: this.actionId,
2020-06-19 13:06:45 +00:00
onSuccess: onSuccess ? `{{${onSuccess.toString()}}}` : "",
onError: onError ? `{{${onError.toString()}}}` : "",
params,
2020-06-19 13:06:45 +00:00
},
};
}
: {},
2020-02-18 10:41:52 +00:00
ENTITY_TYPE: ENTITY_TYPE.ACTION,
};
2020-06-19 13:06:45 +00:00
if (withFunctions) {
actionPaths.push(`${config.name}.run`);
}
dataTree.actionPaths && dataTree.actionPaths.push();
2020-02-18 10:41:52 +00:00
});
Object.keys(widgets).forEach(w => {
const widget = widgets[w];
const widgetMetaProps = widgetsMeta[w];
2020-04-17 16:15:09 +00:00
const defaultMetaProps = WidgetFactory.getWidgetMetaPropertiesMap(
widget.type,
);
const derivedPropertyMap = WidgetFactory.getWidgetDerivedPropertiesMap(
widget.type,
);
const derivedProps: any = {};
const dynamicBindings = widget.dynamicBindings || {};
Object.keys(derivedPropertyMap).forEach(propertyName => {
derivedProps[propertyName] = derivedPropertyMap[propertyName].replace(
/this./g,
`${widget.widgetName}.`,
);
dynamicBindings[propertyName] = true;
});
2020-02-18 10:41:52 +00:00
dataTree[widget.widgetName] = {
...widget,
2020-04-17 16:15:09 +00:00
...defaultMetaProps,
2020-02-18 10:41:52 +00:00
...widgetMetaProps,
2020-04-17 16:15:09 +00:00
...derivedProps,
dynamicBindings,
2020-02-18 10:41:52 +00:00
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
};
});
2020-06-19 13:06:45 +00:00
if (withFunctions) {
dataTree.navigateTo = function(pageNameOrUrl: string, params: object) {
return {
type: "NAVIGATE_TO",
payload: { pageNameOrUrl, params },
};
2020-02-18 10:41:52 +00:00
};
2020-06-19 13:06:45 +00:00
actionPaths.push("navigateTo");
2020-02-18 10:41:52 +00:00
2020-06-19 13:06:45 +00:00
dataTree.showAlert = function(message: string, style: string) {
return {
type: "SHOW_ALERT",
payload: { message, style },
};
2020-02-18 10:41:52 +00:00
};
2020-06-19 13:06:45 +00:00
actionPaths.push("showAlert");
2020-02-18 10:41:52 +00:00
2020-06-19 13:06:45 +00:00
// dataTree.url = url;
dataTree.showModal = function(modalName: string) {
return {
type: "SHOW_MODAL_BY_NAME",
payload: { modalName },
};
};
2020-06-19 13:06:45 +00:00
actionPaths.push("showModal");
dataTree.closeModal = function(modalName: string) {
return {
type: "CLOSE_MODAL",
payload: { modalName },
};
};
2020-06-19 13:06:45 +00:00
actionPaths.push("closeModal");
}
2020-04-20 05:42:46 +00:00
dataTree.pageList = pageList;
2020-06-19 13:06:45 +00:00
dataTree.actionPaths = actionPaths;
dataTree.appsmith = {
ENTITY_TYPE: ENTITY_TYPE.APPSMITH,
...appData,
};
2020-02-18 10:41:52 +00:00
return dataTree;
}
}