diff --git a/src/actions/rk2/api-unit-templates.ts b/src/actions/rk2/api-unit-templates.ts new file mode 100644 index 0000000..491cebe --- /dev/null +++ b/src/actions/rk2/api-unit-templates.ts @@ -0,0 +1,39 @@ +'use server'; + +import { revalidatePath } from 'next/cache'; +import { UnitTemplate, UnitTemplateData } from '@/types/anlagen/unit-templates'; +import { UnitCopy, UnitResponse } from '@/types/anlagen/units'; + +const url = 'http://localhost:3000/rk2/unit-templates'; +async function request(path: string, method: string, data?: unknown): Promise> { + try { + const response = await fetch(url + path, { + method, cache: 'no-store', headers: { 'Content-Type': 'application/json' }, + ...(data !== undefined && { body: JSON.stringify(data) }), + }); + const result = await response.json(); + if (!response.ok) return { success: false, errors: Array.isArray(result.message) ? result.message : [result.message ?? 'Aktion fehlgeschlagen'] }; + if (method !== 'GET') revalidatePath('/rk2/units/templates'); + return { success: true, data: result }; + } catch { + return { success: false, errors: ['Keine Verbindung zum Backend. Bitte vor erneutem Speichern oder Einfügen die Liste prüfen.'] }; + } +} +export async function api_templates_list() { return request('', 'GET'); } +export async function api_templates_save(data: UnitTemplateData, id?: string) { + return request(id ? `/${encodeURIComponent(id)}` : '', id ? 'PUT' : 'POST', data); +} +export async function api_templates_fromUnit(id: string, name: string) { + return request(`/from-unit/${encodeURIComponent(id)}`, 'POST', { name }); +} +export async function api_templates_delete(id: string) { return request<{ success: boolean }>(`/${encodeURIComponent(id)}`, 'DELETE'); } +export async function api_templates_insert(id: string, data: UnitCopy) { + const result = await request(`/${encodeURIComponent(id)}/instantiate`, 'POST', data); + if (result.success) { + revalidatePath('/rk2/units'); + revalidatePath('/rk2/signals'); + revalidatePath('/rk2/signals/assignment'); + revalidatePath('/rk2/signals/assignments'); + } + return result; +} diff --git a/src/app/(pages)/rk2/ios/page.tsx b/src/app/(pages)/rk2/ios/page.tsx new file mode 100644 index 0000000..32e0f71 --- /dev/null +++ b/src/app/(pages)/rk2/ios/page.tsx @@ -0,0 +1,21 @@ +import Link from "next/link"; +import { api_anlage_getSelected } from "@/actions/rk/api-anlage"; +import { api_ios } from "@/actions/rk2/api-ios"; +import { api_signals } from "@/actions/rk2/api-signals"; +import { api_units } from "@/actions/rk2/api-units"; +import IoOverview from "@/components/rk2/ios/ioOverview"; +import Breadcrumbs from "@/components/ui/navigation/breadcrumbs"; + +export default async function IosPage() { + const anlage = await api_anlage_getSelected(); + const [ios, signals, units] = await Promise.all([api_ios(), api_signals(), api_units()]); + + return
+ +

IO-Übersicht

+ IOs zu Signalen zuordnen + io.anlageId === anlage.id)} + signals={signals.filter((signal) => signal.anlageId === anlage.id)} + units={units.filter((unit) => unit.anlageId === anlage.id)} /> +
; +} diff --git a/src/app/(pages)/rk2/layout.tsx b/src/app/(pages)/rk2/layout.tsx index ea1e1e0..ab4cdb1 100644 --- a/src/app/(pages)/rk2/layout.tsx +++ b/src/app/(pages)/rk2/layout.tsx @@ -45,6 +45,9 @@ export default function AnlageLayout({ children }: Props) {
  • {Sites.rk2.signals.name}
  • +
  • + IOs +
  • @@ -56,4 +59,4 @@ export default function AnlageLayout({ children }: Props) { ); -} \ No newline at end of file +} diff --git a/src/app/(pages)/rk2/units/page.tsx b/src/app/(pages)/rk2/units/page.tsx index 4c74fb8..57edf9d 100644 --- a/src/app/(pages)/rk2/units/page.tsx +++ b/src/app/(pages)/rk2/units/page.tsx @@ -28,6 +28,7 @@ export default async function PageUnits() { Units + Unit-Vorlagen / Aus Vorlage hinzufügen + +

    {visible.length} von {ios.length} IOs · {ios.filter((io) => owners.has(io.id)).length} mit Signal

    +
    + + + {['Adresse', 'Funktionstext', 'Seite', 'Signal-Verknüpfungen'].map((title) => )} + + {visible.map((io) => + + + + + )} +
    {title}
    {io.adrType} {io.adrByte}{io.adrBit == null ? '' : `.${io.adrBit}`}{io.functionText || '—'}{[io.seite, io.seiteName].filter((value) => value !== undefined && value !== '').join(' · ') || '—'}{(owners.get(io.id) ?? []).length === 0 ? Ohne Signal + : (owners.get(io.id) ?? []).map((signal) => { + const context = signal.componentId ? contexts.get(signal.componentId) : undefined; + return
    + {context ? {signal.name} : {signal.name}} + {context &&
    {[context.unit.bmk, context.unit.name, formatComponentName(context.component)].filter(Boolean).join(' / ')}
    } + {signal.description &&
    {signal.description}
    } +
    ; + })}
    + {visible.length === 0 &&

    {ios.length === 0 ? 'Keine IOs für diese Anlage vorhanden.' : 'Keine IOs passend zum Filter.'}

    } +
    + ; +} diff --git a/src/components/rk2/units/saveTemplateDialog.tsx b/src/components/rk2/units/saveTemplateDialog.tsx new file mode 100644 index 0000000..74de26d --- /dev/null +++ b/src/components/rk2/units/saveTemplateDialog.tsx @@ -0,0 +1,34 @@ +'use client'; +import { useState } from 'react'; +import Link from 'next/link'; +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 { api_templates_fromUnit } from '@/actions/rk2/api-unit-templates'; +import { UnitResponse } from '@/types/anlagen/units'; + +export function SaveTemplateDialog({ unit, onClose }: { unit: UnitResponse; onClose: () => void }) { + const [name, setName] = useState(unit.name); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + const [saved, setSaved] = useState(false); + return { if (!busy) onClose(); }}> +
    { + event.preventDefault(); + if (busy || saved) return; + setBusy(true); setError(''); + const result = await api_templates_fromUnit(unit.id, name); + if (result.success) setSaved(true); else setError(result.errors.join(', ')); + setBusy(false); + }}> +

    Als Vorlage speichern

    +

    Der gespeicherte Stand einschließlich Komponenten und Signalen wird übernommen. Speichere Änderungen an der Unit vorher.

    + + setName(e.target.value)} /> + {error &&

    {error}

    } + {saved ?

    Vorlage gespeichert. Vorlagen öffnen

    : } + +
    +
    ; +} diff --git a/src/components/rk2/units/unitCopyDialog.tsx b/src/components/rk2/units/unitCopyDialog.tsx index 28dfbb4..58b2155 100644 --- a/src/components/rk2/units/unitCopyDialog.tsx +++ b/src/components/rk2/units/unitCopyDialog.tsx @@ -11,8 +11,10 @@ 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 }: { +export function UnitCopyDialog({ unit, groups, onClose, onCopy, title = 'Unit kopieren' }: { unit: UnitResponse; groups: GroupResponse[]; onClose: () => void; + onCopy?: (data: UnitCopy) => Promise>; + title?: string; }) { const router = useRouter(); const [data, setData] = useState({ @@ -29,7 +31,7 @@ export function UnitCopyDialog({ unit, groups, onClose }: { setBusy(true); setError(''); try { - const result = await api_units_copy(unit.id, data); + const result = onCopy ? await onCopy(data) : await api_units_copy(unit.id, data); if (!result.success) { setError(Array.isArray(result.errors) ? result.errors.join(', ') : String(result.errors)); setBusy(false); @@ -46,7 +48,7 @@ export function UnitCopyDialog({ unit, groups, onClose }: { return { if (!busy) onClose(); }}>
    -

    Unit kopieren

    +

    {title}

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

    diff --git a/src/components/rk2/units/unitEdit.tsx b/src/components/rk2/units/unitEdit.tsx index 5596bc5..4c4cd90 100644 --- a/src/components/rk2/units/unitEdit.tsx +++ b/src/components/rk2/units/unitEdit.tsx @@ -13,6 +13,7 @@ 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'; @@ -33,6 +34,7 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen 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(''); @@ -83,6 +85,8 @@ export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componen return (
    + {selectedUnit && } + {showTemplate && selectedUnit && setShowTemplate(false)} />} {selectedUnit && } {showCopy && selectedUnit && setShowCopy(false)} />} {selectedUnit && } diff --git a/src/components/rk2/units/unitTemplates.tsx b/src/components/rk2/units/unitTemplates.tsx new file mode 100644 index 0000000..f50e5d5 --- /dev/null +++ b/src/components/rk2/units/unitTemplates.tsx @@ -0,0 +1,147 @@ +'use client'; + +import { useState } from 'react'; +import { useRouter } from 'next/navigation'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Select } from '@/components/ui/select'; +import Modal from '@/components/ui/Modal'; +import { UnitTemplate, UnitTemplateData } from '@/types/anlagen/unit-templates'; +import { ComponentCreate, ComponentDefaults, ComponentTypeDefinition } from '@/types/anlagen/components'; +import { GroupResponse } from '@/types/anlagen/groups'; +import { UnitType } from '@/types/anlagen/units'; +import { SignalType } from '@/types/anlagen/signals'; +import { api_templates_save, api_templates_delete, api_templates_insert } from '@/actions/rk2/api-unit-templates'; +import { UnitCopyDialog } from './unitCopyDialog'; +import { PropertyField } from './propertyFields'; + +export function UnitTemplates({ initial, groups, componentTypes, componentDefaults }: { + initial: UnitTemplate[]; groups: GroupResponse[]; componentTypes: ComponentTypeDefinition[]; componentDefaults: ComponentDefaults[]; +}) { + const router = useRouter(); + const [templates, setTemplates] = useState(initial); + const [editing, setEditing] = useState(); + const [editingId, setEditingId] = useState(); + const [inserting, setInserting] = useState(); + const [deleting, setDeleting] = useState(); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(''); + const [status, setStatus] = useState(''); + const [addType, setAddType] = useState(componentTypes[0]?.type ?? ''); + const patchComponent = (index: number, patch: Partial) => setEditing(current => current && ({ + ...current, components: current.components.map((c, i) => i === index ? { ...c, ...patch } : c), + })); + const startEdit = (template?: UnitTemplate) => { + setError(''); setStatus(''); setEditingId(template?.id); + setEditing(template ? structuredClone(template) : { name: '', type: UnitType.BAND, components: [], signals: [] }); + }; + return
    + + {status &&

    {status}

    } + {!templates.length &&

    Noch keine Vorlagen. Lege eine Vorlage an oder speichere eine bestehende Unit als Vorlage.

    } + {templates.map(template =>
    +
    {template.name}

    {template.type} · {template.components.length} Komponenten · {template.signals.length} Signale

    + + + +
    )} + {inserting && ({ ...c, id: String(i) })) }} + onCopy={data => api_templates_insert(inserting.id, data)} onClose={() => setInserting(undefined)} />} + {deleting && { if (!busy) setDeleting(undefined); }}> +

    Vorlage „{deleting.name}“ löschen?

    +

    Bereits eingefügte Units bleiben erhalten.

    + {error &&

    {error}

    } + + +
    } + {editing && { if (!busy) setEditing(undefined); }}> + { + event.preventDefault(); + if (busy) return; + setBusy(true); setError(''); + const result = await api_templates_save(editing, editingId); + if (result.success) { + setTemplates(current => [...current.filter(t => t.id !== result.data.id), result.data].sort((a, b) => a.name.localeCompare(b.name))); + setEditing(undefined); setStatus('Vorlage gespeichert.'); router.refresh(); + } else setError(result.errors.join(', ')); + setBusy(false); + }}> +

    {editingId ? 'Vorlage bearbeiten' : 'Neue Unit-Vorlage'}

    +
    +
    + {(['name', 'prefix', 'bmk', 'postfix', 'info'] as const).map(key =>
    + + setEditing({ ...editing, [key]: e.target.value })} /> +
    )} +
    +
    +

    Komponenten

    + {editing.components.map((component, index) => { + const definition = componentTypes.find(t => t.type === component.type); + return
    +
    +
    + {(['name', 'prefix', 'postfix'] as const).map(key =>
    + patchComponent(index, { [key]: e.target.value })} /> +
    )} +
    patchComponent(index, { index: e.target.value === '' ? undefined : Number(e.target.value) })} />
    +
    +
    {definition?.properties.map(property => patchComponent(index, { properties: { ...component.properties, [key]: property.type === 'number' ? Number(value) : value } })} />)}
    +

    Signale

    + {editing.signals.map((signal, signalIndex) => signal.componentIndex === index &&
    +
    setEditing({ ...editing, signals: editing.signals.map((s, i) => i === signalIndex ? { ...s, name: e.target.value } : s) })} />
    +
    +
    setEditing({ ...editing, signals: editing.signals.map((s, i) => i === signalIndex ? { ...s, description: e.target.value } : s) })} />
    + +
    )} +
    + + +
    +
    ; + })} +
    +
    + +
    +
    + {error &&

    {error}

    } +
    + + +
    + +
    } +
    ; +} diff --git a/src/types/anlagen/unit-templates.ts b/src/types/anlagen/unit-templates.ts new file mode 100644 index 0000000..c00d30d --- /dev/null +++ b/src/types/anlagen/unit-templates.ts @@ -0,0 +1,21 @@ +import { ComponentCreate } from './components'; +import { SignalType } from './signals'; +import { UnitType } from './units'; + +export interface TemplateSignal { + componentIndex: number; + name: string; + type: SignalType; + description?: string; +} +export interface UnitTemplateData { + name: string; + type: UnitType; + info?: string; + prefix?: string; + bmk?: string; + postfix?: string; + components: ComponentCreate[]; + signals: TemplateSignal[]; +} +export interface UnitTemplate extends UnitTemplateData { id: string; }