2020-09-30 12:42:09 +00:00
|
|
|
import { createImmerReducer } from "utils/AppsmithUtils";
|
2019-02-10 13:06:05 +00:00
|
|
|
import {
|
2019-09-12 11:19:38 +00:00
|
|
|
ReduxActionTypes,
|
2019-09-24 12:36:03 +00:00
|
|
|
UpdateCanvasPayload,
|
2019-09-09 10:30:22 +00:00
|
|
|
ReduxAction,
|
2019-11-25 05:07:27 +00:00
|
|
|
} from "constants/ReduxActionConstants";
|
|
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { UpdateWidgetPropertyPayload } from "actions/controlActions";
|
2021-02-25 15:12:29 +00:00
|
|
|
import { set, uniqBy } from "lodash";
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-09-09 10:30:22 +00:00
|
|
|
const initialState: CanvasWidgetsReduxState = {};
|
2019-08-26 12:41:21 +00:00
|
|
|
|
2020-09-16 10:28:01 +00:00
|
|
|
export type FlattenedWidgetProps = WidgetProps & {
|
2019-08-29 11:22:09 +00:00
|
|
|
children?: string[];
|
2019-09-19 22:25:37 +00:00
|
|
|
};
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2020-09-30 12:42:09 +00:00
|
|
|
const canvasWidgetsReducer = createImmerReducer(initialState, {
|
2021-01-25 08:57:26 +00:00
|
|
|
[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
|
|
|
) => {
|
2020-09-30 12:42:09 +00:00
|
|
|
return action.payload.widgets;
|
2019-08-26 12:41:21 +00:00
|
|
|
},
|
2019-09-19 22:25:37 +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
|
|
|
) => {
|
2020-09-30 12:42:09 +00:00
|
|
|
return action.payload.widgets;
|
2019-09-09 10:30:22 +00:00
|
|
|
},
|
2019-09-18 10:19:50 +00:00
|
|
|
[ReduxActionTypes.UPDATE_WIDGET_PROPERTY]: (
|
|
|
|
|
state: CanvasWidgetsReduxState,
|
|
|
|
|
action: ReduxAction<UpdateWidgetPropertyPayload>,
|
|
|
|
|
) => {
|
2021-02-25 14:00:02 +00:00
|
|
|
const { dynamicUpdates, updates, widgetId } = action.payload;
|
2021-01-25 08:57:26 +00:00
|
|
|
// We loop over all updates
|
2021-02-25 14:00:02 +00:00
|
|
|
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 || [];
|
2021-02-25 15:12:29 +00:00
|
|
|
state[widgetId].dynamicBindingPathList = uniqBy(
|
|
|
|
|
[...currentList, ...dynamicUpdates.dynamicBindingPathList],
|
|
|
|
|
"key",
|
2021-02-25 14:00:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (dynamicUpdates && dynamicUpdates.dynamicTriggerPathList.length) {
|
|
|
|
|
const currentList = state[widgetId].dynamicTriggerPathList || [];
|
2021-02-25 15:12:29 +00:00
|
|
|
state[widgetId].dynamicTriggerPathList = uniqBy(
|
|
|
|
|
[...currentList, ...dynamicUpdates.dynamicTriggerPathList],
|
|
|
|
|
"key",
|
2021-02-25 14:00:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
2019-09-18 10:19:50 +00:00
|
|
|
},
|
2019-09-09 10:30:22 +00:00
|
|
|
});
|
2019-02-10 13:06:05 +00:00
|
|
|
|
|
|
|
|
export interface CanvasWidgetsReduxState {
|
2019-09-09 10:30:22 +00:00
|
|
|
[widgetId: string]: FlattenedWidgetProps;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:30:22 +00:00
|
|
|
export default canvasWidgetsReducer;
|