Files
Tandur/templates/admin.html
T
Rudi c4cef10909 Base Website
Basic Website, mit Login, Homepage, Usericon, Profilsettings, adminsettings.
(KI)
2026-09-23 23:07:35 +02:00

155 lines
6.5 KiB
HTML
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.
{% extends "base.html" %}
{% block title %}Admin – Tandur{% endblock %}
{% block content %}
{% include "navbar.html" %}
<main class="mx-auto max-w-5xl px-4 py-10" x-data="adminPanel()" x-init="load()">
<div class="mb-8 flex items-center justify-between">
<h1 class="font-display text-2xl font-semibold">Benutzer</h1>
<button @click="openCreate()" class="rounded bg-marker px-4 py-2 text-sm font-medium text-board hover:brightness-95">
Benutzer anlegen
</button>
</div>
<p x-show="error" x-text="error" class="mb-4 text-sm text-brick" style="display:none"></p>
<div class="overflow-hidden rounded border border-panel">
<table class="w-full text-left text-sm">
<thead class="bg-panel text-muted">
<tr>
<th class="px-4 py-2 font-medium">E-Mail</th>
<th class="px-4 py-2 font-medium">Rolle</th>
<th class="px-4 py-2 font-medium"></th>
</tr>
</thead>
<tbody>
<template x-for="u in users" :key="u.id">
<tr class="border-t border-panel">
<td class="px-4 py-2" x-text="u.email"></td>
<td class="px-4 py-2" x-text="u.role"></td>
<td class="space-x-3 px-4 py-2 text-right">
<button @click="openEdit(u)" class="text-marker hover:underline">Bearbeiten</button>
<button @click="remove(u)" class="text-brick hover:underline">Löschen</button>
</td>
</tr>
</template>
<tr x-show="!loading && users.length === 0">
<td colspan="3" class="px-4 py-6 text-center text-muted">Keine Benutzer vorhanden.</td>
</tr>
</tbody>
</table>
</div>
<!-- Modal: Anlegen / Bearbeiten -->
<div x-show="modalOpen" style="display:none" class="fixed inset-0 z-10 flex items-center justify-center bg-black/50 px-4">
<div class="w-full max-w-sm rounded border border-panel bg-panel p-6">
<h2 class="mb-4 font-display text-lg font-semibold" x-text="editing ? 'Benutzer bearbeiten' : 'Benutzer anlegen'"></h2>
<form @submit.prevent="save" class="space-y-4">
<div>
<label class="mb-1 block text-sm text-muted">E-Mail</label>
<input type="username" x-model="form.email" required
class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
</div>
<div>
<label class="mb-1 block text-sm text-muted" x-text="editing ? 'Neues Passwort (optional)' : 'Passwort'"></label>
<input type="password" x-model="form.password" :required="!editing"
class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
</div>
<div>
<label class="mb-1 block text-sm text-muted">Rolle</label>
<select x-model="form.role" class="w-full rounded border border-board bg-board px-3 py-2 text-chalk focus:outline-none focus:ring-2 focus:ring-marker">
<option value="user">user</option>
<option value="admin">admin</option>
</select>
</div>
<p x-show="formError" x-text="formError" class="text-sm text-brick" style="display:none"></p>
<div class="flex justify-end gap-3 pt-2">
<button type="button" @click="modalOpen = false" class="rounded px-4 py-2 text-sm text-muted hover:text-chalk">Abbrechen</button>
<button type="submit" class="rounded bg-marker px-4 py-2 text-sm font-medium text-board hover:brightness-95">Speichern</button>
</div>
</form>
</div>
</div>
</main>
<script>
function adminPanel() {
return {
users: [],
loading: true,
error: '',
modalOpen: false,
editing: null,
form: { email: '', password: '', role: 'user' },
formError: '',
async load() {
this.loading = true;
this.error = '';
const res = await fetch('/api/users/', { credentials: 'same-origin' });
if (res.status === 401) {
window.location.href = '/login?next=/admin';
return;
}
if (res.status === 404) {
// Backend meldet "keine User" als 404 statt leerer Liste
this.users = [];
} else if (!res.ok) {
this.error = 'Benutzer konnten nicht geladen werden.';
} else {
this.users = await res.json();
}
this.loading = false;
},
openCreate() {
this.editing = null;
this.form = { email: '', password: '', role: 'user' };
this.formError = '';
this.modalOpen = true;
},
openEdit(u) {
this.editing = u;
this.form = { email: u.email, password: '', role: u.role };
this.formError = '';
this.modalOpen = true;
},
async save() {
this.formError = '';
const payload = { email: this.form.email, role: this.form.role };
if (this.form.password) payload.password = this.form.password;
const url = this.editing ? `/api/users/${this.editing.id}` : '/api/users/';
const method = this.editing ? 'PATCH' : 'POST';
const res = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
credentials: 'same-origin',
body: JSON.stringify(payload)
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
this.formError = data.detail || 'Speichern fehlgeschlagen.';
return;
}
this.modalOpen = false;
await this.load();
},
async remove(u) {
if (!confirm(`Benutzer ${u.email} wirklich löschen?`)) return;
const res = await fetch(`/api/users/${u.id}`, { method: 'DELETE', credentials: 'same-origin' });
if (res.ok) await this.load();
}
}
}
</script>
{% endblock %}