diff --git a/components/modales/SettingsModal.jsx b/components/modales/SettingsModal.jsx index 1c4f7b9..65f5418 100644 --- a/components/modales/SettingsModal.jsx +++ b/components/modales/SettingsModal.jsx @@ -4,6 +4,8 @@ import { ClipLoader } from "react-spinners"; import "bootstrap-icons/font/bootstrap-icons.css"; // Import Bootstrap Icons import { useSelector } from "react-redux"; import { current } from "@reduxjs/toolkit"; +import handleReboot from "./handlers/handleReboot"; +import handleClearDatabase from "./handlers/handleClearDatabase"; // Definiere das App-Element für ReactModal ReactModal.setAppElement("#__next"); @@ -48,60 +50,6 @@ function SettingModal({ showModal, onClose }) { if (!currentPath.endsWith(".html")) { currentPath += ".html"; } - const handleReboot = () => { - if ( - window.confirm("Sind Sie sicher, dass Sie den CPL neu starten möchten?") - ) { - // Get the current path and ensure it ends with ".html" - let currentPath = window.location.pathname; - if (!currentPath.endsWith(".html")) { - currentPath += ".html"; - } - - // Full URL with host, current path, and reboot command - const url = `${window.location.origin}/CPL?${currentPath}&BOOT=1`; - - // Log the full URL to the console for debugging - console.log(url); - - // Send the reboot command to the server using fetch and GET method - fetch(url, { method: "GET" }).finally(() => { - window.location.href = `/wait`; // Redirect to wait page on client side - }); - } - }; - - const handleClearDatabase = async () => { - const confirmClear = window.confirm( - "Sind Sie sicher, dass Sie die Datenbank leeren möchten?" - ); - if (!confirmClear) return; - - // Get the current path and ensure it ends with ".html" - let currentPath = window.location.pathname; - if (!currentPath.endsWith(".html")) { - currentPath += ".html"; - } - - // Full URL with host, current path, and clear database command - const url = `${window.location.origin}/CPL?${currentPath}&DEDB=1`; - - // Log the full URL to the console for debugging - console.log(url); - - // Send the clear database command to the server using fetch and GET method - try { - const response = await fetch(url, { method: "GET" }); - if (response.ok) { - alert("Datenbank erfolgreich geleert!"); - } else { - alert("Fehler beim Leeren der Datenbank!"); - } - } catch (error) { - console.error("Fehler:", error); - alert("Fehler beim Leeren der Datenbank!"); - } - }; const handleSubmit = () => { const changes = {}; @@ -171,49 +119,6 @@ function SettingModal({ showModal, onClose }) { } }; - const handleSetDateTime = () => { - const currentDate = new Date(); - - // Format date and time as required by the system without leading zeros: - const year = currentDate.getFullYear().toString().slice(-2); // Last two digits of the year - const month = Number(currentDate.getMonth() + 1); // Convert to Number to remove leading zero - const day = Number(currentDate.getDate()); // Convert to Number to remove leading zero - - const hours = Number(currentDate.getHours()); // Convert to Number to remove leading zero - const minutes = Number(currentDate.getMinutes()); // Convert to Number to remove leading zero - const seconds = Number(currentDate.getSeconds()); // Convert to Number to remove leading zero - - // Date and Time commands - const dateCommand = `CLK00=${year}-${month}-${day}`; - const timeCommand = `CLK01=${hours}-${minutes}-${seconds}`; - - // Get the current path and ensure it ends with ".html" - let currentPath = window.location.pathname; - if (!currentPath.endsWith(".html")) { - currentPath += ".html"; - } - - // Full URL with host, current path, date, and time commands - const url = `${window.location.origin}/CPL?${currentPath}&${dateCommand}&${timeCommand}`; - - // Log the full URL to the console - console.log(url); - - // Send the commands to the server using fetch and GET method - fetch(url, { method: "GET" }) - .then((response) => { - if (response.ok) { - alert("Datum und Uhrzeit erfolgreich gesetzt!"); - } else { - alert("Fehler beim Setzen von Datum und Uhrzeit!"); - } - }) - .catch((error) => { - console.error("Fehler:", error); - alert("Fehler beim Setzen von Datum und Uhrzeit!"); - }); - }; - //--------------------------------------------------- // Setze initiale Werte nur beim Öffnen des Modals useEffect(() => { diff --git a/components/modales/handlers/handleClearDatabase.js b/components/modales/handlers/handleClearDatabase.js new file mode 100644 index 0000000..9e4a21c --- /dev/null +++ b/components/modales/handlers/handleClearDatabase.js @@ -0,0 +1,31 @@ +const handleClearDatabase = async () => { + const confirmClear = window.confirm( + "Sind Sie sicher, dass Sie die Datenbank leeren möchten?" + ); + if (!confirmClear) return; + + // Get the current path and ensure it ends with ".html" + let currentPath = window.location.pathname; + if (!currentPath.endsWith(".html")) { + currentPath += ".html"; + } + + // Full URL with host, current path, and clear database command + const url = `${window.location.origin}/CPL?${currentPath}&DEDB=1`; + + // Log the full URL to the console for debugging + console.log(url); + + // Send the clear database command to the server using fetch and GET method + try { + const response = await fetch(url, { method: "GET" }); + if (response.ok) { + alert("Datenbank erfolgreich geleert!"); + } else { + alert("Fehler beim Leeren der Datenbank!"); + } + } catch (error) { + console.error("Fehler:", error); + alert("Fehler beim Leeren der Datenbank!"); + } +}; diff --git a/components/modales/handlers/handleReboot.js b/components/modales/handlers/handleReboot.js new file mode 100644 index 0000000..98414f7 --- /dev/null +++ b/components/modales/handlers/handleReboot.js @@ -0,0 +1,19 @@ +// handlers/handleReboot.js +const handleReboot = () => { + if ( + window.confirm("Sind Sie sicher, dass Sie den CPL neu starten möchten?") + ) { + let currentPath = window.location.pathname; + if (!currentPath.endsWith(".html")) { + currentPath += ".html"; + } + + const url = `${window.location.origin}/CPL?${currentPath}&BOOT=1`; + console.log(url); + fetch(url, { method: "GET" }).finally(() => { + window.location.href = `/wait`; + }); + } +}; + +export default handleReboot; diff --git a/components/modales/handlers/handleSetDateTime.js b/components/modales/handlers/handleSetDateTime.js new file mode 100644 index 0000000..b053704 --- /dev/null +++ b/components/modales/handlers/handleSetDateTime.js @@ -0,0 +1,42 @@ +const handleSetDateTime = () => { + const currentDate = new Date(); + + // Format date and time as required by the system without leading zeros: + const year = currentDate.getFullYear().toString().slice(-2); // Last two digits of the year + const month = Number(currentDate.getMonth() + 1); // Convert to Number to remove leading zero + const day = Number(currentDate.getDate()); // Convert to Number to remove leading zero + + const hours = Number(currentDate.getHours()); // Convert to Number to remove leading zero + const minutes = Number(currentDate.getMinutes()); // Convert to Number to remove leading zero + const seconds = Number(currentDate.getSeconds()); // Convert to Number to remove leading zero + + // Date and Time commands + const dateCommand = `CLK00=${year}-${month}-${day}`; + const timeCommand = `CLK01=${hours}-${minutes}-${seconds}`; + + // Get the current path and ensure it ends with ".html" + let currentPath = window.location.pathname; + if (!currentPath.endsWith(".html")) { + currentPath += ".html"; + } + + // Full URL with host, current path, date, and time commands + const url = `${window.location.origin}/CPL?${currentPath}&${dateCommand}&${timeCommand}`; + + // Log the full URL to the console + console.log(url); + + // Send the commands to the server using fetch and GET method + fetch(url, { method: "GET" }) + .then((response) => { + if (response.ok) { + alert("Datum und Uhrzeit erfolgreich gesetzt!"); + } else { + alert("Fehler beim Setzen von Datum und Uhrzeit!"); + } + }) + .catch((error) => { + console.error("Fehler:", error); + alert("Fehler beim Setzen von Datum und Uhrzeit!"); + }); +};