Files
test/public/dashboard.js
admin 5569f35037
All checks were successful
Build & Push Image / build-image (push) Successful in 1m27s
elemente statt todos in dashboard
2026-03-02 18:57:22 +09:00

58 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// dashboard.js
import { authFetch } from "./api.js";
// ElementReferenzen
const welcomeMessage = document.getElementById("welcomeMessage");
const todosList = document.getElementById("todosList");
const errorMessage = document.getElementById("errorMessage");
const logoutBtn = document.getElementById("logoutBtn");
// Prüfe ob ein JWT vorhanden ist
const token = localStorage.getItem("token");
if (!token) {
// Kein Token → zurück zur LoginSeite
window.location.href = "login.html";
}
// Logout Token löschen + weiterleiten
logoutBtn.addEventListener("click", () => {
localStorage.removeItem("token");
window.location.href = "login.html";
});
// Lade Todos vom Backend
async function loadElements() {
try {
const res = await authFetch("/elements"); // fetchWrapper sendet den JWT Automatisch
if (!res.ok) {
errorMessage.textContent = "Fehler beim Abrufen der Elemente!";
if (res.status === 401) {
window.location.href = "login.html"; // bei ungültigem Token zum Login
}
return;
}
const todos = await res.json();
// Zeige Todos in der Liste an
todosList.innerHTML = "";
if (Array.isArray(todos) && todos.length > 0) {
todos.forEach(todo => {
const li = document.createElement("li");
li.textContent = todo.task;
todosList.appendChild(li);
});
} else {
todosList.innerHTML = "<li>Keine Todos gefunden.</li>";
}
} catch (err) {
console.error("Fetch Error:", err);
errorMessage.textContent = "Serverfehler beim Laden!";
}
}
// Willkommenstext setzen
welcomeMessage.textContent = "Willkommen zum Dashboard!";
// Rufe die Todos ab
loadTodos();