From c15e69f7b5a1521d09cda0d8761f14621a6964ce Mon Sep 17 00:00:00 2001 From: MarcoWS00 Date: Fri, 9 Oct 2026 16:50:09 +0200 Subject: [PATCH] copy unit --- src/actions/rk2/api-units.ts | 32 ++++++++- src/components/rk2/units/unitCopyDialog.tsx | 80 +++++++++++++++++++++ src/components/rk2/units/unitEdit.tsx | 40 +++++++++++ src/types/anlagen/units.ts | 8 +++ 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/components/rk2/units/unitCopyDialog.tsx diff --git a/src/actions/rk2/api-units.ts b/src/actions/rk2/api-units.ts index a5c9e95..7b460bc 100644 --- a/src/actions/rk2/api-units.ts +++ b/src/actions/rk2/api-units.ts @@ -2,13 +2,43 @@ import { Sites } from "@/_config/sites"; import { ComponentCreate, ComponentUpdate } from "@/types/anlagen/components"; -import { UnitCreate, UnitResponse } from "@/types/anlagen/units"; +import { UnitCopy, UnitCreate, UnitResponse } from "@/types/anlagen/units"; import { revalidatePath } from "next/cache"; const url = "http://localhost:3000/rk2/"; const url_units = url + "units" const comp_update = "component" const comp_add = "addComponent" + +export async function api_units_delete(id: string): Promise> { + try { + const response = await fetch(`${url_units}/${encodeURIComponent(id)}`, { method: 'DELETE' }); + const result = await response.json(); + if (!response.ok) return { success: false, errors: Array.isArray(result.message) ? result.message : [result.message ?? 'Löschen fehlgeschlagen'] }; + revalidatePath('/rk2/units'); + revalidatePath('/rk2/signals'); + revalidatePath('/rk2/signals/assignment'); + revalidatePath('/rk2/signals/assignments'); + return { success: true, data: result }; + } catch { + return { success: false, errors: ['Löschen fehlgeschlagen. Bitte die Unit-Liste prüfen und gegebenenfalls erneut versuchen.'] }; + } +} + +export async function api_units_copy(id: string, data: UnitCopy): Promise> { + try { + const response = await fetch(`${url_units}/${encodeURIComponent(id)}/copy`, { + method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), + }); + const result = await response.json(); + if (!response.ok) return { success: false, errors: Array.isArray(result.message) ? result.message : [result.message ?? 'Kopieren fehlgeschlagen'] }; + revalidatePath('/rk2/units'); + revalidatePath('/rk2/signals'); + return { success: true, data: result }; + } catch { + return { success: false, errors: ['Keine Verbindung zum Backend. Bitte vor erneutem Kopieren die Unit-Liste prüfen.'] }; + } +} //----------------------------------------------------------------------------- //--- Units //----------------------------------------------------------------------------- diff --git a/src/components/rk2/units/unitCopyDialog.tsx b/src/components/rk2/units/unitCopyDialog.tsx new file mode 100644 index 0000000..28dfbb4 --- /dev/null +++ b/src/components/rk2/units/unitCopyDialog.tsx @@ -0,0 +1,80 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; +import { api_units_copy } from "@/actions/rk2/api-units"; +import Modal from "@/components/ui/Modal"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { GroupSelect } from "../groups/groupSelect"; +import type { GroupResponse } from "@/types/anlagen/groups"; +import type { UnitCopy, UnitResponse } from "@/types/anlagen/units"; + +export function UnitCopyDialog({ unit, groups, onClose }: { + unit: UnitResponse; groups: GroupResponse[]; onClose: () => void; +}) { + const router = useRouter(); + const [data, setData] = useState({ + name: unit.name, groupId: unit.groupId || null, prefix: unit.prefix ?? '', bmk: unit.bmk ?? '', postfix: unit.postfix ?? '', + }); + const [selectGroup, setSelectGroup] = useState(false); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + const group = groups.find((item) => item.id === data.groupId); + + const submit = async (event: React.FormEvent) => { + event.preventDefault(); + if (busy) return; + setBusy(true); + setError(''); + try { + const result = await api_units_copy(unit.id, data); + if (!result.success) { + setError(Array.isArray(result.errors) ? result.errors.join(', ') : String(result.errors)); + setBusy(false); + return; + } + router.push(`/rk2/units/edit/${encodeURIComponent(result.data.id)}`); + router.refresh(); + onClose(); + } catch { + setError('Kopieren fehlgeschlagen. Bitte vor erneutem Kopieren die Unit-Liste prüfen.'); + setBusy(false); + } + }; + + return { if (!busy) onClose(); }}> +
+

Unit kopieren

+

Komponenten und Signale werden kopiert. IO-Zuordnungen bleiben leer. Kopiert wird der gespeicherte Stand.

+
+
+
+ +
+ {group?.name ?? 'Ohne Gruppe'} + + +
+ {selectGroup &&
+ { + setData((current) => ({ ...current, groupId: selected.id })); + setSelectGroup(false); + }} /> +
} +
+ {(['prefix', 'bmk', 'name', 'postfix'] as const).map((key) =>
+ + setData((current) => ({ ...current, [key]: event.target.value }))} /> +
)} +
+ {error &&

{error}

} +
+ + +
+
+
+
; +} diff --git a/src/components/rk2/units/unitEdit.tsx b/src/components/rk2/units/unitEdit.tsx index 653b4ed..5596bc5 100644 --- a/src/components/rk2/units/unitEdit.tsx +++ b/src/components/rk2/units/unitEdit.tsx @@ -9,6 +9,10 @@ import { useGroups } from "@/hooks/rk2/useGroups"; import { UnitCreateForm } from "./unit_createForm"; import { GroupSelect } from "../groups/groupSelect"; import { ComponentDefaults, ComponentTypeDefinition } from "@/types/anlagen/components"; +import { UnitCopyDialog } from "./unitCopyDialog"; +import { Button } from "@/components/ui/button"; +import { useRouter } from "next/navigation"; +import { api_units_delete } from "@/actions/rk2/api-units"; @@ -28,6 +32,30 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen const { getGroupById } = useGroups({ groups }); const [showSelectGroup, setShowSelectGroup] = useState(false); + const [showCopy, setShowCopy] = useState(false); + const [showDelete, setShowDelete] = useState(false); + const [deleting, setDeleting] = useState(false); + const [deleteError, setDeleteError] = useState(''); + const router = useRouter(); + + const deleteUnit = async () => { + if (!selectedUnit || deleting) return; + setDeleting(true); + setDeleteError(''); + try { + const result = await api_units_delete(selectedUnit.id); + if (!result.success) { + setDeleteError(result.errors.join(', ')); + setDeleting(false); + return; + } + router.push('/rk2/units'); + router.refresh(); + } catch { + setDeleteError('Löschen fehlgeschlagen. Bitte die Unit-Liste prüfen.'); + setDeleting(false); + } + }; const [selectedGroup, setSelectedGroup] = useState(); @@ -55,6 +83,18 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen return (
+ {selectedUnit && } + {showCopy && selectedUnit && setShowCopy(false)} />} + {selectedUnit && } + {showDelete && selectedUnit && { if (!deleting) setShowDelete(false); }}> +

Unit löschen?

+

„{selectedUnit.name}“ mit allen Komponenten und Signalen endgültig löschen? Die IO-Datensätze bleiben erhalten.

+ {deleteError &&

{deleteError}

} +
+ + +
+
} setShowSelectGroup(true)} unitIntial={selectedUnit} componentTypes={componentTypes} componentDefaults={componentDefaults} initialComponentId={initialComponentId}/> diff --git a/src/types/anlagen/units.ts b/src/types/anlagen/units.ts index 963ae08..9eb590a 100644 --- a/src/types/anlagen/units.ts +++ b/src/types/anlagen/units.ts @@ -36,6 +36,14 @@ export interface UnitCreate { } +export interface UnitCopy { + name: string; + groupId?: string | null; + prefix: string; + bmk: string; + postfix: string; +} + export interface UnitUpdateBase { name?: string,