forked from jappel/leistungsbilanz-ts
Persist circuit reorders
This commit is contained in:
parent
08a2775a88
commit
abcb468807
26 changed files with 914 additions and 372 deletions
|
|
@ -36,6 +36,12 @@ import type {
|
|||
import type {
|
||||
CircuitDeviceRowMoveAssignment,
|
||||
} from "../../domain/models/circuit-device-row-move-project-command.model";
|
||||
import type {
|
||||
CircuitSectionReorderAssignment,
|
||||
} from "../../domain/models/circuit-section-reorder-project-command.model";
|
||||
import type {
|
||||
CircuitSectionsReorderSection,
|
||||
} from "../../domain/models/circuit-sections-reorder-project-command.model";
|
||||
|
||||
async function request<T>(url: string, init?: RequestInit): Promise<T> {
|
||||
const response = await fetch(url, {
|
||||
|
|
@ -319,11 +325,41 @@ export function renumberCircuitSection(sectionId: string) {
|
|||
});
|
||||
}
|
||||
|
||||
export function reorderSectionCircuits(sectionId: string, orderedCircuitIds: string[]) {
|
||||
return request(`/api/circuit-sections/${sectionId}/circuits/reorder`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ orderedCircuitIds }),
|
||||
});
|
||||
export function reorderCircuitSectionCommand(
|
||||
projectId: string,
|
||||
expectedRevision: number,
|
||||
sectionId: string,
|
||||
assignments: CircuitSectionReorderAssignment[],
|
||||
description: string
|
||||
) {
|
||||
return executeProjectCommand(
|
||||
projectId,
|
||||
expectedRevision,
|
||||
{
|
||||
schemaVersion: 1,
|
||||
type: "circuit.reorder-section",
|
||||
payload: { sectionId, assignments },
|
||||
},
|
||||
description
|
||||
);
|
||||
}
|
||||
|
||||
export function reorderCircuitSectionsCommand(
|
||||
projectId: string,
|
||||
expectedRevision: number,
|
||||
sections: CircuitSectionsReorderSection[],
|
||||
description: string
|
||||
) {
|
||||
return executeProjectCommand(
|
||||
projectId,
|
||||
expectedRevision,
|
||||
{
|
||||
schemaVersion: 1,
|
||||
type: "circuit.reorder-sections",
|
||||
payload: { sections },
|
||||
},
|
||||
description
|
||||
);
|
||||
}
|
||||
|
||||
export function updateSectionEquipmentIdentifiers(
|
||||
|
|
|
|||
44
src/frontend/utils/circuit-section-reorder-command.ts
Normal file
44
src/frontend/utils/circuit-section-reorder-command.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import type {
|
||||
CircuitSectionReorderAssignment,
|
||||
} from "../../domain/models/circuit-section-reorder-project-command.model.js";
|
||||
import type { CircuitTreeCircuitDto } from "../types.js";
|
||||
|
||||
export function buildCircuitSectionReorderAssignments(
|
||||
circuits: CircuitTreeCircuitDto[],
|
||||
orderedCircuitIds: string[]
|
||||
): CircuitSectionReorderAssignment[] {
|
||||
if (
|
||||
circuits.length !== orderedCircuitIds.length ||
|
||||
new Set(orderedCircuitIds).size !== orderedCircuitIds.length
|
||||
) {
|
||||
throw new Error(
|
||||
"Die Reihenfolge muss jeden Stromkreis des Bereichs genau einmal enthalten."
|
||||
);
|
||||
}
|
||||
|
||||
const circuitsById = new Map(
|
||||
circuits.map((circuit) => [circuit.id, circuit])
|
||||
);
|
||||
const assignments = orderedCircuitIds.map((circuitId, index) => {
|
||||
const circuit = circuitsById.get(circuitId);
|
||||
if (!circuit) {
|
||||
throw new Error(
|
||||
"Die Reihenfolge enthält einen ungültigen Stromkreis."
|
||||
);
|
||||
}
|
||||
return {
|
||||
circuitId,
|
||||
expectedSortOrder: circuit.sortOrder,
|
||||
targetSortOrder: (index + 1) * 10,
|
||||
};
|
||||
});
|
||||
if (
|
||||
assignments.every(
|
||||
(assignment) =>
|
||||
assignment.expectedSortOrder === assignment.targetSortOrder
|
||||
)
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
return assignments;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue