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

161 lines
4.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 } from "entities/Action";
import { AppDataState } from "reducers/entityReducers/appReducer";
import _ from "lodash";
import {
DynamicPath,
getEntityDynamicBindingPathList,
} from "../../utils/DynamicBindingUtils";
2020-02-18 10:41:52 +00:00
export type ActionDescription<T> = {
type: string;
payload: T;
};
export type ActionDispatcher<T, A extends string[]> = (
2020-02-18 10:41:52 +00:00
...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]>
| Record<string, any>;
dynamicBindingPathList: DynamicPath[];
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;
store: Record<string, unknown>;
}
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 {
static create({
actions,
widgets,
widgetsMeta,
pageList,
appData,
}: DataTreeSeed): DataTree {
2020-02-18 10:41:52 +00:00
const dataTree: DataTree = {};
actions.forEach(action => {
let dynamicBindingPathList: DynamicPath[] = [];
2020-06-04 13:49:22 +00:00
// update paths
if (
action.config.dynamicBindingPathList &&
action.config.dynamicBindingPathList.length
2020-06-04 13:49:22 +00:00
) {
dynamicBindingPathList = action.config.dynamicBindingPathList.map(
d => ({
...d,
key: `config.${d.key}`,
}),
);
2020-06-04 13:49:22 +00:00
}
dataTree[action.config.name] = {
run: {},
actionId: action.config.id,
name: action.config.name,
pluginType: action.config.pluginType,
config: action.config.actionConfiguration,
2020-06-04 13:49:22 +00:00
dynamicBindingPathList,
data: action.data ? action.data.body : {},
2020-02-18 10:41:52 +00:00
ENTITY_TYPE: ENTITY_TYPE.ACTION,
isLoading: action.isLoading,
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 dynamicBindingPathList = getEntityDynamicBindingPathList(widget);
dynamicBindingPathList.forEach(dynamicPath => {
const propertyPath = dynamicPath.key;
const propertyValue = _.get(widget, propertyPath);
if (_.isObject(propertyValue)) {
// Stringify this because composite controls may have bindings in the sub controls
_.set(widget, propertyPath, JSON.stringify(propertyValue));
}
});
2020-04-17 16:15:09 +00:00
Object.keys(derivedPropertyMap).forEach(propertyName => {
derivedProps[propertyName] = derivedPropertyMap[propertyName].replace(
/this./g,
`${widget.widgetName}.`,
);
dynamicBindingPathList.push({
key: propertyName,
});
2020-04-17 16:15:09 +00:00
});
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,
dynamicBindingPathList,
2020-02-18 10:41:52 +00:00
ENTITY_TYPE: ENTITY_TYPE.WIDGET,
};
});
2020-06-19 13:06:45 +00:00
2020-04-20 05:42:46 +00:00
dataTree.pageList = pageList;
dataTree.appsmith = { ...appData } as DataTreeAppsmith;
(dataTree.appsmith as DataTreeAppsmith).ENTITY_TYPE = ENTITY_TYPE.APPSMITH;
2020-02-18 10:41:52 +00:00
return dataTree;
}
}