PromucFlow_constructor/app/client/src/reducers/entityReducers/canvasWidgetsReducer.tsx

63 lines
2.0 KiB
TypeScript
Raw Normal View History

import { createImmerReducer } from "utils/AppsmithUtils";
import {
ReduxActionTypes,
2019-09-24 12:36:03 +00:00
UpdateCanvasPayload,
ReduxAction,
2019-11-25 05:07:27 +00:00
} from "constants/ReduxActionConstants";
import { WidgetProps } from "widgets/BaseWidget";
import { UpdateWidgetPropertyPayload } from "actions/controlActions";
import { set, uniqBy } from "lodash";
const initialState: CanvasWidgetsReduxState = {};
2019-08-26 12:41:21 +00:00
export type FlattenedWidgetProps = WidgetProps & {
2019-08-29 11:22:09 +00:00
children?: string[];
};
const canvasWidgetsReducer = createImmerReducer(initialState, {
[ReduxActionTypes.INIT_CANVAS_LAYOUT]: (
2019-03-21 17:42:23 +00:00
state: CanvasWidgetsReduxState,
2019-09-24 12:36:03 +00:00
action: ReduxAction<UpdateCanvasPayload>,
2019-03-21 17:42:23 +00:00
) => {
return action.payload.widgets;
2019-08-26 12:41:21 +00:00
},
[ReduxActionTypes.UPDATE_LAYOUT]: (
2019-08-26 12:41:21 +00:00
state: CanvasWidgetsReduxState,
2019-09-24 12:36:03 +00:00
action: ReduxAction<UpdateCanvasPayload>,
2019-08-26 12:41:21 +00:00
) => {
return action.payload.widgets;
},
[ReduxActionTypes.UPDATE_WIDGET_PROPERTY]: (
state: CanvasWidgetsReduxState,
action: ReduxAction<UpdateWidgetPropertyPayload>,
) => {
const { dynamicUpdates, updates, widgetId } = action.payload;
// We loop over all updates
Object.entries(updates).forEach(([propertyPath, propertyValue]) => {
// since property paths could be nested, we use lodash set method
set(state[widgetId], propertyPath, propertyValue);
});
if (dynamicUpdates && dynamicUpdates.dynamicBindingPathList.length) {
const currentList = state[widgetId].dynamicBindingPathList || [];
state[widgetId].dynamicBindingPathList = uniqBy(
[...currentList, ...dynamicUpdates.dynamicBindingPathList],
"key",
);
}
if (dynamicUpdates && dynamicUpdates.dynamicTriggerPathList.length) {
const currentList = state[widgetId].dynamicTriggerPathList || [];
state[widgetId].dynamicTriggerPathList = uniqBy(
[...currentList, ...dynamicUpdates.dynamicTriggerPathList],
"key",
);
}
},
});
export interface CanvasWidgetsReduxState {
[widgetId: string]: FlattenedWidgetProps;
}
export default canvasWidgetsReducer;