2020-03-06 09:45:21 +00:00
|
|
|
import { WidgetBlueprint } from "reducers/entityReducers/widgetConfigReducer";
|
2020-04-14 05:35:16 +00:00
|
|
|
import { FlattenedWidgetProps } from "reducers/entityReducers/canvasWidgetsReducer";
|
|
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
2020-03-06 09:45:21 +00:00
|
|
|
import { generateReactKey } from "utils/generators";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { call } from "redux-saga/effects";
|
2021-03-04 05:24:47 +00:00
|
|
|
import { get } from "lodash";
|
2021-04-23 05:43:13 +00:00
|
|
|
import WidgetFactory from "utils/WidgetFactory";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
MAIN_CONTAINER_WIDGET_ID,
|
|
|
|
|
WidgetType,
|
|
|
|
|
} from "constants/WidgetConstants";
|
|
|
|
|
import WidgetConfigResponse from "mockResponses/WidgetConfigResponse";
|
|
|
|
|
import { Variant } from "components/ads/common";
|
|
|
|
|
import { Toaster } from "components/ads/Toast";
|
|
|
|
|
import { BlueprintOperationTypes } from "./WidgetBlueprintSagasEnums";
|
2020-03-06 09:45:21 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
function buildView(view: WidgetBlueprint["view"], widgetId: string) {
|
|
|
|
|
const children = [];
|
2020-09-16 10:28:01 +00:00
|
|
|
if (view) {
|
|
|
|
|
for (const template of view) {
|
|
|
|
|
//TODO(abhinav): Can we keep rows and size mandatory?
|
|
|
|
|
try {
|
|
|
|
|
children.push({
|
|
|
|
|
widgetId,
|
|
|
|
|
type: template.type,
|
|
|
|
|
leftColumn: template.position.left || 0,
|
|
|
|
|
topRow: template.position.top || 0,
|
|
|
|
|
columns: template.size && template.size.cols,
|
|
|
|
|
rows: template.size && template.size.rows,
|
|
|
|
|
newWidgetId: generateReactKey(),
|
|
|
|
|
props: template.props,
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2020-03-06 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-16 10:28:01 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
return children;
|
2020-03-06 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* buildWidgetBlueprint(
|
|
|
|
|
blueprint: WidgetBlueprint,
|
|
|
|
|
widgetId: string,
|
|
|
|
|
) {
|
2020-03-27 09:02:11 +00:00
|
|
|
const widgetProps = yield call(buildView, blueprint.view, widgetId);
|
|
|
|
|
return widgetProps;
|
2020-03-06 09:45:21 +00:00
|
|
|
}
|
2020-04-14 05:35:16 +00:00
|
|
|
|
|
|
|
|
export type UpdatePropertyArgs = {
|
|
|
|
|
widgetId: string;
|
|
|
|
|
propertyName: string;
|
|
|
|
|
propertyValue: any;
|
|
|
|
|
};
|
|
|
|
|
export type BlueprintOperationAddActionFn = () => void;
|
|
|
|
|
export type BlueprintOperationModifyPropsFn = (
|
|
|
|
|
widget: WidgetProps & { children?: WidgetProps[] },
|
2021-04-23 05:43:13 +00:00
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps },
|
2020-04-14 05:35:16 +00:00
|
|
|
parent?: WidgetProps,
|
|
|
|
|
) => UpdatePropertyArgs[] | undefined;
|
|
|
|
|
|
2021-04-23 05:43:13 +00:00
|
|
|
export interface ChildOperationFnResponse {
|
|
|
|
|
widgets: Record<string, FlattenedWidgetProps>;
|
|
|
|
|
message?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type BlueprintOperationChildOperationsFn = (
|
|
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps },
|
|
|
|
|
widgetId: string,
|
|
|
|
|
parentId: string,
|
|
|
|
|
widgetPropertyMaps: {
|
|
|
|
|
defaultPropertyMap: Record<string, string>;
|
|
|
|
|
},
|
|
|
|
|
) => ChildOperationFnResponse;
|
|
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
export type BlueprintOperationFunction =
|
|
|
|
|
| BlueprintOperationModifyPropsFn
|
2021-04-23 05:43:13 +00:00
|
|
|
| BlueprintOperationAddActionFn
|
|
|
|
|
| BlueprintOperationChildOperationsFn;
|
2020-04-14 05:35:16 +00:00
|
|
|
|
|
|
|
|
export type BlueprintOperationType = keyof typeof BlueprintOperationTypes;
|
|
|
|
|
|
|
|
|
|
export type BlueprintOperation = {
|
|
|
|
|
type: BlueprintOperationType;
|
|
|
|
|
fn: BlueprintOperationFunction;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function* executeWidgetBlueprintOperations(
|
|
|
|
|
operations: BlueprintOperation[],
|
|
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps },
|
|
|
|
|
widgetId: string,
|
|
|
|
|
) {
|
|
|
|
|
operations.forEach((operation: BlueprintOperation) => {
|
2021-04-23 05:43:13 +00:00
|
|
|
const widget: WidgetProps & { children?: string[] | WidgetProps[] } = {
|
|
|
|
|
...widgets[widgetId],
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
switch (operation.type) {
|
|
|
|
|
case BlueprintOperationTypes.MODIFY_PROPS:
|
|
|
|
|
if (widget.children && widget.children.length > 0) {
|
|
|
|
|
widget.children = (widget.children as string[]).map(
|
|
|
|
|
(childId: string) => widgets[childId],
|
|
|
|
|
) as WidgetProps[];
|
|
|
|
|
}
|
|
|
|
|
const updatePropertyPayloads:
|
|
|
|
|
| UpdatePropertyArgs[]
|
|
|
|
|
| undefined = (operation.fn as BlueprintOperationModifyPropsFn)(
|
|
|
|
|
widget as WidgetProps & { children?: WidgetProps[] },
|
2021-04-23 05:43:13 +00:00
|
|
|
widgets,
|
2021-03-04 05:24:47 +00:00
|
|
|
get(widgets, widget.parentId || "", undefined),
|
2020-04-14 05:35:16 +00:00
|
|
|
);
|
|
|
|
|
updatePropertyPayloads &&
|
|
|
|
|
updatePropertyPayloads.forEach((params: UpdatePropertyArgs) => {
|
|
|
|
|
widgets[params.widgetId][params.propertyName] =
|
|
|
|
|
params.propertyValue;
|
|
|
|
|
});
|
2021-04-23 05:43:13 +00:00
|
|
|
break;
|
2020-04-14 05:35:16 +00:00
|
|
|
}
|
|
|
|
|
});
|
2021-04-23 05:43:13 +00:00
|
|
|
|
2020-04-14 05:35:16 +00:00
|
|
|
return yield widgets;
|
|
|
|
|
}
|
2021-04-23 05:43:13 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* this saga executes the blueprint child operation
|
|
|
|
|
*
|
|
|
|
|
* @param parent
|
|
|
|
|
* @param newWidgetId
|
|
|
|
|
* @param widgets
|
|
|
|
|
*
|
|
|
|
|
* @returns { [widgetId: string]: FlattenedWidgetProps }
|
|
|
|
|
*/
|
|
|
|
|
export function* executeWidgetBlueprintChildOperations(
|
|
|
|
|
operation: BlueprintOperation,
|
|
|
|
|
canvasWidgets: { [widgetId: string]: FlattenedWidgetProps },
|
|
|
|
|
widgetId: string,
|
|
|
|
|
parentId: string,
|
|
|
|
|
) {
|
|
|
|
|
// TODO(abhinav): Special handling for child operaionts
|
|
|
|
|
// This needs to be deprecated soon
|
|
|
|
|
|
|
|
|
|
// Get the default properties map of the current widget
|
|
|
|
|
// The operation can handle things based on this map
|
|
|
|
|
// Little abstraction leak, but will be deprecated soon
|
|
|
|
|
const widgetPropertyMaps = {
|
|
|
|
|
defaultPropertyMap: WidgetFactory.getWidgetDefaultPropertiesMap(
|
|
|
|
|
canvasWidgets[widgetId].type as WidgetType,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
message,
|
2021-05-13 08:35:39 +00:00
|
|
|
widgets,
|
2021-04-23 05:43:13 +00:00
|
|
|
} = (operation.fn as BlueprintOperationChildOperationsFn)(
|
|
|
|
|
canvasWidgets,
|
|
|
|
|
widgetId,
|
|
|
|
|
parentId,
|
|
|
|
|
widgetPropertyMaps,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// If something odd happens show the message related to the odd scenario
|
|
|
|
|
if (message) {
|
|
|
|
|
Toaster.show({
|
|
|
|
|
text: message,
|
|
|
|
|
hideProgressBar: false,
|
|
|
|
|
variant: Variant.info,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flow returns to the usual from here.
|
|
|
|
|
return widgets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* this saga traverse the tree till we get
|
|
|
|
|
* to MAIN_CONTAINER_WIDGET_ID while travesring, if we find
|
|
|
|
|
* any widget which has CHILD_OPERATION, we will call the fn in it
|
|
|
|
|
*
|
|
|
|
|
* @param parent
|
|
|
|
|
* @param newWidgetId
|
|
|
|
|
* @param widgets
|
|
|
|
|
*
|
|
|
|
|
* @returns { [widgetId: string]: FlattenedWidgetProps }
|
|
|
|
|
*/
|
|
|
|
|
export function* traverseTreeAndExecuteBlueprintChildOperations(
|
|
|
|
|
parent: FlattenedWidgetProps,
|
|
|
|
|
newWidgetId: string,
|
|
|
|
|
widgets: { [widgetId: string]: FlattenedWidgetProps },
|
|
|
|
|
) {
|
|
|
|
|
let root = parent;
|
|
|
|
|
|
|
|
|
|
while (root.parentId && root.widgetId !== MAIN_CONTAINER_WIDGET_ID) {
|
|
|
|
|
const parentConfig = {
|
|
|
|
|
...(WidgetConfigResponse as any).config[root.type],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// find the blueprint with type CHILD_OPERATIONS
|
|
|
|
|
const blueprintChildOperation = get(
|
|
|
|
|
parentConfig,
|
|
|
|
|
"blueprint.operations",
|
|
|
|
|
[],
|
|
|
|
|
).find(
|
|
|
|
|
(operation: BlueprintOperation) =>
|
|
|
|
|
operation.type === BlueprintOperationTypes.CHILD_OPERATIONS,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// if there is blueprint operation with CHILD_OPERATION type, call the fn in it
|
|
|
|
|
if (blueprintChildOperation) {
|
|
|
|
|
const updatedWidgets:
|
|
|
|
|
| { [widgetId: string]: FlattenedWidgetProps }
|
|
|
|
|
| undefined = yield call(
|
|
|
|
|
executeWidgetBlueprintChildOperations,
|
|
|
|
|
blueprintChildOperation,
|
|
|
|
|
widgets,
|
|
|
|
|
newWidgetId,
|
|
|
|
|
root.widgetId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (updatedWidgets) {
|
|
|
|
|
widgets = updatedWidgets;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root = widgets[root.parentId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return widgets;
|
|
|
|
|
}
|