[278] Nutzungseinheiten und Wohnungsspiegel: Project.units, Room.unitId/roomCount, Kern-Abfrage mit Zimmerzahl und SIA-416-Flächen, Panel, Objekt-Info, Blatt-Tabelle, CSV/PDF, CLI, Beispiel Wettbewerb
This commit is contained in:
38 files changed
+1445
-54
No files matched your search
@@ -12,6 +12,7 @@ import { exportProjectName, areaReportToCsv, buildAreaReport, buildAreaReportPdf
|
||||
import type { AgfBasis } from "../core";
|
||||
import { DEFAULT_AREA_INPUTS, ivhbOptionsOf, type AreaInputs } from "./areaInputs";
|
||||
import { downloadCsv } from "./downloadFile";
|
||||
import { ReportTable } from "./ReportTable";
|
||||
import { runWithAnonymityGate } from "../state/anonymityGate";
|
||||
import { useStore } from "../state/appStore";
|
||||
|
||||
@@ -26,6 +27,8 @@ export function AreaTableSection() {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[project, inputs],
|
||||
);
|
||||
// [278] CSV/PDF enthalten zusätzlich den Wohnungsspiegel (die Anzeige hat ihn im Abschnitt „Nutzungseinheiten“).
|
||||
const buildExport = () => buildAreaReport(project, { areas: true, ivhb: true, siteProof: true, units: true, ivhbOptions: ivhbOptionsOf(inputs) });
|
||||
const set = (patch: Partial<AreaInputs>) => setInputs((i) => ({ ...i, ...patch }));
|
||||
const stem = `${exportProjectName(project) || "projekt"}-flaechen`;
|
||||
|
||||
@@ -37,13 +40,13 @@ export function AreaTableSection() {
|
||||
};
|
||||
const exportCsv = () => {
|
||||
if (emptyHint()) return;
|
||||
runWithAnonymityGate(project, [`${stem}.csv`], () => downloadCsv(`${stem}.csv`, areaReportToCsv(report)));
|
||||
runWithAnonymityGate(project, [`${stem}.csv`], () => downloadCsv(`${stem}.csv`, areaReportToCsv(buildExport())));
|
||||
};
|
||||
const exportPdf = () => {
|
||||
if (emptyHint()) return;
|
||||
runWithAnonymityGate(project, [`${stem}.pdf`], async () => {
|
||||
try {
|
||||
(await buildAreaReportPdf(report)).save(`${stem}.pdf`);
|
||||
(await buildAreaReportPdf(buildExport())).save(`${stem}.pdf`);
|
||||
} catch {
|
||||
notify(t("areas.exportPdf.fail"));
|
||||
}
|
||||
@@ -65,40 +68,7 @@ export function AreaTableSection() {
|
||||
</div>
|
||||
|
||||
{report.sections.map((s, si) => (
|
||||
<div key={si} className="area-block">
|
||||
<div className="area-heading">{s.heading}</div>
|
||||
<div className="area-scroll">
|
||||
<table className={s.columns.length > 6 ? "area-table wide" : "area-table"}>
|
||||
<thead>
|
||||
<tr>
|
||||
{s.columns.map((c, i) => (
|
||||
<th key={i} className={i === 0 ? "" : "num"}>
|
||||
{c}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{s.rows.map((r, ri) => (
|
||||
<tr key={ri} className={r.total ? "total" : ""}>
|
||||
{r.cells.map((c, ci) =>
|
||||
typeof c === "string" ? (
|
||||
// Kurze Textzellen in Zahlenspalten (Strich, ja/nein) stehen wie die Zahlen rechts.
|
||||
<td key={ci} data-label={ci > 0 ? s.columns[ci] : undefined} className={ci > 0 && c.length <= 4 ? "num" : undefined}>
|
||||
{c}
|
||||
</td>
|
||||
) : (
|
||||
<td key={ci} data-label={ci > 0 ? s.columns[ci] : undefined} className="num">
|
||||
{c.n === null ? "–" : c.n.toFixed(c.d)}
|
||||
</td>
|
||||
),
|
||||
)}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<ReportTable key={si} section={s} />
|
||||
))}
|
||||
|
||||
<div className="area-inputs">
|
||||
|
||||
@@ -24,7 +24,7 @@ Kern; hier stehen Eingabe und Darstellung. Zustand über den Store (`src/state`)
|
||||
| `objectInfo/*.tsx` | Abschnitte je Element-Art (`WallSection`, `CeilingSection`, `RoofSections`, `OpeningSections`, `StairSection`, `RoomSection`, `RoomStampSection` (Raumstempel samt Schrift, [218]), `SolidSections`, `SectionLineSection`, `RefSections`, `TreeSection`), gemeinsame Felder (`fields.tsx`) und Info-Blöcke (`InfoBlocks.tsx`); `ObjectInfoPanel` zeigt sie als einklappbare Gruppen (`InfoGroups.tsx`, `InfoGroup.tsx`, Zustand `infoGroupState.ts`); `groups.ts` hält die Zuordnung (Attribute = nur Gestaltung, Objekt-Info = Bauteil/BIM) und das Ziel der Taste T (`[189]`) |
|
||||
| `DrawingLevelsPanel.tsx`, `LayersPanel.tsx`, `ElementTreePanel.tsx` | Zeichnungsebenen, Ebenen, Baum Geschoss → Bauteilklasse → Element |
|
||||
| `LayoutsPanel.tsx`, `PublishSection.tsx`, `ViewSnapshotsPanel.tsx` | Layouts als Baum, Publikationssätze (`[79]`), gespeicherte Ansichten |
|
||||
| `RoomBalancePanel.tsx` | Flächenbilanz mit den Abschnitten `AreaTableSection.tsx`, `KennzahlenSection.tsx`, `ProgramCompareSection.tsx`, `ElementScheduleSection.tsx`, `OpeningScheduleSection.tsx`, `RoomBookSection.tsx` |
|
||||
| `RoomBalancePanel.tsx` | Flächenbilanz mit den Abschnitten `AreaTableSection.tsx`, `KennzahlenSection.tsx`, `ProgramCompareSection.tsx`, `ElementScheduleSection.tsx`, `OpeningScheduleSection.tsx`, `RoomBookSection.tsx`, `UnitsSection.tsx` (Nutzungseinheiten und Wohnungsspiegel [278]; Ops in `unitOps.ts`, Tabelle `ReportTable.tsx`) |
|
||||
| `SitePanel.tsx` | Aktionen zu Gelände und Lage (Import, Gelände erzeugen, Baugrube; keine Import-Liste, [181]), mit `SiteRulesSection.tsx` (Baurecht), `SunStudySection.tsx` (Schatten) und `PointCloudSection.tsx` |
|
||||
| `BcfPanel.tsx`, `ClashPanel.tsx` | BCF-Themen und Kollisionsliste; Sitzungszustand in `bcfSession.ts` und `clashSession.ts` |
|
||||
| `HistoryPanel.tsx` | Undo-Verlauf mit Sprung auf einen Schritt |
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// Ein Abschnitt des Berichtsmodells (`ReportSection` aus `export/areaReport.ts`) als Tabelle im Panel. Gemeinsam für die
|
||||
// Flächentabelle (`AreaTableSection`) und den Wohnungsspiegel (`UnitsSection`): was hier steht, steht auch im Export.
|
||||
|
||||
import type { ReportSection } from "../export/areaReport";
|
||||
|
||||
export function ReportTable({ section: s }: { section: ReportSection }) {
|
||||
return (
|
||||
<div className="area-block">
|
||||
<div className="area-heading">{s.heading}</div>
|
||||
<div className="area-scroll">
|
||||
<table className={s.columns.length > 6 ? "area-table wide" : "area-table"}>
|
||||
<thead>
|
||||
<tr>
|
||||
{s.columns.map((c, i) => (
|
||||
<th key={i} className={i === 0 ? "" : "num"}>
|
||||
{c}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{s.rows.map((r, ri) => (
|
||||
<tr key={ri} className={r.total ? "total" : ""}>
|
||||
{r.cells.map((c, ci) =>
|
||||
typeof c === "string" ? (
|
||||
// Kurze Textzellen in Zahlenspalten (Strich, ja/nein) stehen wie die Zahlen rechts.
|
||||
<td key={ci} data-label={ci > 0 ? s.columns[ci] : undefined} className={ci > 0 && c.length <= 4 ? "num" : undefined}>
|
||||
{c}
|
||||
</td>
|
||||
) : (
|
||||
<td key={ci} data-label={ci > 0 ? s.columns[ci] : undefined} className="num">
|
||||
{c.n === null ? "–" : c.n.toFixed(c.d)}
|
||||
</td>
|
||||
),
|
||||
)}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import type { Room } from "../model/types";
|
||||
import { roomDisplayName } from "../model/roomStamp";
|
||||
import { KennzahlenSection } from "./KennzahlenSection";
|
||||
import { AreaTableSection } from "./AreaTableSection";
|
||||
import { UnitsSection } from "./UnitsSection";
|
||||
import { CostEstimateSection } from "./CostEstimateSection";
|
||||
import { OpeningScheduleSection } from "./OpeningScheduleSection";
|
||||
import { ElementScheduleSection } from "./ElementScheduleSection";
|
||||
@@ -85,6 +86,9 @@ export function RoomBalancePanel() {
|
||||
{/* Flächentabelle und IVHB-Kennziffern. */}
|
||||
<AreaTableSection />
|
||||
|
||||
{/* Nutzungseinheiten und Wohnungsspiegel ([278]). */}
|
||||
<UnitsSection />
|
||||
|
||||
{/* Kostenschätzung grob ([209]). */}
|
||||
<CostEstimateSection />
|
||||
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
// Nutzungseinheiten und Wohnungsspiegel im Flächenbilanz-Panel ([278]).
|
||||
//
|
||||
// • Liste der Einheiten (Nummer, Typ, Nutzung, Farbe) bearbeiten, neue Einheit anlegen (leer oder aus der Raum-Auswahl),
|
||||
// Auswahl einer Einheit zuordnen, Einheit löschen (die Räume bleiben, ohne Einheit).
|
||||
// • Wohnungsspiegel (Einheiten mit Zimmerzahl und Flächen, Summe je Typ und je Geschoss) aus demselben Berichtsmodell wie
|
||||
// CSV/PDF (`buildAreaReport` mit `units`); gerechnet im Kern (`unit_schedule.rs`).
|
||||
// Alle Änderungen laufen über `applyChange` (Ops), eine Aktion = ein Undo-Schritt; Tippen verschmilzt über den coalesceKey.
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { t } from "../i18n";
|
||||
import { useStore } from "../state/appStore";
|
||||
import { UNIT_USAGES, buildAreaReport, computeUnitSchedule, exportProjectName, areaReportToCsv, buildAreaReportPdf } from "../core";
|
||||
import type { Project, UnitUsage } from "../model/types";
|
||||
import { ReportTable } from "./ReportTable";
|
||||
import { downloadCsv } from "./downloadFile";
|
||||
import { runWithAnonymityGate } from "../state/anonymityGate";
|
||||
import { addUnitOps, assignRoomsOps, deleteUnitOps, draftUnit, patchUnitOps } from "./unitOps";
|
||||
|
||||
/** Auswahlwert „keine Einheit“ (leer ist der Platzhalter). */
|
||||
const NONE = "-";
|
||||
|
||||
export function UnitsSection() {
|
||||
const project = useStore((s) => s.project);
|
||||
const applyChange = useStore((s) => s.applyChange);
|
||||
const notify = useStore((s) => s.notify);
|
||||
const selectedRooms = useStore((s) => s.selection.rooms);
|
||||
const units = project.units ?? [];
|
||||
const sched = useMemo(() => computeUnitSchedule(project), [project]);
|
||||
const rowOf = new Map(sched.units.map((r) => [r.id, r]));
|
||||
// Reihenfolge der Liste wie im Spiegel (nach Nummer natürlich sortiert).
|
||||
const ordered = sched.units.map((r) => units.find((u) => u.id === r.id)).filter((u): u is NonNullable<typeof u> => !!u);
|
||||
const report = useMemo(() => buildAreaReport(project, { areas: false, units: true }), [project]);
|
||||
const stem = `${exportProjectName(project) || "projekt"}-wohnungsspiegel`;
|
||||
|
||||
const add = (fromSelection: boolean) =>
|
||||
applyChange((p: Project) => addUnitOps(p, draftUnit(p, t("units.nameDefault")), fromSelection ? selectedRooms : []));
|
||||
const patch = (id: string, field: string, v: Parameters<typeof patchUnitOps>[2]) => applyChange((p: Project) => patchUnitOps(p, id, v), `unit:${id}:${field}`);
|
||||
const assign = (unitId: string) => {
|
||||
if (selectedRooms.length === 0 || unitId === "") return;
|
||||
applyChange((p: Project) => assignRoomsOps(p, selectedRooms, unitId === NONE ? undefined : unitId));
|
||||
};
|
||||
|
||||
const exportCsv = () => {
|
||||
if (!units.length) return notify(t("units.export.empty"));
|
||||
runWithAnonymityGate(project, [`${stem}.csv`], () => downloadCsv(`${stem}.csv`, areaReportToCsv(report)));
|
||||
};
|
||||
const exportPdf = () => {
|
||||
if (!units.length) return notify(t("units.export.empty"));
|
||||
runWithAnonymityGate(project, [`${stem}.pdf`], async () => {
|
||||
try {
|
||||
(await buildAreaReportPdf(report)).save(`${stem}.pdf`);
|
||||
} catch {
|
||||
notify(t("units.exportPdf.fail"));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="area-section units-section" data-testid="units-section">
|
||||
<div className="balance-head">
|
||||
<span className="balance-section-label">{t("units.title")}</span>
|
||||
<span className="area-actions">
|
||||
<button type="button" className="balance-export" onClick={exportCsv}>
|
||||
{t("units.exportCsv")}
|
||||
</button>
|
||||
<button type="button" className="balance-export" onClick={exportPdf}>
|
||||
{t("units.exportPdf")}
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{units.length === 0 && <div className="balance-empty">{t("units.empty")}</div>}
|
||||
|
||||
{ordered.length > 0 && (
|
||||
<div className="units-list">
|
||||
{ordered.map((u) => {
|
||||
const r = rowOf.get(u.id);
|
||||
return (
|
||||
<div key={u.id} className="units-row" data-testid="unit-row">
|
||||
<input
|
||||
type="text"
|
||||
className="kz-input units-name"
|
||||
value={u.name}
|
||||
title={t("units.nameHint")}
|
||||
placeholder={t("units.nameHint")}
|
||||
onChange={(e) => patch(u.id, "name", { name: e.target.value })}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
className="kz-input units-type"
|
||||
value={u.unitType ?? ""}
|
||||
title={t("units.typeHint")}
|
||||
placeholder={r?.typeLabel || t("units.autoType")}
|
||||
onChange={(e) => patch(u.id, "type", { unitType: e.target.value === "" ? undefined : e.target.value })}
|
||||
/>
|
||||
<button type="button" className="site-remove" title={t("units.delete")} aria-label={t("units.delete")} onClick={() => applyChange((p: Project) => deleteUnitOps(p, u.id))}>
|
||||
×
|
||||
</button>
|
||||
<select
|
||||
className="kz-input units-usage"
|
||||
value={u.usage ?? "living"}
|
||||
aria-label={t("units.col.usage")}
|
||||
title={t("units.col.usage")}
|
||||
onChange={(e) => patch(u.id, "usage", { usage: e.target.value as UnitUsage })}
|
||||
>
|
||||
{UNIT_USAGES.map((x) => (
|
||||
<option key={x} value={x}>
|
||||
{t(`units.usage.${x}`)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<input
|
||||
type="color"
|
||||
className="units-color"
|
||||
value={u.color ?? "#9aa4b2"}
|
||||
title={t("units.color")}
|
||||
aria-label={t("units.color")}
|
||||
onChange={(e) => patch(u.id, "color", { color: e.target.value })}
|
||||
/>
|
||||
<span className="units-facts" title={r ? t("units.rooms", { n: r.rooms }) : undefined}>
|
||||
{r ? `${r.usage === "living" ? t("units.zi", { n: r.roomCount }) + " · " : ""}${r.hnf.toFixed(1)} m²` : ""}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="kz-program-actions">
|
||||
<button type="button" className="kz-add" onClick={() => add(false)}>
|
||||
{t("units.add")}
|
||||
</button>
|
||||
<button type="button" className="kz-add" disabled={selectedRooms.length === 0} onClick={() => add(true)}>
|
||||
{t("units.addFromSelection")}
|
||||
</button>
|
||||
{units.length > 0 && (
|
||||
<select className="kz-input" value="" disabled={selectedRooms.length === 0} aria-label={t("units.assignTo")} title={t("units.assignTo")} onChange={(e) => assign(e.target.value)}>
|
||||
<option value="">{t("units.assign")}</option>
|
||||
<option value={NONE}>{t("units.field.none")}</option>
|
||||
{ordered.map((u) => (
|
||||
<option key={u.id} value={u.id}>
|
||||
{u.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{report.sections.map((s, si) => (
|
||||
<ReportTable key={si} section={s} />
|
||||
))}
|
||||
{report.sections.length > 0 && (
|
||||
<ul className="area-notes">
|
||||
{report.notes.map((n, i) => (
|
||||
<li key={i}>{n}</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { t } from "../../i18n";
|
||||
import { Dropdown } from "../../ui/Dropdown";
|
||||
import type { SiaCategory, Project, Room } from "../../model/types";
|
||||
import { SIA_CATEGORIES, siaLabelFull, patchItems, roomBook, ROOM_USAGES, usageLabel } from "../../core";
|
||||
import { addUnitOps, assignRoomsOps, draftUnit } from "../unitOps";
|
||||
import { formatM } from "../../model/types";
|
||||
import { useStore } from "../../state/appStore";
|
||||
import type { TranslationKey } from "../../i18n";
|
||||
@@ -82,6 +83,78 @@ export function RoomSection({
|
||||
|
||||
{/* [158] Raumbuch: Nutzung und Oberflächen. */}
|
||||
<RoomFinishFields roomId={roomId} />
|
||||
|
||||
{/* [278] Nutzungseinheit und Zimmer-Zählung (Wohnungsspiegel). */}
|
||||
<RoomUnitFields roomId={roomId} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const UNIT_NONE = "";
|
||||
const UNIT_NEW = "__new";
|
||||
const UNIT_MIXED = "__mixed";
|
||||
/** Zimmer-Zählung: automatisch (aus der Nutzung) oder fest 0 / 0.5 / 1 / 1.5 / 2. */
|
||||
const ROOM_COUNTS = [0, 0.5, 1, 1.5, 2] as const;
|
||||
const COUNT_KEY = { 0: "units.roomCount.0", 0.5: "units.roomCount.05", 1: "units.roomCount.1", 1.5: "units.roomCount.15", 2: "units.roomCount.2" } as const satisfies Record<(typeof ROOM_COUNTS)[number], TranslationKey>;
|
||||
|
||||
/** [278] Einheit des Raums (Auswahl, „Neue Einheit …“) und Beitrag zur Zimmerzahl. Bei Mehrfachauswahl gilt die Wahl für alle
|
||||
* gewählten Räume in einem Undo-Schritt; unterschiedliche Werte zeigt die Auswahl als „gemischt“. */
|
||||
function RoomUnitFields({ roomId }: { roomId: string }) {
|
||||
const project = useStore((st) => st.project);
|
||||
const applyChange = useStore((s) => s.applyChange);
|
||||
const selectedRooms = useStore((s) => s.selection.rooms);
|
||||
const room = (project.rooms ?? []).find((r) => r.id === roomId);
|
||||
if (!room) return null;
|
||||
const ids = selectedRooms.length > 1 && selectedRooms.includes(roomId) ? [...selectedRooms] : [roomId];
|
||||
const group = ids.map((id) => (project.rooms ?? []).find((r) => r.id === id)).filter((r): r is Room => !!r);
|
||||
const unitIds = new Set(group.map((r) => r.unitId ?? UNIT_NONE));
|
||||
const counts = new Set(group.map((r) => (r.roomCount === undefined ? "auto" : String(r.roomCount))));
|
||||
const units = project.units ?? [];
|
||||
const known = new Set(units.map((u) => u.id));
|
||||
const unitValue = unitIds.size > 1 ? UNIT_MIXED : known.has(room.unitId ?? "") ? (room.unitId as string) : UNIT_NONE;
|
||||
const countValue = counts.size > 1 ? UNIT_MIXED : room.roomCount === undefined ? "auto" : String(room.roomCount);
|
||||
const onUnit = (v: string) => {
|
||||
if (v === UNIT_MIXED) return;
|
||||
if (v === UNIT_NEW) applyChange((p: Project) => addUnitOps(p, draftUnit(p, t("units.nameDefault")), ids), "unit:new");
|
||||
else applyChange((p: Project) => assignRoomsOps(p, ids, v === UNIT_NONE ? undefined : v), `room:${roomId}:unit`);
|
||||
};
|
||||
const onCount = (v: string) => {
|
||||
if (v === UNIT_MIXED) return;
|
||||
applyChange((p: Project) => patchItems(p, "rooms", ids, (r) => ({ ...r, roomCount: v === "auto" ? undefined : Number(v) }) as Room), `room:${roomId}:roomCount`);
|
||||
};
|
||||
const mixed = { value: UNIT_MIXED, label: t("objsettings.mixed"), disabled: true };
|
||||
return (
|
||||
<>
|
||||
<div className="objinfo-section-label">{t("units.title")}</div>
|
||||
<div className="objinfo-field">
|
||||
<span className="objinfo-flabel">{t("units.field")}</span>
|
||||
<Dropdown
|
||||
value={unitValue}
|
||||
onChange={onUnit}
|
||||
options={[
|
||||
...(unitValue === UNIT_MIXED ? [mixed] : []),
|
||||
{ value: UNIT_NONE, label: t("units.field.none") },
|
||||
...units.map((u) => ({ value: u.id, label: u.unitType ? `${u.name} · ${u.unitType}` : u.name })),
|
||||
{ value: UNIT_NEW, label: t("units.field.new") },
|
||||
]}
|
||||
title={t("units.field")}
|
||||
width={170}
|
||||
/>
|
||||
</div>
|
||||
<div className="objinfo-field">
|
||||
<span className="objinfo-flabel">{t("units.roomCount")}</span>
|
||||
<Dropdown
|
||||
value={countValue}
|
||||
onChange={onCount}
|
||||
options={[
|
||||
...(countValue === UNIT_MIXED ? [mixed] : []),
|
||||
{ value: "auto", label: t("units.roomCount.auto") },
|
||||
...ROOM_COUNTS.map((c) => ({ value: String(c), label: t(COUNT_KEY[c]) })),
|
||||
]}
|
||||
title={t("units.roomCount.hint")}
|
||||
width={170}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// Ops der Nutzungseinheiten ([278]): Einheit anlegen, Räumen zuordnen, löschen. Jede Funktion liefert alle Ops einer
|
||||
// Benutzeraktion (Projekt-Feld `units` und `unitId` der Räume), damit `applyChange` sie zu EINEM Undo-Schritt macht.
|
||||
|
||||
import type { Project, Room, Unit } from "../model/types";
|
||||
import { setField, patchItems, nextUnitName, newUnitId } from "../core";
|
||||
import type { CoreOp } from "../core";
|
||||
|
||||
/** Räume `roomIds` der Einheit `unitId` zuordnen (`undefined` = keiner Einheit). */
|
||||
export function assignRoomsOps(p: Project, roomIds: readonly string[], unitId: string | undefined): CoreOp[] {
|
||||
return patchItems(p, "rooms", roomIds, (r) => ({ ...r, unitId }) as Room);
|
||||
}
|
||||
|
||||
/** Neue Einheit anlegen und (optional) `roomIds` ihr zuordnen. `id` kommt vom Aufrufer, damit er sie danach kennt. */
|
||||
export function addUnitOps(p: Project, unit: Unit, roomIds: readonly string[] = []): CoreOp[] {
|
||||
return [setField("units", [...(p.units ?? []), unit]), ...assignRoomsOps(p, roomIds, unit.id)];
|
||||
}
|
||||
|
||||
/** Vorschlag einer neuen Einheit (Name „Whg n“, frische id). */
|
||||
export function draftUnit(p: Project, namePrefix: string): Unit {
|
||||
const units = p.units ?? [];
|
||||
return { id: newUnitId(units), name: nextUnitName(units, namePrefix) };
|
||||
}
|
||||
|
||||
/** Einheit ändern. */
|
||||
export function patchUnitOps(p: Project, id: string, patch: Partial<Unit>): CoreOp[] {
|
||||
const units = p.units ?? [];
|
||||
return [setField("units", units.map((u) => (u.id === id ? { ...u, ...patch } : u)))];
|
||||
}
|
||||
|
||||
/** Einheit löschen; ihre Räume bleiben, ohne Einheit. */
|
||||
export function deleteUnitOps(p: Project, id: string): CoreOp[] {
|
||||
const next = (p.units ?? []).filter((u) => u.id !== id);
|
||||
return [setField("units", next.length > 0 ? next : undefined), ...patchItems(p, "rooms", (r) => r.unitId === id, (r) => ({ ...r, unitId: undefined }) as Room)];
|
||||
}
|
||||
Reference in new issue
Block a user