2019-08-30 10:33:49 +00:00
|
|
|
import { createReducer } from "../../utils/AppsmithUtils"
|
2019-02-10 13:06:05 +00:00
|
|
|
import {
|
|
|
|
|
ActionTypes,
|
|
|
|
|
LoadCanvasPayload,
|
|
|
|
|
ReduxAction
|
|
|
|
|
} from "../../constants/ActionConstants"
|
2019-09-09 09:08:54 +00:00
|
|
|
import { WidgetProps } from "../../widgets/BaseWidget"
|
2019-08-29 11:22:09 +00:00
|
|
|
import CanvasWidgetsNormalizer from "../../normalizers/CanvasWidgetsNormalizer";
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-08-26 12:41:21 +00:00
|
|
|
const initialState: CanvasWidgetsReduxState = {}
|
|
|
|
|
|
|
|
|
|
|
2019-09-09 09:08:54 +00:00
|
|
|
export interface IFlattenedWidgetProps extends WidgetProps {
|
2019-08-29 11:22:09 +00:00
|
|
|
children?: string[];
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-21 17:42:23 +00:00
|
|
|
const canvasWidgetsReducer = createReducer(initialState, {
|
2019-03-26 15:28:24 +00:00
|
|
|
[ActionTypes.UPDATE_CANVAS]: (
|
2019-03-21 17:42:23 +00:00
|
|
|
state: CanvasWidgetsReduxState,
|
|
|
|
|
action: ReduxAction<LoadCanvasPayload>
|
|
|
|
|
) => {
|
|
|
|
|
return { ...action.payload.widgets }
|
2019-08-26 12:41:21 +00:00
|
|
|
},
|
|
|
|
|
[ActionTypes.ADD_PAGE_WIDGET]: (
|
|
|
|
|
state: CanvasWidgetsReduxState,
|
2019-09-09 09:08:54 +00:00
|
|
|
action: ReduxAction<{pageId: string, widget: WidgetProps}>
|
2019-08-26 12:41:21 +00:00
|
|
|
) => {
|
|
|
|
|
const widget = action.payload.widget
|
|
|
|
|
const widgetTree = CanvasWidgetsNormalizer.denormalize("0", { canvasWidgets: state })
|
|
|
|
|
const children = widgetTree.children || []
|
|
|
|
|
children.push(widget)
|
|
|
|
|
widgetTree.children = children
|
|
|
|
|
const newState = CanvasWidgetsNormalizer.normalize({
|
|
|
|
|
responseMeta: {},
|
|
|
|
|
pageWidget: widgetTree
|
|
|
|
|
}).entities
|
|
|
|
|
return newState.canvasWidgets
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
2019-03-21 17:42:23 +00:00
|
|
|
})
|
2019-02-10 13:06:05 +00:00
|
|
|
|
|
|
|
|
export interface CanvasWidgetsReduxState {
|
2019-09-02 10:58:11 +00:00
|
|
|
[widgetId: string]: IFlattenedWidgetProps;
|
2019-02-10 13:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default canvasWidgetsReducer
|