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,195 @@
|
||||
import { api_component_create, api_component_remove, api_component_update } from "@/actions/rk2/api-component";
|
||||
import { api_signals_create } from "@/actions/rk2/api-signals";
|
||||
import { ComponentCreate, ComponentDefaults, ComponentResponse, ComponentType, ComponentTypeDefinition, ComponentUpdate, PropertyDefinition } from "@/types/anlagen/components";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
component?: ComponentResponse,
|
||||
componentTypes: ComponentTypeDefinition[]
|
||||
componentDefaults: ComponentDefaults[]
|
||||
}
|
||||
interface ComponentNames {
|
||||
id: string,
|
||||
name: string
|
||||
}
|
||||
|
||||
const emptyComponent: ComponentResponse = {
|
||||
id: "",
|
||||
name: "",
|
||||
type: "",
|
||||
index: 0,
|
||||
postfix: "",
|
||||
prefix: "",
|
||||
properties: {}
|
||||
|
||||
}
|
||||
|
||||
export function useComponent({ component, componentTypes, componentDefaults }: Props) {
|
||||
|
||||
const [data, setData] = useState<ComponentResponse>(emptyComponent);
|
||||
const [componentNames, setComponentNames] = useState<ComponentNames[]>([])
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- init component Data
|
||||
//-------------------------------------------------------------------------
|
||||
useEffect(() => {
|
||||
console.log('Render',component?.properties)
|
||||
if (!component)
|
||||
setData(() => ({ ...emptyComponent,properties:{} }))
|
||||
else
|
||||
setData(() => ({ ...emptyComponent,properties:{}, ...component }))
|
||||
|
||||
}, [component])
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- init Components Names
|
||||
//-------------------------------------------------------------------------
|
||||
useEffect(() => {
|
||||
let compNames: ComponentNames[] = [];
|
||||
|
||||
componentTypes.forEach((c) => compNames.push({ id: c.type, name: c.label }))
|
||||
setComponentNames(() => compNames);
|
||||
}, [componentTypes])
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- set Values of Compnent
|
||||
//-------------------------------------------------------------------------
|
||||
const setValue = <K extends keyof ComponentResponse>(fieldName: K, value: ComponentResponse[K]): void => {
|
||||
setData((cur) => ({ ...cur, [fieldName]: value }))
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Suche Type von Componente
|
||||
//-------------------------------------------------------------------------
|
||||
const findCompType = (type: string): ComponentTypeDefinition | undefined => {
|
||||
return componentTypes.find((item) => item.type === type)
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Propertie Typ Suchen
|
||||
//-------------------------------------------------------------------------
|
||||
const findCompPropertyType = (propName: string): PropertyDefinition | undefined => {
|
||||
let comType = findCompType(data.type);
|
||||
let propType = comType?.properties.find((p) => p.key === propName);
|
||||
|
||||
return propType
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Propertie Änderung
|
||||
//-------------------------------------------------------------------------
|
||||
const changePropertie = (name: string, value: unknown) => {
|
||||
|
||||
let newData = { ...data };
|
||||
if (!newData.properties) return;
|
||||
newData.properties[name] = value;
|
||||
|
||||
setData((cur) => ({ ...cur, properties: newData.properties }));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Default Properties hinzufügen
|
||||
//-------------------------------------------------------------------------
|
||||
const setDefaultPropertie = () => {
|
||||
|
||||
let newData = { ...data }; // Compenenten daten kopieren
|
||||
let comType = findCompType(data.type);
|
||||
if(!comType) return;
|
||||
let currentProperties = newData.properties;
|
||||
if(!currentProperties) currentProperties = {};
|
||||
|
||||
comType.properties.forEach((dp)=>{
|
||||
if(!currentProperties[dp.key]) currentProperties[dp.key] = dp.defaultValue;
|
||||
})
|
||||
|
||||
|
||||
|
||||
setData((cur) => ({ ...cur, properties: currentProperties }));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Defaults
|
||||
//-------------------------------------------------------------------------
|
||||
const getCompDefaults = (compType: ComponentType) => {
|
||||
let defaults = componentDefaults.find((c) => c.type === compType);
|
||||
if (!defaults) defaults = { name: "unbekannt", signals: [], type: "unbekannt" }
|
||||
|
||||
return defaults;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Create
|
||||
//-------------------------------------------------------------------------
|
||||
const createComponent = async (unitId: string, data: ComponentCreate) => {
|
||||
let defaults = getCompDefaults(data.type);
|
||||
let comTyp = findCompType(data.type);
|
||||
if (!data.name || data.name == "") data.name = defaults.name;
|
||||
|
||||
if (!comTyp) return;
|
||||
|
||||
const record = Object.fromEntries(
|
||||
comTyp.properties.map(({ key, type, defaultValue }) => [key, defaultValue])
|
||||
);
|
||||
|
||||
if (comTyp && comTyp.properties) data.properties = record;
|
||||
console.log(data)
|
||||
setLoading(true);
|
||||
let resultCreatComp = await api_component_create(unitId, data);
|
||||
if (resultCreatComp.success) {
|
||||
for (let defSig of defaults.signals) {
|
||||
await api_signals_create({ name: defSig.name, type: defSig.type, componentId: resultCreatComp.data.id })
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
setLoading(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Update
|
||||
//-------------------------------------------------------------------------
|
||||
const updateComponent = async (compId: string, data: ComponentResponse) => {
|
||||
|
||||
let { id, ...update } = data;
|
||||
setLoading(true);
|
||||
await api_component_update(compId, update);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Remove
|
||||
//-------------------------------------------------------------------------
|
||||
const removeComponent = async (compId: string) => {
|
||||
setLoading(true);
|
||||
await api_component_remove(compId);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return {
|
||||
loading,
|
||||
compnentData: data,
|
||||
setValue,
|
||||
componentNames,
|
||||
findCompType,
|
||||
findCompPropertyType,
|
||||
createComponent,
|
||||
updateComponent,
|
||||
removeComponent,
|
||||
changePropertie,
|
||||
setDefaultPropertie
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user