Reorder circuit groups
This commit is contained in:
@@ -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