Merged PR 216: Deprecate parent from IContainerModel for FindContainerById

This commit is contained in:
Eric Nguyen 2022-10-17 16:01:06 +00:00
parent d40cd8cf8e
commit b4eba6bb9b
19 changed files with 97 additions and 109 deletions

View file

@ -8,6 +8,7 @@ import { ISetContainerListRequest } from '../../../Interfaces/ISetContainerListR
import { ISetContainerListResponse } from '../../../Interfaces/ISetContainerListResponse';
import { FindContainerById } from '../../../utils/itertools';
import { SetContainerList } from '../../API/api';
import { GetCurrentHistoryState } from '../Editor';
import { AddContainers } from './AddContainer';
import { DeleteContainer } from './ContainerOperations';
@ -62,15 +63,16 @@ export function GetAction(
function GetPreviousAndNextSiblings(containers: Map<string, IContainerModel>, container: IContainerModel): { prev: IContainerModel | undefined, next: IContainerModel | undefined } {
let prev;
let next;
if (container.parent !== undefined &&
container.parent !== null &&
container.parent.children.length > 1) {
const index = container.parent.children.indexOf(container.properties.id);
const parent = FindContainerById(containers, container.properties.parentId);
if (parent !== undefined &&
parent !== null &&
parent.children.length > 1) {
const index = parent.children.indexOf(container.properties.id);
if (index > 0) {
prev = FindContainerById(containers, container.parent.children[index - 1]);
prev = FindContainerById(containers, parent.children[index - 1]);
}
if (index < container.parent.children.length - 1) {
next = FindContainerById(containers, container.parent.children[index + 1]);
if (index < parent.children.length - 1) {
next = FindContainerById(containers, parent.children[index + 1]);
}
}
return { prev, next };
@ -86,6 +88,9 @@ function HandleSetContainerList(
setNewHistory: (newHistory: IHistoryState[]) => void
): void {
const addingBehavior = response.AddingBehavior ?? action.AddingBehavior;
const current = GetCurrentHistoryState(history, historyCurrentStep);
const containers = current.containers;
const parent = FindContainerById(containers, selectedContainer.properties.parentId);
switch (addingBehavior) {
case AddMethod.Append:
setNewHistory(
@ -103,6 +108,7 @@ function HandleSetContainerList(
case AddMethod.Replace:
setNewHistory(
HandleReplace(
containers,
selectedContainer,
response,
configuration,
@ -112,7 +118,7 @@ function HandleSetContainerList(
);
break;
case AddMethod.ReplaceParent:
if (selectedContainer.parent === undefined || selectedContainer.parent === null) {
if (parent === undefined || parent === null) {
Swal.fire({
title: 'Error',
text: 'The selected container has not parent to replace',
@ -122,7 +128,8 @@ function HandleSetContainerList(
}
setNewHistory(
HandleReplace(
selectedContainer.parent,
containers,
parent,
response,
configuration,
history,
@ -134,17 +141,19 @@ function HandleSetContainerList(
}
function HandleReplace(
containers: Map<string, IContainerModel>,
selectedContainer: IContainerModel,
response: ISetContainerListResponse,
configuration: IConfiguration,
history: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] {
if (selectedContainer.parent === undefined || selectedContainer.parent === null) {
const parent = FindContainerById(containers, selectedContainer.properties.parentId);
if (parent === undefined || parent === null) {
throw new Error('[ReplaceContainer] Cannot replace a container that does not exists');
}
const index = selectedContainer.parent.children.indexOf(selectedContainer.properties.id);
const index = parent.children.indexOf(selectedContainer.properties.id);
const newHistoryAfterDelete = DeleteContainer(
selectedContainer.properties.id,