stand2026_10_06
This commit is contained in:
1 parent
41f80ce947
commit
266f8c3b4f
199 files changed
+17061
-87
No files matched your search
@@ -0,0 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import { Anlage } from "@/types/rk";
|
||||
import Modal from "@/components/ui/Modal";
|
||||
import { GroupResponse } from "@/types/anlagen/groups";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useGroups } from "@/hooks/rk2/useGroups";
|
||||
import { UnitResponse } from "@/types/anlagen/units";
|
||||
import { useUnit } from "@/hooks/rk2/useUnit";
|
||||
import { ComponentResponse } from "@/types/anlagen/components";
|
||||
|
||||
|
||||
|
||||
interface Params {
|
||||
anlage?: Anlage,
|
||||
groups: GroupResponse[],
|
||||
units: UnitResponse[]
|
||||
}
|
||||
|
||||
export const AusgabeUnitInfo = ({ anlage, groups, units }: Params) => {
|
||||
|
||||
const {getPathToRoot} = useGroups({groups})
|
||||
|
||||
const getUnitInfo = (unit:UnitResponse) =>{
|
||||
let groupId = unit.groupId;
|
||||
|
||||
let uGroupPath = getPathToRoot(groupId);
|
||||
|
||||
let unitName = [unit.prefix,unit.bmk,unit.name]
|
||||
if(unit.postfix) unitName.push(unit.postfix)
|
||||
|
||||
let typ = unit.type;
|
||||
|
||||
//--- Components
|
||||
let txt = "";
|
||||
txt = uGroupPath.map((g)=>g.name).join('\t')+'\t';
|
||||
txt += "\t" + unitName.join('_')+'\t';
|
||||
//txt += "\tTyp:" + typ+'\n';
|
||||
|
||||
//txt += "\t\tKomponenten:"+'\n'
|
||||
unit.components.forEach((comp)=>{
|
||||
let cName:string[] = []
|
||||
comp.prefix && cName.push(comp.prefix);
|
||||
comp.name && cName.push(comp.name + (comp.index ? comp.index.toString():""));
|
||||
//comp.index && cName.push();
|
||||
comp.postfix && cName.push(comp.postfix);
|
||||
//,comp.name,comp.index,comp.postfix
|
||||
|
||||
txt += '\t' + cName.join('_')+''
|
||||
})
|
||||
|
||||
return txt;
|
||||
|
||||
}
|
||||
|
||||
const getComponentData = (comp:ComponentResponse) =>{
|
||||
const name = [comp.prefix, comp.name,comp.index, comp.postfix]
|
||||
}
|
||||
|
||||
const getText = ()=>{
|
||||
let txt = ""
|
||||
|
||||
units.forEach((u)=> txt += getUnitInfo(u)+`\n`)
|
||||
return txt;
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<textarea className="w-full h-100" value={getText()} />
|
||||
|
||||
|
||||
|
||||
</div >
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
"use client";
|
||||
|
||||
import { Anlage } from "@/types/rk";
|
||||
import { GroupSelect } from "./groupSelect";
|
||||
import { GroupForm } from "./groupForm";
|
||||
import Modal from "@/components/ui/Modal";
|
||||
import { GroupResponse } from "@/types/anlagen/groups";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useGroups } from "@/hooks/rk2/useGroups";
|
||||
|
||||
|
||||
|
||||
interface Params {
|
||||
anlage: Anlage,
|
||||
groups: GroupResponse[],
|
||||
selectedGroup?: GroupResponse
|
||||
}
|
||||
|
||||
export const GroupEdit = ({ anlage, groups, selectedGroup }: Params) => {
|
||||
|
||||
|
||||
const { getGroupById } = useGroups({ groups });
|
||||
const [showSelectParentGroup, setShowSelectParentGroup] = useState(false);
|
||||
const [selectedParentGroup, setSelectedParentGroup] = useState<GroupResponse>();
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedGroup && selectedGroup.parentId) {
|
||||
const pGroup = getGroupById(selectedGroup.parentId)
|
||||
setSelectedParentGroup(() => pGroup)
|
||||
}
|
||||
}, [selectedGroup])
|
||||
|
||||
|
||||
const handlerParentSelect = (group: GroupResponse) => {
|
||||
if (selectedGroup && selectedGroup.id == group.id) {
|
||||
setShowSelectParentGroup(false);
|
||||
return;
|
||||
}
|
||||
setSelectedParentGroup(() => group);
|
||||
setShowSelectParentGroup(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<div className="" >
|
||||
|
||||
<GroupForm
|
||||
anlage={anlage}
|
||||
groupIntial={selectedGroup}
|
||||
parentGroup={selectedParentGroup}
|
||||
onDisplaySelectParent={() => setShowSelectParentGroup(true)}
|
||||
onResetParent={() => setSelectedParentGroup(() => undefined)}
|
||||
/>
|
||||
{showSelectParentGroup &&
|
||||
<Modal onClose={() => setShowSelectParentGroup(false)} >
|
||||
<GroupSelect groups={groups} onSelect={handlerParentSelect} />
|
||||
</Modal>
|
||||
}
|
||||
|
||||
</div >
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { InputTextR } from "@/components/ui/inputTextR";
|
||||
import Toast from "@/components/ui/Toast";
|
||||
import { GroupCreate, GroupResponse } from "@/types/anlagen/groups";
|
||||
import { useGroup } from "@/hooks/rk2/useGroup";
|
||||
import { Anlage } from "@/types/rk";
|
||||
import { api_groups_create, api_groups_update } from "@/actions/rk2/api-groups";
|
||||
import MonButton from "@/components/ui/MonBotton";
|
||||
import { GroupSelect } from "./groupSelect";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
|
||||
|
||||
interface Params {
|
||||
anlage: Anlage,
|
||||
groupIntial?: GroupResponse,
|
||||
parentGroup?: GroupResponse,
|
||||
onDisplaySelectParent?: () => void,
|
||||
onResetParent: () => void
|
||||
|
||||
}
|
||||
|
||||
export const GroupForm = ({ anlage, groupIntial, onDisplaySelectParent, parentGroup, onResetParent }: Params) => {
|
||||
const router = useRouter();
|
||||
const { emptyGroup } = useGroup({ group: groupIntial });
|
||||
|
||||
const [id, setId] = useState<string>();
|
||||
const { register, handleSubmit, formState: { errors }, watch, reset, setValue } = useForm<GroupCreate>({ defaultValues: emptyGroup, })
|
||||
const [toast, setToast] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
|
||||
//--- GroupInit changed
|
||||
useEffect(() => {
|
||||
|
||||
if (groupIntial) {
|
||||
const { id, ...rest } = groupIntial;
|
||||
setId(id);
|
||||
reset({ ...emptyGroup, ...rest })
|
||||
} else {
|
||||
reset({ ...emptyGroup })
|
||||
}
|
||||
}, [groupIntial]);
|
||||
|
||||
//--- Parent Group hat sich geändert
|
||||
useEffect(() => {
|
||||
parentGroup ? setValue("parentId", parentGroup.id) : setValue("parentId", undefined);
|
||||
}, [parentGroup])
|
||||
|
||||
const onSubmit = async (data: GroupCreate) => {
|
||||
|
||||
setError(undefined);
|
||||
|
||||
const { id: id1, ...sendData } = data; // Id entfernen!
|
||||
|
||||
let response = id ? await api_groups_update(id, sendData) : await api_groups_create(data);
|
||||
if (response.success) {
|
||||
reset({ ...emptyGroup, ...response.data })
|
||||
setId(response.data.id);
|
||||
setToast("Erfolgreich gespeichert!");
|
||||
router.back();
|
||||
} else {
|
||||
setToast("Fehler beim Speichern!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className="flex flex-col p-4 " >
|
||||
<div className="relative inline-block">
|
||||
{toast && (
|
||||
<Toast
|
||||
message={toast}
|
||||
onClose={() => setToast(null)}
|
||||
/>
|
||||
)}
|
||||
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="">
|
||||
<div className="flex gap-8 border-1 border-gray-200 rounded-sm p-4 pt-8 shadow-md">
|
||||
|
||||
<section className="relative flex flex-col gap-1 border-1 border-gray-200 rounded-sm p-2 ">
|
||||
<div className="absolute -top-4 bg-gray-100 rounded-sm border-1 border-gray-200 w-20 ps-2 font-bold ">Gruppe</div>
|
||||
<InputTextR label="Name" {...register("name", { required: "Pflichtfeld" })} error={errors.name?.message} />
|
||||
<InputTextR label="Nr" {...register("nr")} error={errors.nr?.message} />
|
||||
<input type="hidden" {...register("parentId")} />
|
||||
</section>
|
||||
|
||||
<section className="flex flex-col gap-4 border-1 border-gray-200 rounded-sm p-2 ">
|
||||
{/* Eltern zuordnung */}
|
||||
<div className="">
|
||||
<div>Zuordnung </div>
|
||||
{parentGroup &&
|
||||
<div className="flex flex-row gap-2 pb-2 ">
|
||||
<div>Ändern</div>
|
||||
<div>{parentGroup.name}</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
{/* Eltern zuordnung ändern */}
|
||||
<div>
|
||||
<div className="flex flex-col gap-2 ps-8">
|
||||
<MonButton onClick={() => onResetParent()}>root</MonButton>
|
||||
<MonButton onClick={() => onDisplaySelectParent && onDisplaySelectParent()}>Auswahl</MonButton>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex gap-4">
|
||||
<button type="submit" className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600">Speichern</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</form >
|
||||
|
||||
{
|
||||
true &&
|
||||
<div>
|
||||
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
||||
</div>
|
||||
}
|
||||
</div >
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
"use client"
|
||||
|
||||
import { useGroups } from "@/hooks/rk2/useGroups";
|
||||
import { GroupResponse } from "@/types/anlagen/groups";
|
||||
|
||||
interface Props {
|
||||
groups: GroupResponse[],
|
||||
groupId?: string,
|
||||
}
|
||||
|
||||
|
||||
export const GroupPath = ({ groups, groupId }: Props) => {
|
||||
const { getPathToRoot } = useGroups({ groups: groups })
|
||||
|
||||
return (
|
||||
<>
|
||||
{groupId ?
|
||||
<div>{getPathToRoot(groupId).map((g)=>g.name).join(".")}</div>:<div></div>
|
||||
}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client"
|
||||
import { useGroups } from "@/hooks/rk2/useGroups"
|
||||
import { useGroupTree } from "@/hooks/rk2/useGroupTree"
|
||||
import { GroupResponse } from "@/types/anlagen/groups"
|
||||
import { GroupTreeView } from "./groupTreeView"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
|
||||
|
||||
|
||||
interface Props {
|
||||
groups: GroupResponse[]
|
||||
onSelect: (group: GroupResponse) => void
|
||||
}
|
||||
|
||||
export function GroupSelect({ groups, onSelect }: Props) {
|
||||
|
||||
const { getGroupById, groupsExtended } = useGroups({ groups })
|
||||
const { tree, close, closeAll, isOpen, open, openAll, openGroups, toggle, isSelected, select, selectedGroupId } = useGroupTree(groups);
|
||||
const [modalClose,setModalClose] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedGroupId) {
|
||||
const group = getGroupById(selectedGroupId);
|
||||
if (group){
|
||||
onSelect(group)
|
||||
setModalClose(true)
|
||||
}
|
||||
}
|
||||
}, [selectedGroupId]);
|
||||
|
||||
const handelModalClose = ()=>{
|
||||
setModalClose(false);
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
<div className="flex flex-col gap-8">
|
||||
<GroupTreeView isSelected={isSelected} select={select} nodes={tree} isOpen={isOpen} toggle={toggle} />
|
||||
</div>
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
"use client"
|
||||
import { GroupTree } from "@/types/anlagen/groups"
|
||||
|
||||
interface GroupTreeViewProps {
|
||||
nodes: GroupTree[]
|
||||
isOpen: (id: string) => boolean
|
||||
toggle: (id: string) => void
|
||||
isSelected: (id: string) => boolean
|
||||
select: (id: string|undefined) => void
|
||||
}
|
||||
|
||||
export function GroupTreeView({ nodes, isOpen, toggle, select, isSelected }: GroupTreeViewProps) {
|
||||
return (
|
||||
<ul className="ps-6">
|
||||
{nodes.map((node) => {
|
||||
const id = node.group.id
|
||||
const hasChildren = node.childs.length > 0
|
||||
const open = isOpen(id)
|
||||
const selected = isSelected(id)
|
||||
|
||||
return (
|
||||
<li key={id}>
|
||||
<div
|
||||
onClick={() => select(id)}
|
||||
className={selected ? "bg-sky-100" : ""}
|
||||
>
|
||||
{hasChildren && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation()
|
||||
toggle(id)
|
||||
}}
|
||||
>
|
||||
{open ? "▼" : "▶"}
|
||||
</button>
|
||||
)}
|
||||
|
||||
<span>{node.group.name}</span>
|
||||
</div>
|
||||
|
||||
{open && hasChildren && (
|
||||
<GroupTreeView
|
||||
nodes={node.childs}
|
||||
isOpen={isOpen}
|
||||
toggle={toggle}
|
||||
isSelected={isSelected}
|
||||
select={select}
|
||||
/>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
"use client"
|
||||
|
||||
import { Sites } from "@/_config/sites"
|
||||
import { api_groups_delete } from "@/actions/rk2/api-groups"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { ListItem, ListItemAction } from "@/components/ui/list"
|
||||
import { GroupResponse } from "@/types/anlagen/groups"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
|
||||
|
||||
|
||||
interface Props {
|
||||
group: GroupResponse,
|
||||
parent?: GroupResponse
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function GroupViewItem({ group, parent }: Props) {
|
||||
|
||||
let router = useRouter()
|
||||
|
||||
return (
|
||||
<ListItem key={group.id} size={"md"} >
|
||||
{parent ? <div className="w-20">
|
||||
{parent.name}
|
||||
</div> : <div className="w-20">root</div>}
|
||||
|
||||
<div className="w-30">
|
||||
{group.name}
|
||||
</div>
|
||||
<div className="w-30">
|
||||
{group.nr}
|
||||
</div>
|
||||
<ListItemAction>
|
||||
<Button variant={"secondary"} size={"xs"} onClick={()=>router.push(`${Sites.rk2.groups.edit.url}/${group.id}`)}>Ändern</Button>
|
||||
</ListItemAction>
|
||||
<ListItemAction>
|
||||
<Button variant={"danger"} onClick={()=>api_groups_delete(group.id)} size={"xs"}>Löschen</Button>
|
||||
</ListItemAction>
|
||||
</ListItem>
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
"use client"
|
||||
import { useGroups } from "@/hooks/rk2/useGroups"
|
||||
import { useGroupTree } from "@/hooks/rk2/useGroupTree"
|
||||
import { GroupResponse } from "@/types/anlagen/groups"
|
||||
import { GroupTreeView } from "./groupTreeView"
|
||||
import { GroupViewItem } from "./groupViewItem"
|
||||
import { GroupForm } from "./groupForm"
|
||||
import { Anlage } from "@/types/rk"
|
||||
import Modal from "@/components/ui/Modal"
|
||||
import { GroupSelect } from "./groupSelect"
|
||||
import { GroupEdit } from "./groupEdit"
|
||||
import MonButton from "@/components/ui/MonBotton"
|
||||
import Link from "next/link"
|
||||
import { Sites } from "@/_config/sites"
|
||||
import { List, ListItem } from "@/components/ui/list"
|
||||
|
||||
|
||||
|
||||
|
||||
interface Props {
|
||||
anlage: Anlage,
|
||||
groups: GroupResponse[]
|
||||
}
|
||||
|
||||
export function GroupViewList({ anlage, groups }: Props) {
|
||||
|
||||
const { getGroupById, groupsExtended } = useGroups({ groups })
|
||||
const { tree, close, closeAll, isOpen, open, openAll, openGroups, toggle, isSelected, select, selectedGroupId } = useGroupTree(groups);
|
||||
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8">
|
||||
|
||||
{/* Gruppe neu anlegen */}
|
||||
<div>
|
||||
<Link href={Sites.rk2.groups.new.url}>{Sites.rk2.groups.new.name}</Link>
|
||||
</div>
|
||||
|
||||
|
||||
{/* Grupplist flat */}
|
||||
<List size={"md"} className="w-130">
|
||||
|
||||
{groupsExtended.map((item) =>
|
||||
|
||||
<GroupViewItem key={item.group.id} group={item.group} parent={item.parent} />
|
||||
|
||||
|
||||
)}
|
||||
|
||||
</List>
|
||||
|
||||
|
||||
{/* Grupplist as Tree */}
|
||||
<div className="flex gap-8">
|
||||
<div className="ps-4">
|
||||
<div onClick={() => select(undefined)}>root</div>
|
||||
<GroupTreeView isSelected={isSelected} select={select} nodes={tree} isOpen={isOpen} toggle={toggle} />
|
||||
</div>
|
||||
<div className="">
|
||||
{selectedGroupId && <div>
|
||||
<GroupEdit anlage={anlage} groups={groups} selectedGroup={getGroupById(selectedGroupId)} />
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
"use client"
|
||||
|
||||
import { api_signals, api_signals_create } from "@/actions/rk2/api-signals";
|
||||
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 { useSignal } from "@/hooks/rk2/useSignal";
|
||||
import { ComponentResponse } from "@/types/anlagen/components"
|
||||
import { SignalCreate, SignalResponse, SignalType } from "@/types/anlagen/signals"
|
||||
import { useEffect } from "react";
|
||||
|
||||
|
||||
|
||||
interface Props {
|
||||
componente: ComponentResponse,
|
||||
initSignal?: SignalResponse,
|
||||
}
|
||||
|
||||
export function SignalForm({ componente, initSignal }: Props) {
|
||||
|
||||
const { setSignal, setValue, signalData, string_to_signalType } = useSignal({});
|
||||
|
||||
/*
|
||||
useEffect(() => {
|
||||
if (initSignal) setSignal({...initSignal,componentId:componente.id});
|
||||
}, [initSignal]);
|
||||
*/
|
||||
useEffect(() => {
|
||||
setValue("componentId",componente.id);
|
||||
//if (initSignal) setSignal({...initSignal,componentId:componente.id});
|
||||
// setSignal({componentId:componente.id});
|
||||
}, [componente]);
|
||||
|
||||
const handlerSave = ()=>{
|
||||
console.log("save")
|
||||
const saveData:SignalCreate ={
|
||||
componentId:signalData.componentId!,
|
||||
name:signalData.name!,
|
||||
description:signalData.description!,
|
||||
type:signalData.type!,
|
||||
}
|
||||
api_signals_create(saveData);
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ border: "1px solid #ccc", padding: 16, marginBottom: 16, borderRadius: 8, }} >
|
||||
|
||||
{/*---Form Data */}
|
||||
<div className="flex gap-4">
|
||||
<div className="w-30 flex flex-row">
|
||||
<Label size={"xs"} >Name</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={signalData.name} onChange={(e) => setValue('name', e.target.value)} />
|
||||
</div>
|
||||
<div className="w-50 flex flex-row">
|
||||
<Label className="w-30" size={"xs"} >Signal Typ</Label>
|
||||
<Select sizeVariant={"xs"} value={signalData.type} onChange={(e) => setValue('type', string_to_signalType(e.target.value))} >
|
||||
<option value={SignalType.ANALOG_INPUT}>{SignalType.ANALOG_INPUT}</option>
|
||||
<option value={SignalType.ANALOG_OUTPUT}>{SignalType.ANALOG_OUTPUT}</option>
|
||||
<option value={SignalType.DIGITAL_INPUT}>{SignalType.DIGITAL_INPUT}</option>
|
||||
<option value={SignalType.DIGITAL_OUTPUT}>{SignalType.DIGITAL_OUTPUT}</option>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="w-40 flex flex-row">
|
||||
<Label size={"xs"} >Beschreibung</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={signalData.description} onChange={(e) => setValue('description', e.target.value)} />
|
||||
</div>
|
||||
<Button onClick={()=>handlerSave()} >Save </Button>
|
||||
</div>
|
||||
|
||||
|
||||
{true &&
|
||||
<div>
|
||||
<pre>{JSON.stringify(signalData, null, 2)}</pre>
|
||||
|
||||
</div>
|
||||
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,132 @@
|
||||
"use client"
|
||||
|
||||
import { PropertyField } from "./propertyFields"
|
||||
import { ComponentDefaults, ComponentResponse, ComponentType, ComponentTypeDefinition } from "@/types/anlagen/components"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { useComponent } from "@/hooks/rk2/useComponent"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Select } from "@/components/ui/select"
|
||||
import { useSignal } from "@/hooks/rk2/useSignal"
|
||||
import { Card, CardContent, CardHeader } from "@/components/ui/card"
|
||||
|
||||
|
||||
|
||||
interface Props {
|
||||
component?: ComponentResponse
|
||||
componentTypes: ComponentTypeDefinition[]
|
||||
componentDefaults: ComponentDefaults[]
|
||||
onClose?: () => void
|
||||
}
|
||||
|
||||
export function ComponentForm({ component, componentTypes, componentDefaults, onClose }: Props) {
|
||||
|
||||
|
||||
const { findCompType,
|
||||
findCompPropertyType,
|
||||
updateComponent,
|
||||
compnentData,
|
||||
setValue,
|
||||
changePropertie,
|
||||
setDefaultPropertie
|
||||
} = useComponent({ component, componentTypes, componentDefaults });
|
||||
|
||||
|
||||
const { signalByComp } = useSignal({ componenteId: component?.id })
|
||||
|
||||
const handleSaveChanges = async ()=>{
|
||||
await updateComponent(compnentData.id, compnentData)
|
||||
if(onClose) onClose();
|
||||
}
|
||||
|
||||
if (!component) return <>{true &&
|
||||
<Card positons={"start"}>
|
||||
<CardHeader variant={"danger"} >Debug</CardHeader>
|
||||
<CardContent className="ps-8">
|
||||
<pre>{JSON.stringify(compnentData, null, 2)}</pre>
|
||||
|
||||
</CardContent>
|
||||
</Card>
|
||||
}</>
|
||||
return (
|
||||
<div style={{ border: "1px solid #ccc", padding: 16, marginBottom: 16, borderRadius: 8, }} >
|
||||
|
||||
{/*--- Component Basisdaten */}
|
||||
<div className="flex gap-4">
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Name</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={compnentData.name} onChange={(e) => setValue('name', e.target.value)} />
|
||||
</div>
|
||||
|
||||
|
||||
<div className="w-30 flex flex-col">
|
||||
<label className="ps-1 font-normal text-xs text-gray-600 "> {"Index"}</label>
|
||||
<Input sizeVariant={"xs"} type="number" value={compnentData.index} onChange={(e) => setValue('index', Number(e.target.value))} />
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Prefix</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={compnentData.prefix} onChange={(e) => setValue('prefix', e.target.value)} />
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Postfix</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={compnentData.postfix} onChange={(e) => setValue('postfix', e.target.value)} />
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Unit Typ</Label>
|
||||
<Select sizeVariant={"xs"} value={compnentData.type} onChange={(e) => setValue('type', e.target.value)} >
|
||||
{componentTypes.map((t) => <option key={t.type} value={t.type}>{t.label}</option>)}
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
{/*--- Properties */}
|
||||
<div className="mt-6">
|
||||
<div className="font-bold">Eigenschaften</div>
|
||||
|
||||
{compnentData.properties &&
|
||||
Object.entries(compnentData.properties).map(([key, value]) => (
|
||||
|
||||
<PropertyField
|
||||
key={key}
|
||||
name={key}
|
||||
value={value}
|
||||
propertyType={findCompPropertyType(key)}
|
||||
setPropValue={(name, value) => changePropertie(name, value)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="flex gap-4">
|
||||
<Button type="button" onClick={() => handleSaveChanges()}>Save</Button>
|
||||
<Button type="button" onClick={() => setDefaultPropertie()}>Add default Props</Button>
|
||||
</div>
|
||||
|
||||
|
||||
{/* --- Signal IDs ----*/}
|
||||
{signalByComp && signalByComp.map((s) => <div key={s.id}>
|
||||
{s.name}
|
||||
</div>)}
|
||||
{true &&
|
||||
<Card positons={"start"}>
|
||||
<CardHeader variant={"danger"} >Debug</CardHeader>
|
||||
<CardContent className="ps-8">
|
||||
<pre>{JSON.stringify(compnentData, null, 2)}</pre>
|
||||
|
||||
</CardContent>
|
||||
</Card>
|
||||
}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
/*
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,78 @@
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { PropertyDefinition } from "@/types/anlagen/components"
|
||||
|
||||
|
||||
interface PropertyFieldProps {
|
||||
name: string
|
||||
propertyType?: PropertyDefinition,
|
||||
value: unknown,
|
||||
setPropValue: (name:string,value:unknown)=>void
|
||||
|
||||
}
|
||||
|
||||
export function PropertyField({ name, value, propertyType,setPropValue }: PropertyFieldProps) {
|
||||
|
||||
|
||||
if (!propertyType) return <div className="w-30">
|
||||
{name} {value as string} {propertyType}
|
||||
</div>
|
||||
|
||||
const handleChangeValue = (value:unknown,type:string)=>{
|
||||
|
||||
setPropValue(name,value);
|
||||
}
|
||||
|
||||
switch (propertyType.type) {
|
||||
|
||||
case "text":
|
||||
return (
|
||||
<div className="w-30 flex flex-col">
|
||||
<label className="ps-1 font-normal text-xs text-gray-600 "> {propertyType.label}</label>
|
||||
<Input type="text" value={""+value} onChange={(e)=>handleChangeValue(e.target.value,"text")} />
|
||||
</div>
|
||||
)
|
||||
|
||||
case "number":
|
||||
return (
|
||||
<div className="w-30 flex flex-col">
|
||||
<label className="ps-1 font-normal text-xs text-gray-600 "> {propertyType.label}</label>
|
||||
<Input type="number" value={Number(value)} onChange={(e)=>handleChangeValue(e.target.value,"number")} />
|
||||
|
||||
</div>
|
||||
)
|
||||
|
||||
case "boolean":
|
||||
return (
|
||||
<div className="w-30 flex flex-row gap-4">
|
||||
<label className="w-20 ps-1 font-normal text-xs text-gray-600 ">
|
||||
{propertyType.label}
|
||||
</label>
|
||||
<input type="checkbox" checked={value as boolean} onChange={(e)=>handleChangeValue(e.target.checked,"boolean")} />
|
||||
</div>
|
||||
)
|
||||
|
||||
case "select":
|
||||
return (
|
||||
<div>
|
||||
<label> {propertyType.label} </label>
|
||||
|
||||
<select value={value as string} onChange={(e)=>handleChangeValue(e.target.value,"text")}>
|
||||
<option value="">
|
||||
Bitte auswählen
|
||||
</option>
|
||||
|
||||
{propertyType.options?.map(
|
||||
(option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
)
|
||||
)}
|
||||
</select>
|
||||
</div>
|
||||
)
|
||||
|
||||
default:
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
"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";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
interface Params {
|
||||
anlage: Anlage,
|
||||
groups: GroupResponse[],
|
||||
selectedUnit?: UnitResponse,
|
||||
componentTypes: ComponentTypeDefinition[]
|
||||
componentDefaults: ComponentDefaults[]
|
||||
}
|
||||
|
||||
export const UnitEdit = ({ anlage, groups, selectedUnit, componentTypes,componentDefaults }: Params) => {
|
||||
|
||||
|
||||
const { getGroupById } = useGroups({ groups });
|
||||
const [showSelectGroup, setShowSelectGroup] = useState(false);
|
||||
const [selectedGroup, setSelectedGroup] = useState<GroupResponse>();
|
||||
|
||||
|
||||
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 (
|
||||
<div className="" >
|
||||
|
||||
<UnitCreateForm anlage={anlage} selectedGroup={selectedGroup} onDisplaySelectGroup={() => setShowSelectGroup(true)} unitIntial={selectedUnit} componentTypes={componentTypes} componentDefaults={componentDefaults}/>
|
||||
|
||||
{showSelectGroup &&
|
||||
<Modal onClose={() => setShowSelectGroup(false)} >
|
||||
<GroupSelect groups={groups} onSelect={handlerGroupSelect} />
|
||||
</Modal>
|
||||
}
|
||||
|
||||
</div >
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Anlage } from "@/types/rk";
|
||||
import Toast from "@/components/ui/Toast";
|
||||
import { UnitResponse } from "@/types/anlagen/units";
|
||||
import { useUnit } from "@/hooks/rk2/useUnit";
|
||||
import { api_units_create, api_units_update } from "@/actions/rk2/api-units";
|
||||
import { ComponentForm } from "./componentForm";
|
||||
import { GroupResponse } from "@/types/anlagen/groups";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ComponentDefaults, ComponentResponse, ComponentTypeDefinition } from "@/types/anlagen/components";
|
||||
import { useComponent } from "@/hooks/rk2/useComponent";
|
||||
import { ComponentIcon, Divide, Save, TableOfContents } from "lucide-react";
|
||||
import { Card, CardContent, CardFooder, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { List, ListItem, ListItemAction, ListItemContent, ListItemDescription, ListItemIcon, ListItemTitle } from "@/components/ui/list";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Select } from "@/components/ui/select";
|
||||
|
||||
|
||||
|
||||
interface Params {
|
||||
anlage: Anlage,
|
||||
unitIntial?: UnitResponse
|
||||
selectedGroup?: GroupResponse
|
||||
onDisplaySelectGroup: () => void
|
||||
componentTypes: ComponentTypeDefinition[]
|
||||
componentDefaults: ComponentDefaults[]
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
export const UnitCreateForm = ({ anlage, unitIntial, onDisplaySelectGroup, selectedGroup, componentTypes, componentDefaults, debug }: Params) => {
|
||||
const [id, setId] = useState<string>();
|
||||
const [selectedComponent, setSelectedComponent] = useState<ComponentResponse>();
|
||||
|
||||
|
||||
const { emptyUnit, unitType, setValue, unitData, string_to_UnitType } = useUnit({ unit: unitIntial });
|
||||
const { componentNames, findCompType, createComponent, removeComponent } = useComponent({ componentTypes, componentDefaults });
|
||||
|
||||
const [toast, setToast] = useState<string | null>(null);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
console.log('init');
|
||||
if (unitIntial) {
|
||||
const { id, ...rest } = unitIntial;
|
||||
setId(id);
|
||||
} else {
|
||||
}
|
||||
}, [unitIntial]);
|
||||
|
||||
//--- Parent Group hat sich geändert
|
||||
useEffect(() => {
|
||||
selectedGroup && setValue("groupId", selectedGroup.id);
|
||||
}, [selectedGroup])
|
||||
|
||||
|
||||
//--- Submit --> Create oder Update
|
||||
const onSubmit = async () => {
|
||||
|
||||
const { id: id1, ...sendData } = unitData;
|
||||
|
||||
let response = id ? await api_units_update(id, sendData) : await api_units_create(sendData);
|
||||
|
||||
if (response.success) {
|
||||
setId(response.data!.id);
|
||||
setToast("Erfolgreich gespeichert!");
|
||||
} else {
|
||||
setToast("Fehler beim Speichern!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const handlerAppendComponent = (type: string) => {
|
||||
const comp = findCompType(type);
|
||||
if (!comp || !id) return;
|
||||
console.log(comp)
|
||||
createComponent(id, { name: "", type: comp.type })
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="relative inline-block">
|
||||
{toast && (
|
||||
<Toast
|
||||
message={toast}
|
||||
onClose={() => setToast(null)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<Card variant={"primary"}>
|
||||
<CardHeader >Unit</CardHeader>
|
||||
<CardContent className="ps-12 pt-4">
|
||||
|
||||
<div className="flex gap-4">
|
||||
|
||||
|
||||
<div className="flex gap-1 items-center ">
|
||||
<div className="text-xs">{selectedGroup ? selectedGroup.name : "noGroup"}</div>
|
||||
<Button type="button" variant={"secondary"} size={"xs"} onClick={() => onDisplaySelectGroup()}><TableOfContents /></Button>
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Prefix</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={unitData.prefix} onChange={(e) => setValue('prefix', e.target.value)} />
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >BMK</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={unitData.bmk} onChange={(e) => setValue('bmk', e.target.value)} />
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex flex-col w-150">
|
||||
<Label size={"xs"} >Name</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={unitData.name} onChange={(e) => setValue('name', e.target.value)} />
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col w-100">
|
||||
<Label size={"xs"} >Postfix</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={unitData.postfix} onChange={(e) => setValue('postfix', e.target.value)} />
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col w-150">
|
||||
<Label size={"xs"} >Unit Typ</Label>
|
||||
<Select sizeVariant={"xs"} value={unitData.type} onChange={(e) => setValue('type', string_to_UnitType(e.target.value))} >
|
||||
{unitType.map((t) => <option key={t.id} value={t.id}>{t.value}</option>)}
|
||||
</Select>
|
||||
</div>
|
||||
<Button type="button" size={"lg"} onClick={() => onSubmit()} ><Save /> </Button>
|
||||
|
||||
</div>
|
||||
|
||||
<div className="w-30 flex flex-row">
|
||||
<Label size={"xs"} >Info</Label>
|
||||
<Input sizeVariant={"xs"} type="text" value={unitData.info} onChange={(e) => setValue('info', e.target.value)} />
|
||||
</div>
|
||||
|
||||
</CardContent>
|
||||
<CardFooder></CardFooder>
|
||||
</Card >
|
||||
|
||||
{selectedComponent && <div>
|
||||
<Button onClick={() => setSelectedComponent(undefined)}>close</Button>
|
||||
</div>
|
||||
}
|
||||
|
||||
|
||||
{/* Componente */}
|
||||
|
||||
|
||||
{/*--- Show addable Components */}
|
||||
{
|
||||
!selectedComponent && <section className="flex gap-4">
|
||||
{id &&
|
||||
<Card className="w-60" variant={"primary"}>
|
||||
<CardHeader variant={"ghost"}>Komponenten +</CardHeader>
|
||||
<List variant="transparent" border={"none"} divide={false} size={"sm"}>
|
||||
{componentNames.map((comp) => (
|
||||
<ListItem className="" key={comp.id}>
|
||||
<Button onClick={() => handlerAppendComponent(comp.id)} type="button" key={comp.id} variant={"select"} size={"xs"} >{comp.name}</Button>
|
||||
</ListItem>
|
||||
))}
|
||||
</List>
|
||||
</Card >
|
||||
}
|
||||
|
||||
|
||||
{/*--- Show Components List */}
|
||||
{id && unitData.components &&
|
||||
<Card positons="center" className="w-full">
|
||||
<CardHeader>Komponenten</CardHeader>
|
||||
<CardContent className="p-2">
|
||||
|
||||
<List className="w-120" >
|
||||
{unitData.components.map((c, index) => <ListItem hover={true} size={"sm"} key={c.id}>
|
||||
<ListItemIcon>
|
||||
<ComponentIcon />
|
||||
</ListItemIcon>
|
||||
|
||||
<ListItemContent className="w-100">
|
||||
<ListItemTitle>
|
||||
{c.type}
|
||||
</ListItemTitle>
|
||||
|
||||
<ListItemDescription>
|
||||
{c.prefix && c.prefix}
|
||||
{c.name}
|
||||
{c.postfix && c.postfix}
|
||||
{c.index && c.index > 0 ? c.index: ""}
|
||||
</ListItemDescription>
|
||||
</ListItemContent>
|
||||
|
||||
<ListItemAction>
|
||||
<Button onClick={() => setSelectedComponent(() => c)} type="button" size={"xs"} variant={"secondary"}>Details</Button>
|
||||
<Button onClick={() => removeComponent(c.id)} type="button" size={"xs"} variant={"secondary"}>Löschen</Button>
|
||||
</ListItemAction>
|
||||
|
||||
</ListItem>)}
|
||||
|
||||
|
||||
</List>
|
||||
<div>
|
||||
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
</Card >
|
||||
}
|
||||
|
||||
</section>
|
||||
}
|
||||
{/* --- Components */}
|
||||
|
||||
|
||||
|
||||
<ComponentForm
|
||||
component={selectedComponent}
|
||||
componentTypes={componentTypes}
|
||||
componentDefaults={componentDefaults}
|
||||
onClose={() => setSelectedComponent(() => undefined)}
|
||||
/>
|
||||
|
||||
|
||||
{
|
||||
true &&
|
||||
<Card positons={"start"}>
|
||||
<CardHeader variant={"danger"} >Debug</CardHeader>
|
||||
<CardContent className="ps-8">
|
||||
<pre>{JSON.stringify(unitData, null, 2)}</pre>
|
||||
|
||||
</CardContent>
|
||||
</Card>
|
||||
}
|
||||
|
||||
</>
|
||||
|
||||
)
|
||||
}
|
||||
/*
|
||||
{fields.length === 0 && (<p>Noch keine Components vorhanden.</p>)}
|
||||
|
||||
{selectedComponent && (
|
||||
|
||||
<ComponentForm
|
||||
index={selectedComponent.index}
|
||||
register={register}
|
||||
control={control}
|
||||
remove={(index) => handlerRemoveItem(index)}
|
||||
setValue={setValue}
|
||||
componentTypes={componentTypes}
|
||||
componentDefaults={componentDefaults}
|
||||
|
||||
/>
|
||||
|
||||
)}
|
||||
|
||||
{false && fields.map(
|
||||
(field, index) => (
|
||||
<ComponentForm
|
||||
key={field.id}
|
||||
index={index}
|
||||
register={register}
|
||||
control={control}
|
||||
remove={(index) => handlerRemoveItem(index)}
|
||||
setValue={setValue}
|
||||
componentTypes={componentTypes}
|
||||
componentDefaults={componentDefaults}
|
||||
|
||||
/>
|
||||
)
|
||||
)}
|
||||
|
||||
|
||||
*/
|
||||
Reference in new issue
Block a user