Merged PR 173: Implements API methods through right click + more (read desc)

- Implements API methods through right click
- Refactor events
- Refactor usage of setHistory and setHistoryCurrentStep into a single function
- Update ContainerOperations documentations
- Added AddContainers in order to add multiple containers + refactor AddContainer to use it
- Fix regression
- Fix AddContainer at index
This commit is contained in:
Eric Nguyen 2022-08-30 14:45:29 +00:00
parent 79c6874240
commit 57e6c9a156
20 changed files with 652 additions and 291 deletions

View file

@ -0,0 +1,131 @@
import Swal from 'sweetalert2';
import { AddMethod } from '../../../Enums/AddMethod';
import { IAction } from '../../../Interfaces/IAction';
import { IConfiguration } from '../../../Interfaces/IConfiguration';
import { IContainerModel } from '../../../Interfaces/IContainerModel';
import { IHistoryState } from '../../../Interfaces/IHistoryState';
import { ISetContainerListRequest } from '../../../Interfaces/ISetContainerListRequest';
import { ISetContainerListResponse } from '../../../Interfaces/ISetContainerListResponse';
import { FindContainerById } from '../../../utils/itertools';
import { SetContainerList } from '../../API/api';
import { AddContainers, DeleteContainer } from './ContainerOperations';
export function GetAction(
action: IAction,
currentState: IHistoryState,
configuration: IConfiguration,
history: IHistoryState[],
historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void
): (target: HTMLElement) => void {
return (target: HTMLElement) => {
const id = target.id;
const container = FindContainerById(currentState.mainContainer, id);
if (container === undefined) {
Swal.fire({
title: 'Error',
text: 'No container was selected on right click',
icon: 'error'
});
throw new Error(`[API:${action.Action}] No container was selected`);
}
/* eslint-disable @typescript-eslint/naming-convention */
const request: ISetContainerListRequest = {
Container: container,
Action: action.Action,
ApplicationState: currentState
};
/* eslint-enable */
SetContainerList(request)
.then((response: ISetContainerListResponse) => {
HandleSetContainerList(
action,
container,
response,
configuration,
history,
historyCurrentStep,
setNewHistory
);
});
};
}
function HandleSetContainerList(
action: IAction,
selectedContainer: IContainerModel,
response: ISetContainerListResponse,
configuration: IConfiguration,
history: IHistoryState[],
historyCurrentStep: number,
setNewHistory: (newHistory: IHistoryState[]) => void
): void {
switch (action.AddingBehavior) {
case AddMethod.Append:
setNewHistory(
AddContainers(
selectedContainer.children.length,
response.Containers.map(container => container.properties.type),
selectedContainer.properties.id,
configuration,
history,
historyCurrentStep
));
break;
case AddMethod.Insert:
break;
case AddMethod.Replace:
setNewHistory(
HandleReplace(
selectedContainer,
response,
configuration,
history,
historyCurrentStep
)
);
break;
}
}
function HandleReplace(
selectedContainer: IContainerModel,
response: ISetContainerListResponse,
configuration: IConfiguration,
history: IHistoryState[],
historyCurrentStep: number
): IHistoryState[] {
if (selectedContainer.parent === undefined || selectedContainer.parent === null) {
throw new Error('[ReplaceContainer] Cannot replace a container that does not exists');
}
const index = selectedContainer.parent.children.indexOf(selectedContainer);
const types = response.Containers.map(container => container.properties.type);
const newHistoryBeforeDelete = AddContainers(
index + 1,
types,
selectedContainer.properties.parentId,
configuration,
history,
historyCurrentStep
);
const newHistoryAfterDelete = DeleteContainer(
selectedContainer.properties.id,
newHistoryBeforeDelete,
newHistoryBeforeDelete.length - 1
);
// Remove AddContainers from history
newHistoryAfterDelete.splice(newHistoryAfterDelete.length - 2, 1);
// Rename the last action by Replace
newHistoryAfterDelete[newHistoryAfterDelete.length - 1].lastAction =
`Replace ${selectedContainer.properties.id} by [${types.join(', ')}]`;
return newHistoryAfterDelete;
}