"use client"; import React, { useEffect, useState } from "react"; import { Listbox } from "@headlessui/react"; import { useDispatch, useSelector } from "react-redux"; import { RootState } from "@/redux/store"; import { setIsSettingsModalOpen } from "@/redux/slices/analogInputs/analogInputsUiSlice"; import type { AnalogInput } from "@/types/analogInput"; // 👈 Importiere den Typ (jetzt definiert und exportiert) export default function AnalogInputsSettingsModal() { const dispatch = useDispatch(); const isOpen = useSelector( (state: RootState) => state.analogInputsUi.isSettingsModalOpen ); const selectedInput = useSelector( (state) => state.selectedAnalogInput ); const [label, setLabel] = useState(""); const [offset, setOffset] = useState("0.000"); const [factor, setFactor] = useState("1.000"); const [loggerInterval, setLoggerInterval] = useState("9"); const [unit, setUnit] = useState("V"); const [isSaving, setIsSaving] = useState(false); const unitOptions = ["V", "mA", "°C", "bar", "%"]; useEffect(() => { if (selectedInput && isOpen) { setLabel(selectedInput.label || ""); setOffset( typeof selectedInput.offset === "number" ? selectedInput.offset.toFixed(3) : selectedInput.offset || "0.000" ); setFactor( typeof selectedInput.factor === "number" ? selectedInput.factor.toFixed(3) : selectedInput.factor || "1.000" ); setLoggerInterval(selectedInput.loggerInterval || "9"); setUnit(selectedInput.unit || "V"); } }, [selectedInput, isOpen]); if (!isOpen || !selectedInput) return null; const handleSave = async () => { setIsSaving(true); const slot = selectedInput.id; const isDev = window.location.hostname === "localhost"; const offsetParam = offset.replace(",", "."); const factorParam = factor.replace(",", "."); const loggerParam = loggerInterval; const acn = encodeURIComponent(label); const acu = encodeURIComponent(unit); const url = `/CPL?/Service/ae.ACP&ACN${slot}=${acn}&ACO${slot}=${offsetParam}&ACF${slot}=${factorParam}&ACL${slot}=${loggerParam}&ACU${slot}=${acu}`; try { if (isDev) { await fetch("/api/cpl/updateAnalogInputsSettingsHandler", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ updates: [ { key: "win_analogInputsLabels", index: slot - 1, value: label }, { key: "win_analogInputsOffset", index: slot - 1, value: parseFloat(offsetParam), }, { key: "win_analogInputsFactor", index: slot - 1, value: parseFloat(factorParam), }, { key: "win_analogInputsUnits", index: slot - 1, value: unit }, { key: "win_analogInputsLoggerIntervall", index: slot - 1, value: parseInt(loggerParam), }, ], }), }); alert("Mockdaten gespeichert."); } else { const result = await fetch(url); if (!result.ok) throw new Error("Fehler bei CGI-Aufruf"); alert("Einstellungen gespeichert (Produktion)."); } dispatch(setIsSettingsModalOpen(false)); location.reload(); } catch (err) { alert("Fehler beim Speichern."); console.error(err); } finally { setIsSaving(false); } }; return (

Einstellungen Messwerteingang {selectedInput.id}

Bezeichnung: setLabel(e.target.value)} />
Offset: setOffset(e.target.value)} />
Faktor: setFactor(e.target.value)} />
Einheit:
{unit} {unitOptions.map((opt) => ( `px-4 py-1 cursor-pointer ${ selected ? "bg-littwin-blue text-white font-medium" : active ? "bg-base-muted" : "text-fg" }` } > {opt} ))}
Speicherintervall:
setLoggerInterval(e.target.value)} /> Minuten
); }