feat: Redux-Thunk für analoge Eingänge integriert & useFetchAnalogeEingaenge entfernt
- `fetchAnalogeEingaengeThunk` in `AnalogeEingaengeTable.tsx` verwendet, um API-Daten in Redux zu speichern.
- `useFetchAnalogeEingaenge` entfernt, um doppelte API-Aufrufe zu vermeiden.
- Sicherstellung, dass Redux-Thunk nur im Client (`useEffect`) ausgeführt wird.
- Automatische Aktualisierung der API-Daten alle 10 Sekunden über Redux-Thunk.
- Code-Optimierungen für eine stabilere Client-Side-Architektur mit Next.js.
✅ Jetzt läuft Redux-Thunk stabil & effizient in der Next.js-Anwendung!
This commit is contained in:
@@ -1,24 +1,35 @@
|
||||
"use client";
|
||||
// pages/_app.tsx
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
import store, { useAppDispatch } from "../redux/store";
|
||||
import { fetchAnalogeEingaengeThunk } from "../redux/thunks/fetchAnalogeEingaengeThunk";
|
||||
import { loadWindowVariables } from "../utils/loadWindowVariables";
|
||||
import Header from "../components/header/Header";
|
||||
import Navigation from "../components/navigation/Navigation";
|
||||
import Footer from "../components/footer/Footer";
|
||||
import WindowVariablesInitializer from "../components/WindowVariablesInitializer";
|
||||
import "../styles/globals.css";
|
||||
import { Provider } from "react-redux";
|
||||
import { AppProps } from "next/app";
|
||||
import { setVariables } from "../redux/slices/variablesSlice";
|
||||
import { setSystemSettings } from "../redux/slices/systemSettingsSlice"; // ✅ System-Settings
|
||||
import { setSystemSettings } from "../redux/slices/systemSettingsSlice";
|
||||
import {
|
||||
setOpcUaZustand,
|
||||
setOpcUaActiveClientCount,
|
||||
setOpcUaNodesetName,
|
||||
} from "../redux/slices/opcuaSettingsSlice"; // ✅ OPC-UA Settings
|
||||
import store from "../redux/store";
|
||||
import { AppProps } from "next/app";
|
||||
import WindowVariablesInitializer from "../components/WindowVariablesInitializer";
|
||||
} from "../redux/slices/opcuaSettingsSlice";
|
||||
|
||||
function MyApp({ Component, pageProps }: AppProps) {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<AppContent Component={Component} pageProps={pageProps} />
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function AppContent({ Component, pageProps }: AppProps) {
|
||||
const dispatch = useAppDispatch();
|
||||
const [sessionExpired, setSessionExpired] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -27,9 +38,10 @@ function MyApp({ Component, pageProps }: AppProps) {
|
||||
const variables = await loadWindowVariables();
|
||||
if (!variables) throw new Error("Sitzungsfehler");
|
||||
|
||||
// ✅ OPC-UA Werte, System-Settings und last20Messages separat speichern
|
||||
console.log("✅ Window-Variablen geladen:", variables);
|
||||
|
||||
const {
|
||||
last20Messages, // Entfernen für eigenes Redux-Slice
|
||||
last20Messages,
|
||||
opcUaZustand,
|
||||
opcUaActiveClientCount,
|
||||
opcUaNodesetName,
|
||||
@@ -44,16 +56,10 @@ function MyApp({ Component, pageProps }: AppProps) {
|
||||
ntp3,
|
||||
ntpTimezone,
|
||||
ntpActive,
|
||||
de,
|
||||
de_label,
|
||||
de_state,
|
||||
da_state,
|
||||
da_bezeichnung,
|
||||
...restVariables
|
||||
} = variables;
|
||||
|
||||
// ✅ Speichere System-Settings in systemSettingsSlice
|
||||
store.dispatch(
|
||||
dispatch(
|
||||
setSystemSettings({
|
||||
deviceName,
|
||||
mac1,
|
||||
@@ -69,54 +75,55 @@ function MyApp({ Component, pageProps }: AppProps) {
|
||||
})
|
||||
);
|
||||
|
||||
// ✅ Speichere OPC-UA Einstellungen in opcuaSettingsSlice
|
||||
store.dispatch(setOpcUaZustand(opcUaZustand || "Offline"));
|
||||
store.dispatch(setOpcUaActiveClientCount(opcUaActiveClientCount || 0));
|
||||
store.dispatch(
|
||||
setOpcUaNodesetName(opcUaNodesetName || "DefaultNodeset")
|
||||
);
|
||||
dispatch(setOpcUaZustand(opcUaZustand || "Offline"));
|
||||
dispatch(setOpcUaActiveClientCount(opcUaActiveClientCount || 0));
|
||||
dispatch(setOpcUaNodesetName(opcUaNodesetName || "DefaultNodeset"));
|
||||
|
||||
// ✅ Speichere alle anderen Variablen in variablesSlice
|
||||
store.dispatch(setVariables(restVariables));
|
||||
dispatch(setVariables(restVariables));
|
||||
|
||||
setSessionExpired(false);
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Laden der Sitzung:", error);
|
||||
console.error("❌ Fehler beim Laden der Sitzung:", error);
|
||||
setSessionExpired(true);
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
loadAndStoreVariables(); // Initiales Laden
|
||||
loadAndStoreVariables();
|
||||
|
||||
// Intervall zum Aktualisieren des Redux-Stores alle 10 Sekunden
|
||||
const intervalId = setInterval(loadAndStoreVariables, 10000);
|
||||
|
||||
// Bereinigen des Intervalls, wenn die Komponente unmountet wird
|
||||
return () => clearInterval(intervalId);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window !== "undefined") {
|
||||
dispatch(fetchAnalogeEingaengeThunk());
|
||||
const interval = setInterval(() => {
|
||||
dispatch(fetchAnalogeEingaengeThunk());
|
||||
}, 10000);
|
||||
return () => clearInterval(interval);
|
||||
}
|
||||
}, [dispatch]);
|
||||
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<div className="flex flex-col h-screen overflow-hidden">
|
||||
<WindowVariablesInitializer />
|
||||
<div className="flex flex-col h-screen overflow-hidden">
|
||||
<Header />
|
||||
<div className="flex flex-grow w-full">
|
||||
<Navigation className="w-1/5" />
|
||||
<main className="w-full flex-grow">
|
||||
{sessionExpired && (
|
||||
<div className="bg-red-500 text-white p-4 text-center">
|
||||
Ihre Sitzung ist abgelaufen oder die Verbindung ist
|
||||
unterbrochen. Bitte laden Sie die Seite neu.
|
||||
</div>
|
||||
)}
|
||||
<Component {...pageProps} />
|
||||
</main>
|
||||
</div>
|
||||
<Footer />
|
||||
<Header />
|
||||
<div className="flex flex-grow w-full">
|
||||
<Navigation className="w-1/5" />
|
||||
<main className="w-full flex-grow">
|
||||
{sessionExpired && (
|
||||
<div className="bg-red-500 text-white p-4 text-center">
|
||||
❌ Ihre Sitzung ist abgelaufen oder die Verbindung ist
|
||||
unterbrochen. Bitte laden Sie die Seite neu.
|
||||
</div>
|
||||
)}
|
||||
<Component {...pageProps} />
|
||||
</main>
|
||||
</div>
|
||||
</Provider>
|
||||
<Footer />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user