Files
CPLv4.0/components/modales/handlers/handleSetDateTime.js
ISA 0f938953db feat: Modularisierung und Optimierung der SettingsModal-Komponente
- Handler-Funktionen (handleClearDatabase, handleReboot, handleSetDateTime, handleSubmit) in separate Dateien ausgelagert, um die Übersichtlichkeit zu verbessern und Wartbarkeit zu erleichtern
- `use client`-Anweisung am Anfang von SettingsModal.jsx hinzugefügt, um clientseitige Funktionen wie `window`-basierte Aufrufe korrekt zu verwenden
- Redux- und lokale State-Werte optimiert und an handleSubmit als Parameter übergeben
- Konsolen-Logs für URL-Bildung und Debugging-Zwecke in den Handlern hinzugefügt
2024-11-09 16:23:56 +01:00

44 lines
1.7 KiB
JavaScript

//components/modales/handlers/handleSetDateTime.js
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!");
});
};