"use client"; import { useEffect, useState } from "react"; import Modal from "@/components/ui/Modal"; import { Anlage } from "@/types/rk"; import { GroupResponse } from "@/types/anlagen/groups"; import { UnitResponse } from "@/types/anlagen/units"; 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"; import { SaveTemplateDialog } from './saveTemplateDialog'; interface Params { initialComponentId?: string; anlage: Anlage, groups: GroupResponse[], selectedUnit?: UnitResponse, componentTypes: ComponentTypeDefinition[] componentDefaults: ComponentDefaults[] } export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componentDefaults, initialComponentId }: Params) => { const { getGroupById } = useGroups({ groups }); const [showSelectGroup, setShowSelectGroup] = useState(false); const [showCopy, setShowCopy] = useState(false); const [showTemplate, setShowTemplate] = 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(); useEffect(() => { if (selectedUnit && selectedUnit.groupId) { const pGroup = getGroupById(selectedUnit.groupId) setSelectedGroup(() => pGroup) } }, [selectedUnit]) useEffect(() => { if (selectedGroup) { const pGroup = getGroupById(selectedGroup.id) setSelectedGroup(() => pGroup) } }, [selectedGroup]) const handlerGroupSelect = (group: GroupResponse) => { setSelectedGroup(() => group); setShowSelectGroup(false); } //groupIntial={selectedGroup} parentGroup={selectedGroup} onResetParent={() => setSelectedGroup(() => undefined)} return (
{selectedUnit && } {showTemplate && selectedUnit && setShowTemplate(false)} />} {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}/> {showSelectGroup && setShowSelectGroup(false)} > }
) }