Use parentId to delete and move
This commit is contained in:
parent
eb195aa321
commit
72443b0b06
|
|
@ -92,12 +92,13 @@ export type WidgetMove = {
|
||||||
widgetId: string;
|
widgetId: string;
|
||||||
leftColumn: number;
|
leftColumn: number;
|
||||||
topRow: number;
|
topRow: number;
|
||||||
|
parentId: string;
|
||||||
/*
|
/*
|
||||||
If parentWidgetId is different from what we have in redux store,
|
If newParentId is different from what we have in redux store,
|
||||||
then we have to delete this,
|
then we have to delete this,
|
||||||
as it has been dropped in another container somewhere.
|
as it has been dropped in another container somewhere.
|
||||||
*/
|
*/
|
||||||
parentWidgetId: string;
|
newParentId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WidgetRemoveChild = {
|
export type WidgetRemoveChild = {
|
||||||
|
|
@ -107,6 +108,7 @@ export type WidgetRemoveChild = {
|
||||||
|
|
||||||
export type WidgetDelete = {
|
export type WidgetDelete = {
|
||||||
widgetId: string;
|
widgetId: string;
|
||||||
|
parentId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WidgetResize = {
|
export type WidgetResize = {
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,10 @@ const DraggableComponent = (props: DraggableComponentProps) => {
|
||||||
const { updateWidget } = useContext(WidgetFunctionsContext);
|
const { updateWidget } = useContext(WidgetFunctionsContext);
|
||||||
const [isResizing, setIsResizing] = useState(false);
|
const [isResizing, setIsResizing] = useState(false);
|
||||||
const deleteWidget = () => {
|
const deleteWidget = () => {
|
||||||
updateWidget && updateWidget(WidgetOperations.DELETE, props.widgetId);
|
updateWidget &&
|
||||||
|
updateWidget(WidgetOperations.DELETE, props.widgetId, {
|
||||||
|
parentId: props.parentId,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
const [{ isDragging }, drag, preview] = useDrag({
|
const [{ isDragging }, drag, preview] = useDrag({
|
||||||
item: props,
|
item: props,
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,7 @@ import {
|
||||||
WidgetDelete,
|
WidgetDelete,
|
||||||
} from "../actions/pageActions";
|
} from "../actions/pageActions";
|
||||||
import { FlattenedWidgetProps } from "../reducers/entityReducers/canvasWidgetsReducer";
|
import { FlattenedWidgetProps } from "../reducers/entityReducers/canvasWidgetsReducer";
|
||||||
import {
|
import { getWidgets, getWidget, getDefaultWidgetConfig } from "./selectors";
|
||||||
getWidgets,
|
|
||||||
getWidget,
|
|
||||||
getWidgetParent,
|
|
||||||
getDefaultWidgetConfig,
|
|
||||||
} from "./selectors";
|
|
||||||
import {
|
import {
|
||||||
generateWidgetProps,
|
generateWidgetProps,
|
||||||
updateWidgetPosition,
|
updateWidgetPosition,
|
||||||
|
|
@ -72,14 +67,14 @@ export function* addChildSaga(addChildAction: ReduxAction<WidgetAddChild>) {
|
||||||
|
|
||||||
export function* deleteSaga(deleteAction: ReduxAction<WidgetDelete>) {
|
export function* deleteSaga(deleteAction: ReduxAction<WidgetDelete>) {
|
||||||
try {
|
try {
|
||||||
const { widgetId } = deleteAction.payload;
|
const { widgetId, parentId } = deleteAction.payload;
|
||||||
const widgets = yield select(getWidgets);
|
const widgets = yield select(getWidgets);
|
||||||
delete widgets[widgetId];
|
const parent = yield select(getWidget, parentId);
|
||||||
const parent = yield select(getWidgetParent, widgetId);
|
|
||||||
parent.children = parent.children.filter(
|
parent.children = parent.children.filter(
|
||||||
(child: string) => child !== widgetId,
|
(child: string) => child !== widgetId,
|
||||||
);
|
);
|
||||||
widgets[parent.widgetId] = parent;
|
delete widgets[widgetId];
|
||||||
|
widgets[parentId] = parent;
|
||||||
yield put({
|
yield put({
|
||||||
type: ReduxActionTypes.UPDATE_LAYOUT,
|
type: ReduxActionTypes.UPDATE_LAYOUT,
|
||||||
payload: { widgets },
|
payload: { widgets },
|
||||||
|
|
@ -97,25 +92,31 @@ export function* deleteSaga(deleteAction: ReduxAction<WidgetDelete>) {
|
||||||
|
|
||||||
export function* moveSaga(moveAction: ReduxAction<WidgetMove>) {
|
export function* moveSaga(moveAction: ReduxAction<WidgetMove>) {
|
||||||
try {
|
try {
|
||||||
const { widgetId, leftColumn, topRow, parentWidgetId } = moveAction.payload;
|
const {
|
||||||
|
widgetId,
|
||||||
|
leftColumn,
|
||||||
|
topRow,
|
||||||
|
parentId,
|
||||||
|
newParentId,
|
||||||
|
} = moveAction.payload;
|
||||||
let widget: FlattenedWidgetProps = yield select(getWidget, widgetId);
|
let widget: FlattenedWidgetProps = yield select(getWidget, widgetId);
|
||||||
// Get all widgets from DSL/Redux Store
|
// Get all widgets from DSL/Redux Store
|
||||||
const widgets = yield select(getWidgets) as any;
|
const widgets = yield select(getWidgets) as any;
|
||||||
// Get parent from DSL/Redux Store
|
// Get parent from DSL/Redux Store
|
||||||
const parent = yield select(getWidgetParent, widgetId);
|
const parent = yield select(getWidget, parentId);
|
||||||
// Update position of widget
|
// Update position of widget
|
||||||
widget = updateWidgetPosition(widget, leftColumn, topRow, parent);
|
widget = updateWidgetPosition(widget, leftColumn, topRow, parent);
|
||||||
// Replace widget with update widget props
|
// Replace widget with update widget props
|
||||||
widgets[widgetId] = widget;
|
widgets[widgetId] = widget;
|
||||||
// If the parent has changed i.e parentWidgetId is not parent.widgetId
|
// If the parent has changed i.e parentWidgetId is not parent.widgetId
|
||||||
if (parent.widgetId !== parentWidgetId && widgetId !== parentWidgetId) {
|
if (parent.widgetId !== newParentId && widgetId !== newParentId) {
|
||||||
// Remove from the previous parent
|
// Remove from the previous parent
|
||||||
parent.children = parent.children.filter(
|
parent.children = parent.children.filter(
|
||||||
(child: string) => child !== widgetId,
|
(child: string) => child !== widgetId,
|
||||||
);
|
);
|
||||||
widgets[parent.widgetId] = parent;
|
widgets[parent.widgetId] = parent;
|
||||||
// Add to new parent
|
// Add to new parent
|
||||||
widgets[parentWidgetId].children.push(widgetId);
|
widgets[newParentId].children.push(widgetId);
|
||||||
}
|
}
|
||||||
yield put({
|
yield put({
|
||||||
type: ReduxActionTypes.UPDATE_LAYOUT,
|
type: ReduxActionTypes.UPDATE_LAYOUT,
|
||||||
|
|
|
||||||
|
|
@ -22,20 +22,6 @@ export const getEditorConfigs = (
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getWidgetParent = (
|
|
||||||
state: AppState,
|
|
||||||
widgetId: string,
|
|
||||||
): FlattenedWidgetProps | undefined => {
|
|
||||||
const widgets = state.entities.canvasWidgets;
|
|
||||||
return Object.values(widgets).find(
|
|
||||||
(widget: FlattenedWidgetProps) =>
|
|
||||||
widget &&
|
|
||||||
widget.children &&
|
|
||||||
widget.children.length > 0 &&
|
|
||||||
widget.children.indexOf(widgetId) > -1,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getDefaultWidgetConfig = (
|
export const getDefaultWidgetConfig = (
|
||||||
state: AppState,
|
state: AppState,
|
||||||
type: WidgetType,
|
type: WidgetType,
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,8 @@ export const widgetOperationParams = (
|
||||||
{
|
{
|
||||||
leftColumn,
|
leftColumn,
|
||||||
topRow,
|
topRow,
|
||||||
parentWidgetId: widgetId,
|
parentId: widget.parentId,
|
||||||
|
newParentId: widgetId,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
// If this is not an existing widget, we'll not have the widgetId
|
// If this is not an existing widget, we'll not have the widgetId
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user