Reorder circuit groups
This commit is contained in:
@@ -52,6 +52,7 @@ import {
|
||||
buildCircuitSectionRenumberAssignments,
|
||||
} from "../utils/circuit-section-renumber-command";
|
||||
import {
|
||||
buildCircuitGroupReorderAssignments,
|
||||
buildNewCircuitGroupSnapshot,
|
||||
canDeleteCircuitGroup,
|
||||
renameCircuitGroupSnapshot,
|
||||
@@ -95,6 +96,7 @@ import {
|
||||
moveCircuitDeviceRowsToNewCircuitCommand,
|
||||
reorderCircuitSectionCommand,
|
||||
reorderCircuitSectionsCommand,
|
||||
reorderCircuitGroupsCommand,
|
||||
renumberCircuitSectionCommand,
|
||||
redoProjectCommand,
|
||||
updateCircuitById,
|
||||
@@ -1235,6 +1237,36 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
});
|
||||
}
|
||||
|
||||
async function handleMoveCircuitGroup(
|
||||
groupId: string,
|
||||
direction: -1 | 1
|
||||
) {
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
const assignments = buildCircuitGroupReorderAssignments(
|
||||
data.sections,
|
||||
groupId,
|
||||
direction
|
||||
);
|
||||
if (!assignments) {
|
||||
return;
|
||||
}
|
||||
await runCommand({
|
||||
label: "Stromkreisgruppen sortieren",
|
||||
redo: async () => {
|
||||
const result = await reorderCircuitGroupsCommand(
|
||||
projectId,
|
||||
getExpectedProjectRevision(),
|
||||
circuitListId,
|
||||
assignments
|
||||
);
|
||||
applyProjectCommandResult(result);
|
||||
return null;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function handleSaveCircuitProtection(
|
||||
protection: CircuitTreeProtectionDeviceDto
|
||||
) {
|
||||
@@ -3477,6 +3509,40 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
>
|
||||
Gruppe bearbeiten
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
disabled={
|
||||
!buildCircuitGroupReorderAssignments(
|
||||
data.sections,
|
||||
section.id,
|
||||
-1
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
void handleMoveCircuitGroup(section.id, -1)
|
||||
}
|
||||
title="Innerhalb derselben Kategorie nach oben verschieben"
|
||||
>
|
||||
Gruppe nach oben
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
disabled={
|
||||
!buildCircuitGroupReorderAssignments(
|
||||
data.sections,
|
||||
section.id,
|
||||
1
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
void handleMoveCircuitGroup(section.id, 1)
|
||||
}
|
||||
title="Innerhalb derselben Kategorie nach unten verschieben"
|
||||
>
|
||||
Gruppe nach unten
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
|
||||
@@ -57,6 +57,7 @@ import type {
|
||||
} from "../../domain/models/distribution-board-component-structure-project-command.model";
|
||||
import type {
|
||||
CircuitGroupSnapshot,
|
||||
CircuitGroupReorderAssignment,
|
||||
} from "../../domain/models/circuit-group-structure-project-command.model";
|
||||
import type {
|
||||
CircuitProtectionSnapshot,
|
||||
@@ -433,6 +434,24 @@ export function deleteCircuitGroupCommand(
|
||||
);
|
||||
}
|
||||
|
||||
export function reorderCircuitGroupsCommand(
|
||||
projectId: string,
|
||||
expectedRevision: number,
|
||||
circuitListId: string,
|
||||
assignments: CircuitGroupReorderAssignment[]
|
||||
) {
|
||||
return executeProjectCommand(
|
||||
projectId,
|
||||
expectedRevision,
|
||||
{
|
||||
schemaVersion: 1,
|
||||
type: "circuit-group.reorder",
|
||||
payload: { circuitListId, assignments },
|
||||
},
|
||||
"Stromkreisgruppen sortieren"
|
||||
);
|
||||
}
|
||||
|
||||
export function updateCircuitProtectionCommand(
|
||||
projectId: string,
|
||||
expectedRevision: number,
|
||||
|
||||
@@ -7,6 +7,9 @@ import {
|
||||
type CircuitGroupCategory,
|
||||
} from "../../shared/constants/circuit-group";
|
||||
import type { CircuitTreeSectionDto } from "../types";
|
||||
import type {
|
||||
CircuitGroupReorderAssignment,
|
||||
} from "../../domain/models/circuit-group-structure-project-command.model";
|
||||
|
||||
export function toCircuitGroupSnapshot(
|
||||
section: CircuitTreeSectionDto,
|
||||
@@ -77,6 +80,41 @@ export function canDeleteCircuitGroup(
|
||||
return section.circuits.length === 0 && section.components.length === 0;
|
||||
}
|
||||
|
||||
export function buildCircuitGroupReorderAssignments(
|
||||
sections: readonly CircuitTreeSectionDto[],
|
||||
groupId: string,
|
||||
direction: -1 | 1
|
||||
): CircuitGroupReorderAssignment[] | null {
|
||||
const current = sections.find((section) => section.id === groupId);
|
||||
if (!current?.category) {
|
||||
return null;
|
||||
}
|
||||
const categoryGroups = sections
|
||||
.filter((section) => section.category === current.category)
|
||||
.sort(
|
||||
(left, right) =>
|
||||
left.sortOrder - right.sortOrder ||
|
||||
left.id.localeCompare(right.id)
|
||||
);
|
||||
const currentIndex = categoryGroups.findIndex(
|
||||
(section) => section.id === groupId
|
||||
);
|
||||
const target = categoryGroups[currentIndex + direction];
|
||||
if (!target) {
|
||||
return null;
|
||||
}
|
||||
return sections.map((section) => ({
|
||||
groupId: section.id,
|
||||
expectedSortOrder: section.sortOrder,
|
||||
targetSortOrder:
|
||||
section.id === current.id
|
||||
? target.sortOrder
|
||||
: section.id === target.id
|
||||
? current.sortOrder
|
||||
: section.sortOrder,
|
||||
}));
|
||||
}
|
||||
|
||||
function getNewGroupSortOrder(
|
||||
category: CircuitGroupCategory,
|
||||
sections: readonly (CircuitTreeSectionDto & {
|
||||
|
||||
Reference in New Issue
Block a user