- Added direct implementation of `getFromIndexedDB` in Header.jsx to avoid import issues - Increased polling interval to 10 seconds to prevent timing issues in data fetching - Added console logs to verify data loading process and debug potential undefined values This resolves issues where deviceName and kueVersion were showing as "Unbekannt" in production due to IndexedDB timing differences.
110 lines
3.8 KiB
JavaScript
110 lines
3.8 KiB
JavaScript
"use client"; // components/header.jsx
|
|
import React, { useEffect, useState } from "react";
|
|
import Image from "next/image";
|
|
import "bootstrap-icons/font/bootstrap-icons.css";
|
|
import SettingsModal from "./modales/SettingsModal";
|
|
import { getFromIndexedDB } from "../utils/indexedDB";
|
|
|
|
function Header() {
|
|
const [stationsname, setStationsname] = useState("Lädt...");
|
|
const [cplStatus, setCplStatus] = useState("Lädt...");
|
|
const [showSettingsModal, setShowSettingsModal] = useState(false);
|
|
|
|
const handleSettingsClick = () => setShowSettingsModal(true);
|
|
const handleCloseSettingsModal = () => setShowSettingsModal(false);
|
|
const handleLogout = () => (window.location.href = "/offline.html");
|
|
//------------------------------------------------------------------
|
|
async function getFromIndexedDB(key) {
|
|
return new Promise((resolve, reject) => {
|
|
const request = indexedDB.open("CPLDatabase", 1);
|
|
request.onsuccess = () => {
|
|
const db = request.result;
|
|
const transaction = db.transaction("cplVariables", "readonly");
|
|
const store = transaction.objectStore("cplVariables");
|
|
const getRequest = store.get(key);
|
|
getRequest.onsuccess = () => resolve(getRequest.result);
|
|
getRequest.onerror = () => reject(getRequest.error);
|
|
};
|
|
request.onerror = () => reject(request.error);
|
|
});
|
|
}
|
|
|
|
//------------------------------------------------------------------
|
|
useEffect(() => {
|
|
const loadData = async () => {
|
|
try {
|
|
console.log("Lade Daten aus IndexedDB...");
|
|
|
|
const deviceName = await getFromIndexedDB("deviceName");
|
|
console.log("DeviceName geladen:", deviceName); // Sollte den geladenen Wert zeigen
|
|
|
|
const hardwareVersion = await getFromIndexedDB("kueVersion");
|
|
console.log("HardwareVersion geladen:", hardwareVersion); // Sollte den geladenen Wert zeigen
|
|
|
|
setStationsname(deviceName || "Unbekannt");
|
|
setCplStatus(hardwareVersion || "Unbekannt");
|
|
} catch (error) {
|
|
console.error("Fehler beim Laden der Daten aus IndexedDB:", error);
|
|
}
|
|
};
|
|
|
|
loadData();
|
|
|
|
const interval = setInterval(() => {
|
|
loadData();
|
|
}, 5000);
|
|
|
|
return () => clearInterval(interval);
|
|
}, []);
|
|
|
|
//------------------------------------------------------------------
|
|
return (
|
|
<header className="bg-gray-300 flex justify-between items-center w-full h-28 relative text-black">
|
|
<div className="absolute left-32 top-32 transform -translate-y-1/2">
|
|
<Image
|
|
src="/images/Logo.png"
|
|
alt="Logo"
|
|
width={80}
|
|
height={80}
|
|
style={{ width: "auto", height: "auto" }}
|
|
priority={false}
|
|
/>
|
|
</div>
|
|
<div className="w-64 flex-shrink-0 h-full mt-24 "></div>
|
|
|
|
<div className="flex items-start space-x-4 w-full">
|
|
<div className="flex flex-col text-left pl-8">
|
|
<h2 className="text-base">Stationsname</h2>
|
|
<p className="text-base text-gray-600">{stationsname}</p>
|
|
</div>
|
|
</div>
|
|
<div className="p-4 w-full lg:w-full flex flex-row gap-4 justify-between">
|
|
<div className="flex items-center justify-end w-full">
|
|
<button
|
|
onClick={handleSettingsClick}
|
|
className="text-3xl text-black mr-7"
|
|
>
|
|
<i className="bi bi-gear"></i>
|
|
</button>
|
|
</div>
|
|
<div className="flex items-center justify-end w-full">
|
|
<button
|
|
onClick={handleLogout}
|
|
className="bg-littwin-blue text-white px-4 py-2 rounded"
|
|
>
|
|
Abmelden
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{showSettingsModal && (
|
|
<SettingsModal
|
|
showModal={showSettingsModal}
|
|
onClose={handleCloseSettingsModal}
|
|
/>
|
|
)}
|
|
</header>
|
|
);
|
|
}
|
|
|
|
export default Header;
|