Create circuits with rows atomically
This commit is contained in:
@@ -38,6 +38,7 @@ import {
|
||||
import type { VisibleGridRow } from "../utils/circuit-grid-projection";
|
||||
import {
|
||||
createCircuit,
|
||||
createCircuitWithDeviceRows,
|
||||
createCircuitDeviceRow,
|
||||
deleteCircuitById,
|
||||
deleteCircuitDeviceRowById,
|
||||
@@ -1201,28 +1202,41 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
const sortOrder =
|
||||
section.circuits.length > 0 ? Math.max(...section.circuits.map((circuit) => circuit.sortOrder)) + 10 : 10;
|
||||
const isDeviceField = deviceFieldKeys.has(key);
|
||||
|
||||
if (isDeviceField) {
|
||||
const created = await createCircuitWithDeviceRows(projectId, circuitListId, {
|
||||
circuit: {
|
||||
sectionId,
|
||||
equipmentIdentifier: next.nextIdentifier,
|
||||
displayName: "Neuer Stromkreis",
|
||||
sortOrder,
|
||||
isReserve: false,
|
||||
},
|
||||
deviceRows: [
|
||||
{
|
||||
name: "Manuelles Gerät",
|
||||
displayName: "Manuelles Gerät",
|
||||
phaseType: "single_phase",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0,
|
||||
simultaneityFactor: 1,
|
||||
cosPhi: 1,
|
||||
},
|
||||
],
|
||||
});
|
||||
const createdCircuit = created.circuit;
|
||||
const createdRow = created.deviceRows[0];
|
||||
await patchDeviceRow(createdRow.id, key, draft);
|
||||
return { rowKey: `circuitCompact:${createdCircuit.id}`, cellKey: key };
|
||||
}
|
||||
|
||||
const createdCircuit = (await createCircuit(projectId, circuitListId, {
|
||||
sectionId,
|
||||
equipmentIdentifier: next.nextIdentifier,
|
||||
displayName: "Neuer Stromkreis",
|
||||
sortOrder,
|
||||
isReserve: !isDeviceField,
|
||||
isReserve: true,
|
||||
})) as CircuitTreeCircuitDto;
|
||||
|
||||
if (isDeviceField) {
|
||||
const createdRow = (await createCircuitDeviceRow(createdCircuit.id, {
|
||||
name: "Manuelles Gerät",
|
||||
displayName: "Manuelles Gerät",
|
||||
phaseType: "single_phase",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0,
|
||||
simultaneityFactor: 1,
|
||||
cosPhi: 1,
|
||||
})) as { id: string };
|
||||
await patchDeviceRow(createdRow.id, key, draft);
|
||||
return { rowKey: `circuitCompact:${createdCircuit.id}`, cellKey: key };
|
||||
}
|
||||
|
||||
await patchCircuit(createdCircuit.id, key, draft);
|
||||
return { rowKey: `reserveCircuit:${createdCircuit.id}`, cellKey: key };
|
||||
}
|
||||
@@ -1654,28 +1668,33 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
const next = await getNextCircuitIdentifier(sectionId);
|
||||
const sortOrder =
|
||||
section.circuits.length > 0 ? Math.max(...section.circuits.map((circuit) => circuit.sortOrder)) + 10 : 10;
|
||||
const createdCircuit = (await createCircuit(projectId, circuitListId, {
|
||||
sectionId,
|
||||
equipmentIdentifier: next.nextIdentifier,
|
||||
displayName: device.displayName || device.name,
|
||||
sortOrder,
|
||||
isReserve: false,
|
||||
})) as CircuitTreeCircuitDto;
|
||||
|
||||
const createdRow = (await createCircuitDeviceRow(createdCircuit.id, {
|
||||
linkedProjectDeviceId: device.id,
|
||||
name: device.name,
|
||||
displayName: device.displayName,
|
||||
phaseType: resolvePhaseType(device),
|
||||
connectionKind: device.connectionKind ?? undefined,
|
||||
costGroup: device.costGroup ?? undefined,
|
||||
quantity: device.quantity,
|
||||
powerPerUnit: device.powerPerUnit,
|
||||
simultaneityFactor: device.simultaneityFactor,
|
||||
cosPhi: device.cosPhi ?? undefined,
|
||||
category: device.category ?? undefined,
|
||||
remark: device.remark ?? undefined,
|
||||
})) as { id: string };
|
||||
const created = await createCircuitWithDeviceRows(projectId, circuitListId, {
|
||||
circuit: {
|
||||
sectionId,
|
||||
equipmentIdentifier: next.nextIdentifier,
|
||||
displayName: device.displayName || device.name,
|
||||
sortOrder,
|
||||
isReserve: false,
|
||||
},
|
||||
deviceRows: [
|
||||
{
|
||||
linkedProjectDeviceId: device.id,
|
||||
name: device.name,
|
||||
displayName: device.displayName,
|
||||
phaseType: resolvePhaseType(device),
|
||||
connectionKind: device.connectionKind ?? undefined,
|
||||
costGroup: device.costGroup ?? undefined,
|
||||
quantity: device.quantity,
|
||||
powerPerUnit: device.powerPerUnit,
|
||||
simultaneityFactor: device.simultaneityFactor,
|
||||
cosPhi: device.cosPhi ?? undefined,
|
||||
category: device.category ?? undefined,
|
||||
remark: device.remark ?? undefined,
|
||||
},
|
||||
],
|
||||
});
|
||||
const createdCircuit = created.circuit;
|
||||
const createdRow = created.deviceRows[0];
|
||||
|
||||
setActiveSectionId(sectionId);
|
||||
setTargetCircuitId(createdCircuit.id);
|
||||
@@ -1732,7 +1751,7 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
}
|
||||
|
||||
async function recreateCircuit(snapshot: CircuitSnapshot) {
|
||||
const createdCircuit = (await createCircuit(projectId, circuitListId, {
|
||||
const circuitInput = {
|
||||
sectionId: snapshot.sectionId,
|
||||
equipmentIdentifier: snapshot.equipmentIdentifier,
|
||||
displayName: snapshot.displayName,
|
||||
@@ -1750,11 +1769,23 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
status: snapshot.status,
|
||||
isReserve: snapshot.isReserve,
|
||||
remark: snapshot.remark,
|
||||
})) as CircuitTreeCircuitDto;
|
||||
};
|
||||
|
||||
const createdRowIds: string[] = [];
|
||||
for (const row of snapshot.deviceRows) {
|
||||
const created = (await createCircuitDeviceRow(createdCircuit.id, {
|
||||
if (snapshot.deviceRows.length === 0) {
|
||||
const createdCircuit = (await createCircuit(
|
||||
projectId,
|
||||
circuitListId,
|
||||
circuitInput
|
||||
)) as CircuitTreeCircuitDto;
|
||||
return { circuitId: createdCircuit.id, rowIds: [] };
|
||||
}
|
||||
|
||||
const created = await createCircuitWithDeviceRows(projectId, circuitListId, {
|
||||
circuit: {
|
||||
...circuitInput,
|
||||
isReserve: false,
|
||||
},
|
||||
deviceRows: snapshot.deviceRows.map((row) => ({
|
||||
linkedProjectDeviceId: row.linkedProjectDeviceId,
|
||||
name: row.name,
|
||||
displayName: row.displayName,
|
||||
@@ -1773,10 +1804,12 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
|
||||
remark: row.remark,
|
||||
overriddenFields: row.overriddenFields,
|
||||
sortOrder: row.sortOrder,
|
||||
})) as { id: string };
|
||||
createdRowIds.push(created.id);
|
||||
}
|
||||
return { circuitId: createdCircuit.id, rowIds: createdRowIds };
|
||||
})),
|
||||
});
|
||||
return {
|
||||
circuitId: created.circuit.id,
|
||||
rowIds: created.deviceRows.map((row) => row.id),
|
||||
};
|
||||
}
|
||||
|
||||
function parseDraggedProjectDeviceId(event: DragEvent<HTMLElement>) {
|
||||
|
||||
@@ -18,6 +18,8 @@ import type {
|
||||
RoomDto,
|
||||
UpdateConsumerInput,
|
||||
CircuitTreeResponseDto,
|
||||
CircuitTreeCircuitDto,
|
||||
CircuitTreeDeviceRowDto,
|
||||
CreateCircuitInputDto,
|
||||
UpdateCircuitInputDto,
|
||||
CreateCircuitDeviceRowInputDto,
|
||||
@@ -98,6 +100,26 @@ export function createCircuit(projectId: string, circuitListId: string, input: C
|
||||
});
|
||||
}
|
||||
|
||||
export function createCircuitWithDeviceRows(
|
||||
projectId: string,
|
||||
circuitListId: string,
|
||||
input: {
|
||||
circuit: CreateCircuitInputDto;
|
||||
deviceRows: CreateCircuitDeviceRowInputDto[];
|
||||
}
|
||||
) {
|
||||
return request<{
|
||||
circuit: CircuitTreeCircuitDto;
|
||||
deviceRows: CircuitTreeDeviceRowDto[];
|
||||
}>(
|
||||
`/api/projects/${projectId}/circuit-lists/${circuitListId}/circuits-with-device-rows`,
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify(input),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export function updateCircuitById(circuitId: string, input: UpdateCircuitInputDto) {
|
||||
return request(`/api/circuits/${circuitId}`, {
|
||||
method: "PATCH",
|
||||
|
||||
Reference in New Issue
Block a user