first commit
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
"use strict";
|
||||
|
||||
const msg = document.getElementById("admin-message");
|
||||
|
||||
function escapeHtml(str) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = str;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
function showMessage(text, ok = true) {
|
||||
msg.textContent = text;
|
||||
msg.className = "alert " + (ok ? "alert-ok" : "alert-error");
|
||||
msg.classList.remove("hidden");
|
||||
if (ok) setTimeout(() => msg.classList.add("hidden"), 4000);
|
||||
}
|
||||
|
||||
async function fetchJSON(url, options = {}) {
|
||||
const res = await fetch(url, options);
|
||||
if (res.status === 401) {
|
||||
window.location.href = "/login";
|
||||
throw new Error("Nicht angemeldet");
|
||||
}
|
||||
if (res.status === 403) {
|
||||
window.location.href = "/admin";
|
||||
throw new Error("Keine Berechtigung");
|
||||
}
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || "Unerwarteter Fehler");
|
||||
return data;
|
||||
}
|
||||
|
||||
async function loadAdmin() {
|
||||
const users = await fetchJSON("/api/users");
|
||||
const gruppenData = await fetchJSON("/api/gruppen");
|
||||
|
||||
const mitgliedGruppe = document.getElementById("mitglied-gruppe");
|
||||
mitgliedGruppe.innerHTML = "";
|
||||
gruppenData.forEach((g) => {
|
||||
const o = document.createElement("option");
|
||||
o.value = g.id;
|
||||
o.textContent = g.name;
|
||||
mitgliedGruppe.appendChild(o);
|
||||
});
|
||||
|
||||
const mitgliedUser = document.getElementById("mitglied-user");
|
||||
mitgliedUser.innerHTML = "";
|
||||
users.forEach((u) => {
|
||||
const o = document.createElement("option");
|
||||
o.value = u.id;
|
||||
o.textContent = u.username + (u.is_admin ? " (Admin)" : "");
|
||||
mitgliedUser.appendChild(o);
|
||||
});
|
||||
|
||||
const liste = document.getElementById("gruppen-liste");
|
||||
liste.innerHTML = "";
|
||||
gruppenData.forEach((g) => {
|
||||
const li = document.createElement("li");
|
||||
const mitglieder = g.mitglieder.map((m) =>
|
||||
`<span class="member-chip">${escapeHtml(m.username)}
|
||||
<button class="chip-x" data-gruppe="${g.id}" data-user="${m.id}" title="Entfernen">×</button>
|
||||
</span>`
|
||||
).join("") || '<span class="muted">(keine Mitglieder)</span>';
|
||||
li.innerHTML = `
|
||||
<div class="gruppe-row">
|
||||
<span class="gruppe-name">${escapeHtml(g.name)}</span>
|
||||
<span class="gruppe-mitglieder">${mitglieder}</span>
|
||||
<button class="btn btn-danger gruppe-del" data-gruppe="${g.id}" title="Gruppe löschen">✕</button>
|
||||
</div>`;
|
||||
liste.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById("gruppe-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const name = document.getElementById("gruppe-name").value.trim();
|
||||
try {
|
||||
await fetchJSON("/api/gruppen", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
document.getElementById("gruppe-name").value = "";
|
||||
showMessage("Gruppe angelegt ✓");
|
||||
await loadAdmin();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("mitglied-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const gruppeId = document.getElementById("mitglied-gruppe").value;
|
||||
const userId = document.getElementById("mitglied-user").value;
|
||||
try {
|
||||
await fetchJSON(`/api/gruppen/${gruppeId}/mitglieder`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_id: Number(userId) }),
|
||||
});
|
||||
showMessage("Benutzer freigegeben ✓");
|
||||
await loadAdmin();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById("gruppen-liste").addEventListener("click", async (e) => {
|
||||
const del = e.target.closest(".gruppe-del");
|
||||
const chip = e.target.closest(".chip-x");
|
||||
|
||||
if (del) {
|
||||
if (!confirm("Gruppe wirklich löschen? Alle Einträge dieser Gruppe gehen verloren.")) return;
|
||||
try {
|
||||
await fetchJSON(`/api/gruppen/${del.dataset.gruppe}`, { method: "DELETE" });
|
||||
showMessage("Gruppe gelöscht.");
|
||||
await loadAdmin();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (chip) {
|
||||
try {
|
||||
await fetchJSON(`/api/gruppen/${chip.dataset.gruppe}/mitglieder/${chip.dataset.user}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
showMessage("Freigabe entzogen.");
|
||||
await loadAdmin();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loadAdmin().catch((err) => showMessage(err.message, false));
|
||||
@@ -0,0 +1,413 @@
|
||||
"use strict";
|
||||
|
||||
const form = document.getElementById("ausgabe-form");
|
||||
const msgBox = document.getElementById("form-message");
|
||||
const monatSelect = document.getElementById("monat-select");
|
||||
const tbody = document.querySelector("#ausgaben-tabelle tbody");
|
||||
const summeMonat = document.getElementById("summe-monat");
|
||||
const chartsEmpty = document.getElementById("charts-empty");
|
||||
const listeEmpty = document.getElementById("liste-empty");
|
||||
const gruppenTabs = document.getElementById("gruppen-tabs");
|
||||
const kategorieSelect = document.getElementById("kategorie");
|
||||
const exportBtn = document.getElementById("export-btn");
|
||||
const einnahmeForm = document.getElementById("einnahme-form");
|
||||
const einnahmeMonat = document.getElementById("einnahme-monat");
|
||||
const einnahmeBetrag = document.getElementById("einnahme-betrag");
|
||||
const einnahmeMsg = document.getElementById("einnahme-message");
|
||||
const bilanzJahr = document.getElementById("bilanz-jahr");
|
||||
const bilanzSummary = document.getElementById("bilanz-summary");
|
||||
|
||||
let aktiveGruppe = null;
|
||||
let gruppen = [];
|
||||
let kategorien = [];
|
||||
let statistik = null;
|
||||
let kategorieChart = null;
|
||||
let ortChart = null;
|
||||
let bilanzChart = null;
|
||||
|
||||
const COLORS = [
|
||||
"#6366f1", "#22d3ee", "#a78bfa", "#34d399", "#f472b6",
|
||||
"#fbbf24", "#fb7185", "#60a5fa", "#4ade80", "#e879f9",
|
||||
"#f59e0b", "#2dd4bf", "#c084fc", "#fb923c", "#94a3b8",
|
||||
];
|
||||
|
||||
function formatEuro(value) {
|
||||
return value.toLocaleString("de-DE", { style: "currency", currency: "EUR" });
|
||||
}
|
||||
|
||||
function showMessage(text, ok = true) {
|
||||
msgBox.textContent = text;
|
||||
msgBox.className = "alert " + (ok ? "alert-ok" : "alert-error");
|
||||
msgBox.classList.remove("hidden");
|
||||
if (ok) setTimeout(() => msgBox.classList.add("hidden"), 4000);
|
||||
}
|
||||
|
||||
function setBusy(busy) {
|
||||
const btn = form.querySelector('button[type="submit"]');
|
||||
btn.disabled = busy;
|
||||
btn.textContent = busy ? "Speichert…" : "Hinzufügen";
|
||||
}
|
||||
|
||||
async function fetchJSON(url, options = {}) {
|
||||
const res = await fetch(url, options);
|
||||
if (res.status === 401) {
|
||||
window.location.href = "/login";
|
||||
throw new Error("Nicht angemeldet");
|
||||
}
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || "Unerwarteter Fehler");
|
||||
return data;
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = str;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tabs / Gruppen-Auswahl
|
||||
// ---------------------------------------------------------------------------
|
||||
function renderTabs() {
|
||||
gruppenTabs.innerHTML = "";
|
||||
gruppen.forEach((g) => {
|
||||
const b = document.createElement("button");
|
||||
b.className = "tab" + (g.id === aktiveGruppe ? " tab-active" : "");
|
||||
b.textContent = g.name;
|
||||
b.dataset.id = g.id;
|
||||
b.addEventListener("click", () => {
|
||||
aktiveGruppe = g.id;
|
||||
renderTabs();
|
||||
refresh();
|
||||
});
|
||||
gruppenTabs.appendChild(b);
|
||||
});
|
||||
if (gruppen.length === 0) {
|
||||
gruppenTabs.innerHTML = '<span class="muted">Keine Gruppen vorhanden.</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Kategorien-Dropdown
|
||||
// ---------------------------------------------------------------------------
|
||||
function fillKategorien() {
|
||||
kategorieSelect.innerHTML = "";
|
||||
kategorien.forEach((k) => {
|
||||
const o = document.createElement("option");
|
||||
o.value = k;
|
||||
o.textContent = k;
|
||||
kategorieSelect.appendChild(o);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Statistik & Diagramme
|
||||
// ---------------------------------------------------------------------------
|
||||
function fillMonatSelect() {
|
||||
const current = new Date().toISOString().slice(0, 7);
|
||||
monatSelect.innerHTML = "";
|
||||
if (!statistik || statistik.monate.length === 0) {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = current;
|
||||
opt.textContent = "Aktueller Monat";
|
||||
monatSelect.appendChild(opt);
|
||||
monatSelect.value = current;
|
||||
return;
|
||||
}
|
||||
statistik.monate.forEach((m) => {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = m;
|
||||
const [y, mo] = m.split("-");
|
||||
opt.textContent = new Date(Number(y), Number(mo) - 1, 1)
|
||||
.toLocaleDateString("de-DE", { month: "long", year: "numeric" });
|
||||
monatSelect.appendChild(opt);
|
||||
});
|
||||
monatSelect.value = statistik.monate[statistik.monate.length - 1];
|
||||
}
|
||||
|
||||
function drawCharts(monat) {
|
||||
const kat = statistik?.nach_kategorie?.[monat];
|
||||
const ort = statistik?.nach_ort?.[monat];
|
||||
const hasData = (kat && kat.werte.length > 0) || (ort && ort.werte.length > 0);
|
||||
chartsEmpty.classList.toggle("hidden", hasData);
|
||||
document.querySelector(".charts-grid").style.display = hasData ? "grid" : "none";
|
||||
|
||||
if (kategorieChart) kategorieChart.destroy();
|
||||
if (ortChart) ortChart.destroy();
|
||||
|
||||
const kd = document.getElementById("chart-kategorie").getContext("2d");
|
||||
const od = document.getElementById("chart-ort").getContext("2d");
|
||||
|
||||
const base = {
|
||||
plugins: {
|
||||
legend: {
|
||||
position: "bottom",
|
||||
labels: { color: "#98a0b3", boxWidth: 14, padding: 14 },
|
||||
},
|
||||
tooltip: { callbacks: { label: (c) => ` ${c.label}: ${formatEuro(c.raw)}` } },
|
||||
},
|
||||
};
|
||||
|
||||
kategorieChart = new Chart(kd, {
|
||||
type: "doughnut",
|
||||
data: {
|
||||
labels: kat?.labels || [],
|
||||
datasets: [{ data: kat?.werte || [], backgroundColor: COLORS, borderWidth: 0 }],
|
||||
},
|
||||
options: { ...base, cutout: "60%" },
|
||||
});
|
||||
|
||||
ortChart = new Chart(od, {
|
||||
type: "doughnut",
|
||||
data: {
|
||||
labels: ort?.labels || [],
|
||||
datasets: [{ data: ort?.werte || [], backgroundColor: COLORS, borderWidth: 0 }],
|
||||
},
|
||||
options: { ...base, cutout: "60%" },
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Liste
|
||||
// ---------------------------------------------------------------------------
|
||||
async function renderList(monat) {
|
||||
if (aktiveGruppe === null) return;
|
||||
const liste = await fetchJSON(
|
||||
"/api/ausgaben?gruppe=" + aktiveGruppe + "&monat=" + encodeURIComponent(monat)
|
||||
);
|
||||
tbody.innerHTML = "";
|
||||
let summe = 0;
|
||||
liste.forEach((a) => {
|
||||
summe += a.betrag;
|
||||
const tr = document.createElement("tr");
|
||||
tr.innerHTML = `
|
||||
<td>${a.datum}</td>
|
||||
<td>${escapeHtml(a.kategorie)}</td>
|
||||
<td>${escapeHtml(a.ort)}</td>
|
||||
<td class="text-right">${formatEuro(a.betrag)}</td>
|
||||
<td class="text-right"><button class="btn btn-danger btn-del" data-id="${a.id}" title="Löschen">✕</button></td>`;
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
|
||||
summeMonat.textContent = formatEuro(summe);
|
||||
listeEmpty.classList.toggle("hidden", liste.length > 0);
|
||||
document.querySelector(".table-wrap").style.display = liste.length > 0 ? "block" : "none";
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Refresh
|
||||
// ---------------------------------------------------------------------------
|
||||
async function refresh() {
|
||||
if (aktiveGruppe === null) return;
|
||||
statistik = await fetchJSON("/api/statistik?gruppe=" + aktiveGruppe);
|
||||
fillMonatSelect();
|
||||
const monat = monatSelect.value;
|
||||
await renderList(monat);
|
||||
drawCharts(monat);
|
||||
await loadEinnahme(monat);
|
||||
fillBilanzJahr();
|
||||
await loadBilanz(bilanzJahr.value);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formular
|
||||
// ---------------------------------------------------------------------------
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
if (aktiveGruppe === null) {
|
||||
showMessage("Bitte zuerst eine Gruppe wählen.", false);
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
const payload = {
|
||||
gruppe_id: aktiveGruppe,
|
||||
kategorie: kategorieSelect.value,
|
||||
ort: document.getElementById("ort").value,
|
||||
betrag: document.getElementById("betrag").value,
|
||||
datum: document.getElementById("datum").value,
|
||||
};
|
||||
try {
|
||||
await fetchJSON("/api/ausgaben", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
form.reset();
|
||||
document.getElementById("datum").value = new Date().toISOString().slice(0, 10);
|
||||
showMessage("Ausgabe gespeichert ✓");
|
||||
await refresh();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
});
|
||||
|
||||
tbody.addEventListener("click", async (e) => {
|
||||
const btn = e.target.closest(".btn-del");
|
||||
if (!btn) return;
|
||||
if (!confirm("Eintrag wirklich löschen?")) return;
|
||||
try {
|
||||
await fetchJSON("/api/ausgaben/" + btn.dataset.id, { method: "DELETE" });
|
||||
await refresh();
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
});
|
||||
|
||||
monatSelect.addEventListener("change", async () => {
|
||||
const monat = monatSelect.value;
|
||||
await renderList(monat);
|
||||
drawCharts(monat);
|
||||
loadEinnahme(monat);
|
||||
});
|
||||
|
||||
exportBtn.addEventListener("click", () => {
|
||||
if (aktiveGruppe === null) return;
|
||||
window.location.href =
|
||||
"/api/export?gruppe=" + aktiveGruppe + "&monat=" + encodeURIComponent(monatSelect.value);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Einnahmen & Bilanz
|
||||
// ---------------------------------------------------------------------------
|
||||
let saldenCache = [];
|
||||
|
||||
async function loadEinnahme(monat) {
|
||||
if (aktiveGruppe === null) return;
|
||||
try {
|
||||
const d = await fetchJSON(
|
||||
"/api/einnahmen?gruppe=" + aktiveGruppe + "&monat=" + encodeURIComponent(monat)
|
||||
);
|
||||
einnahmeBetrag.value = d.betrag.toFixed(2);
|
||||
} catch (err) {
|
||||
/* still */
|
||||
}
|
||||
}
|
||||
|
||||
async function loadBilanz(jahr) {
|
||||
if (aktiveGruppe === null) return;
|
||||
const d = await fetchJSON("/api/bilanz?gruppe=" + aktiveGruppe + "&jahr=" + jahr);
|
||||
saldenCache = d.monate;
|
||||
|
||||
// Jahr-Summe
|
||||
const s = d.summe;
|
||||
bilanzSummary.innerHTML = `
|
||||
<div class="bilanz-item ${s.saldo >= 0 ? "pos" : "neg"}">
|
||||
<span>Saldo ${jahr}</span><strong>${formatEuro(s.saldo)}</strong>
|
||||
</div>
|
||||
<div class="bilanz-item">
|
||||
<span>Einnahmen</span><strong class="pos-text">+${formatEuro(s.einnahmen)}</strong>
|
||||
</div>
|
||||
<div class="bilanz-item">
|
||||
<span>Ausgaben</span><strong class="neg-text">−${formatEuro(s.ausgaben)}</strong>
|
||||
</div>`;
|
||||
|
||||
if (bilanzChart) bilanzChart.destroy();
|
||||
const ctx = document.getElementById("chart-bilanz").getContext("2d");
|
||||
bilanzChart = new Chart(ctx, {
|
||||
type: "bar",
|
||||
data: {
|
||||
labels: d.monate.map((m) => {
|
||||
const [y, mo] = m.monat.split("-");
|
||||
return new Date(Number(y), Number(mo) - 1, 1)
|
||||
.toLocaleDateString("de-DE", { month: "short" });
|
||||
}),
|
||||
datasets: [
|
||||
{ label: "Einnahmen", data: d.monate.map((m) => m.einnahmen), backgroundColor: "#34d399", borderRadius: 4 },
|
||||
{ label: "Ausgaben", data: d.monate.map((m) => -m.ausgaben), backgroundColor: "#fb7185", borderRadius: 4 },
|
||||
],
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: true,
|
||||
aspectRatio: 2,
|
||||
scales: {
|
||||
x: { stacked: false, grid: { color: "rgba(152,160,179,0.1)" }, ticks: { color: "#98a0b3" } },
|
||||
y: { grid: { color: "rgba(152,160,179,0.1)" }, ticks: { color: "#98a0b3" } },
|
||||
},
|
||||
plugins: {
|
||||
legend: { labels: { color: "#98a0b3", boxWidth: 14, padding: 14 } },
|
||||
tooltip: { callbacks: { label: (c) => ` ${c.dataset.label}: ${formatEuro(Math.abs(c.raw))}` } },
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function fillBilanzJahr() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
bilanzJahr.innerHTML = "";
|
||||
const jahre = new Set([currentYear]);
|
||||
if (statistik?.monate) {
|
||||
statistik.monate.forEach((m) => jahre.add(Number(m.slice(0, 4))));
|
||||
}
|
||||
saldenCache.forEach((m) => jahre.add(Number(m.monat.slice(0, 4))));
|
||||
Array.from(jahre).sort((a, b) => b - a).forEach((j) => {
|
||||
const o = document.createElement("option");
|
||||
o.value = j;
|
||||
o.textContent = String(j);
|
||||
bilanzJahr.appendChild(o);
|
||||
});
|
||||
}
|
||||
|
||||
einnahmeForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
if (aktiveGruppe === null) {
|
||||
einnahmeMsg.textContent = "Bitte zuerst eine Gruppe wählen.";
|
||||
einnahmeMsg.className = "alert alert-error";
|
||||
einnahmeMsg.classList.remove("hidden");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const d = await fetchJSON("/api/einnahmen", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
gruppe_id: aktiveGruppe,
|
||||
monat: einnahmeMonat.value,
|
||||
betrag: einnahmeBetrag.value,
|
||||
}),
|
||||
});
|
||||
einnahmeMsg.textContent = "Einnahme gespeichert ✓";
|
||||
einnahmeMsg.className = "alert alert-ok";
|
||||
einnahmeMsg.classList.remove("hidden");
|
||||
setTimeout(() => einnahmeMsg.classList.add("hidden"), 4000);
|
||||
await loadBilanz(bilanzJahr.value || new Date().getFullYear());
|
||||
await refresh();
|
||||
} catch (err) {
|
||||
einnahmeMsg.textContent = err.message;
|
||||
einnahmeMsg.className = "alert alert-error";
|
||||
einnahmeMsg.classList.remove("hidden");
|
||||
}
|
||||
});
|
||||
|
||||
bilanzJahr.addEventListener("change", () => loadBilanz(bilanzJahr.value));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Initialisierung
|
||||
// ---------------------------------------------------------------------------
|
||||
async function loadGruppen() {
|
||||
gruppen = await fetchJSON("/api/gruppen");
|
||||
if (aktiveGruppe === null || !gruppen.some((g) => g.id === aktiveGruppe)) {
|
||||
aktiveGruppe = gruppen.length ? gruppen[0].id : null;
|
||||
}
|
||||
renderTabs();
|
||||
}
|
||||
|
||||
async function init() {
|
||||
document.getElementById("datum").value = new Date().toISOString().slice(0, 10);
|
||||
einnahmeMonat.value = new Date().toISOString().slice(0, 7);
|
||||
bilanzJahr.value = new Date().getFullYear();
|
||||
try {
|
||||
kategorien = await fetchJSON("/api/kategorien");
|
||||
fillKategorien();
|
||||
await loadGruppen();
|
||||
if (aktiveGruppe !== null) {
|
||||
await refresh();
|
||||
}
|
||||
} catch (err) {
|
||||
showMessage(err.message, false);
|
||||
}
|
||||
}
|
||||
|
||||
init().catch((err) => showMessage(err.message, false));
|
||||
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
|
||||
const passwortForm = document.getElementById("passwort-form");
|
||||
const passwortMsg = document.getElementById("passwort-message");
|
||||
|
||||
async function fetchJSON(url, options = {}) {
|
||||
const res = await fetch(url, options);
|
||||
if (res.status === 401) {
|
||||
window.location.href = "/login";
|
||||
throw new Error("Nicht angemeldet");
|
||||
}
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) throw new Error(data.error || "Unerwarteter Fehler");
|
||||
return data;
|
||||
}
|
||||
|
||||
passwortForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await fetchJSON("/api/passwort", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
altes_passwort: document.getElementById("altes-passwort").value,
|
||||
neues_passwort: document.getElementById("neues-passwort").value,
|
||||
}),
|
||||
});
|
||||
passwortForm.reset();
|
||||
passwortMsg.textContent = "Passwort erfolgreich geändert ✓";
|
||||
passwortMsg.className = "alert alert-ok";
|
||||
passwortMsg.classList.remove("hidden");
|
||||
setTimeout(() => passwortMsg.classList.add("hidden"), 4000);
|
||||
} catch (err) {
|
||||
passwortMsg.textContent = err.message;
|
||||
passwortMsg.className = "alert alert-error";
|
||||
passwortMsg.classList.remove("hidden");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user