Extend admin for websites/scripts and surface links
This commit is contained in:
@@ -3,42 +3,110 @@ import axios from '../services/api';
|
||||
|
||||
function AdminDashboard() {
|
||||
const [users, setUsers] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [form, setForm] = useState({ username: '', password: '', role: 'user' });
|
||||
const [websites, setWebsites] = useState([]);
|
||||
const [scripts, setScripts] = useState([]);
|
||||
const [loadingUsers, setLoadingUsers] = useState(true);
|
||||
const [loadingSites, setLoadingSites] = useState(true);
|
||||
const [loadingScripts, setLoadingScripts] = useState(true);
|
||||
const [creatingUser, setCreatingUser] = useState(false);
|
||||
const [creatingSite, setCreatingSite] = useState(false);
|
||||
const [creatingScript, setCreatingScript] = useState(false);
|
||||
const [formUser, setFormUser] = useState({ username: '', password: '', role: 'user' });
|
||||
const [formSite, setFormSite] = useState({ name: '', url: '', description: '' });
|
||||
const [formScript, setFormScript] = useState({ name: '', description: '', content: '' });
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const fetchUsers = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setLoadingUsers(true);
|
||||
const res = await axios.get('/api/admin/users');
|
||||
setUsers(res.data);
|
||||
setError(null);
|
||||
} catch (e) {
|
||||
setError('Konnte Nutzerliste nicht laden');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
setLoadingUsers(false);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchWebsites = async () => {
|
||||
try {
|
||||
setLoadingSites(true);
|
||||
const res = await axios.get('/api/admin/websites');
|
||||
setWebsites(res.data);
|
||||
} catch (e) {
|
||||
setError('Webseiten konnten nicht geladen werden');
|
||||
} finally {
|
||||
setLoadingSites(false);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchScripts = async () => {
|
||||
try {
|
||||
setLoadingScripts(true);
|
||||
const res = await axios.get('/api/admin/scripts');
|
||||
setScripts(res.data);
|
||||
} catch (e) {
|
||||
setError('Scripts konnten nicht geladen werden');
|
||||
} finally {
|
||||
setLoadingScripts(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchUsers();
|
||||
fetchWebsites();
|
||||
fetchScripts();
|
||||
}, []);
|
||||
|
||||
const createUser = async () => {
|
||||
if (!form.username || !form.password) {
|
||||
if (!formUser.username || !formUser.password) {
|
||||
setError('Username und Passwort erforderlich');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setCreating(true);
|
||||
await axios.post('/api/admin/users', form);
|
||||
setForm({ username: '', password: '', role: 'user' });
|
||||
setCreatingUser(true);
|
||||
await axios.post('/api/admin/users', formUser);
|
||||
setFormUser({ username: '', password: '', role: 'user' });
|
||||
await fetchUsers();
|
||||
} catch (e) {
|
||||
setError(e.response?.data?.message || 'Erstellen fehlgeschlagen');
|
||||
} finally {
|
||||
setCreating(false);
|
||||
setCreatingUser(false);
|
||||
}
|
||||
};
|
||||
|
||||
const createWebsite = async () => {
|
||||
if (!formSite.name || !formSite.url) {
|
||||
setError('Name und URL erforderlich');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setCreatingSite(true);
|
||||
await axios.post('/api/admin/websites', formSite);
|
||||
setFormSite({ name: '', url: '', description: '' });
|
||||
await fetchWebsites();
|
||||
} catch (e) {
|
||||
setError(e.response?.data?.message || 'Webseite konnte nicht angelegt werden');
|
||||
} finally {
|
||||
setCreatingSite(false);
|
||||
}
|
||||
};
|
||||
|
||||
const createScript = async () => {
|
||||
if (!formScript.name || !formScript.content) {
|
||||
setError('Name und Inhalt erforderlich');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setCreatingScript(true);
|
||||
await axios.post('/api/admin/scripts', formScript);
|
||||
setFormScript({ name: '', description: '', content: '' });
|
||||
await fetchScripts();
|
||||
} catch (e) {
|
||||
setError(e.response?.data?.message || 'Script konnte nicht angelegt werden');
|
||||
} finally {
|
||||
setCreatingScript(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -72,13 +140,33 @@ function AdminDashboard() {
|
||||
}
|
||||
};
|
||||
|
||||
const deleteWebsite = async (id) => {
|
||||
if (!window.confirm('Webseite löschen?')) return;
|
||||
try {
|
||||
await axios.delete(`/api/admin/websites/${id}`);
|
||||
await fetchWebsites();
|
||||
} catch (e) {
|
||||
setError('Konnte Webseite nicht löschen');
|
||||
}
|
||||
};
|
||||
|
||||
const deleteScript = async (id) => {
|
||||
if (!window.confirm('Script löschen?')) return;
|
||||
try {
|
||||
await axios.delete(`/api/admin/scripts/${id}`);
|
||||
await fetchScripts();
|
||||
} catch (e) {
|
||||
setError('Konnte Script nicht löschen');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="main-content admin">
|
||||
<div className="admin-header">
|
||||
<div>
|
||||
<p className="eyebrow">Adminbereich</p>
|
||||
<h2>Benutzerverwaltung</h2>
|
||||
<p className="muted">Nutzer anlegen, Rollen setzen, Passwörter zurücksetzen.</p>
|
||||
<h2>Verwaltung</h2>
|
||||
<p className="muted">Nutzer, externe Links und Python-Skripte zentral verwalten.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -89,8 +177,8 @@ function AdminDashboard() {
|
||||
<label>
|
||||
Benutzername
|
||||
<input
|
||||
value={form.username}
|
||||
onChange={(e) => setForm({ ...form, username: e.target.value })}
|
||||
value={formUser.username}
|
||||
onChange={(e) => setFormUser({ ...formUser, username: e.target.value })}
|
||||
placeholder="z.B. maria"
|
||||
/>
|
||||
</label>
|
||||
@@ -98,34 +186,33 @@ function AdminDashboard() {
|
||||
Passwort
|
||||
<input
|
||||
type="password"
|
||||
value={form.password}
|
||||
onChange={(e) => setForm({ ...form, password: e.target.value })}
|
||||
value={formUser.password}
|
||||
onChange={(e) => setFormUser({ ...formUser, password: e.target.value })}
|
||||
placeholder="Sicheres Passwort"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Rolle
|
||||
<select
|
||||
value={form.role}
|
||||
onChange={(e) => setForm({ ...form, role: e.target.value })}
|
||||
value={formUser.role}
|
||||
onChange={(e) => setFormUser({ ...formUser, role: e.target.value })}
|
||||
>
|
||||
<option value="user">User</option>
|
||||
<option value="admin">Admin</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<button onClick={createUser} disabled={creating}>
|
||||
<button onClick={createUser} disabled={creatingUser}>
|
||||
➕ Nutzer anlegen
|
||||
</button>
|
||||
{error && <p className="error">{error}</p>}
|
||||
</div>
|
||||
|
||||
<div className="admin-card">
|
||||
<div className="table-head">
|
||||
<h3>Nutzer</h3>
|
||||
<button className="ghost" onClick={fetchUsers} disabled={loading}>↻ Aktualisieren</button>
|
||||
<button className="ghost" onClick={fetchUsers} disabled={loadingUsers}>↻ Aktualisieren</button>
|
||||
</div>
|
||||
{loading ? (
|
||||
{loadingUsers ? (
|
||||
<p className="muted">Lade Nutzer...</p>
|
||||
) : (
|
||||
<div className="table">
|
||||
@@ -156,6 +243,109 @@ function AdminDashboard() {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="admin-grid">
|
||||
<div className="admin-card">
|
||||
<h3>Externe Webseiten</h3>
|
||||
<div className="form-grid">
|
||||
<label>
|
||||
Name
|
||||
<input
|
||||
value={formSite.name}
|
||||
onChange={(e) => setFormSite({ ...formSite, name: e.target.value })}
|
||||
placeholder="z.B. Docs"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
URL
|
||||
<input
|
||||
value={formSite.url}
|
||||
onChange={(e) => setFormSite({ ...formSite, url: e.target.value })}
|
||||
placeholder="https://..."
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Beschreibung
|
||||
<input
|
||||
value={formSite.description}
|
||||
onChange={(e) => setFormSite({ ...formSite, description: e.target.value })}
|
||||
placeholder="Kurzbeschreibung"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<button onClick={createWebsite} disabled={creatingSite}>➕ Webseite speichern</button>
|
||||
{loadingSites ? <p className="muted">Lade Webseiten...</p> : (
|
||||
<div className="table compact">
|
||||
<div className="table-row table-headings">
|
||||
<span>🌐 Name</span>
|
||||
<span>URL</span>
|
||||
<span className="actions">Aktionen</span>
|
||||
</div>
|
||||
{websites.map((w) => (
|
||||
<div className="table-row" key={w.id}>
|
||||
<span className="user">{w.name}</span>
|
||||
<span className="muted">{w.url}</span>
|
||||
<span className="actions">
|
||||
<button className="ghost danger" onClick={() => deleteWebsite(w.id)}>🗑️</button>
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="admin-card">
|
||||
<h3>Python-Skripte</h3>
|
||||
<div className="form-grid">
|
||||
<label>
|
||||
Name
|
||||
<input
|
||||
value={formScript.name}
|
||||
onChange={(e) => setFormScript({ ...formScript, name: e.target.value })}
|
||||
placeholder="Cleanup Job"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Beschreibung
|
||||
<input
|
||||
value={formScript.description}
|
||||
onChange={(e) => setFormScript({ ...formScript, description: e.target.value })}
|
||||
placeholder="Kurzbeschreibung"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Inhalt
|
||||
<textarea
|
||||
rows={6}
|
||||
value={formScript.content}
|
||||
onChange={(e) => setFormScript({ ...formScript, content: e.target.value })}
|
||||
placeholder="#!/usr/bin/env python3\nprint('Hello')"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<button onClick={createScript} disabled={creatingScript}>➕ Script speichern</button>
|
||||
{loadingScripts ? <p className="muted">Lade Scripts...</p> : (
|
||||
<div className="table compact">
|
||||
<div className="table-row table-headings">
|
||||
<span>🐍 Script</span>
|
||||
<span>Beschreibung</span>
|
||||
<span className="actions">Aktionen</span>
|
||||
</div>
|
||||
{scripts.map((s) => (
|
||||
<div className="table-row" key={s.id}>
|
||||
<span className="user">{s.name}</span>
|
||||
<span className="muted">{s.description}</span>
|
||||
<span className="actions">
|
||||
<button className="ghost danger" onClick={() => deleteScript(s.id)}>🗑️</button>
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && <p className="error">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useEffect, useState } from 'react';
|
||||
import axios from '../services/api';
|
||||
|
||||
function ToolOverview() {
|
||||
const navigate = useNavigate();
|
||||
const role = localStorage.getItem('role');
|
||||
const [websites, setWebsites] = useState([]);
|
||||
const [loadingWebsites, setLoadingWebsites] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const load = async () => {
|
||||
try {
|
||||
const res = await axios.get('/api/websites');
|
||||
setWebsites(res.data);
|
||||
} catch (e) {
|
||||
setWebsites([]);
|
||||
} finally {
|
||||
setLoadingWebsites(false);
|
||||
}
|
||||
};
|
||||
load();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="main-content">
|
||||
@@ -13,6 +31,31 @@ function ToolOverview() {
|
||||
{role === 'admin' && (
|
||||
<button onClick={() => navigate('/admin')}>🛠️ Admin-Bereich</button>
|
||||
)}
|
||||
|
||||
<h3 style={{ marginTop: '24px' }}>🌐 Externe Webseiten</h3>
|
||||
{loadingWebsites ? (
|
||||
<p className="muted">Lade Links...</p>
|
||||
) : websites.length === 0 ? (
|
||||
<p className="muted">Keine Links angelegt.</p>
|
||||
) : (
|
||||
<div className="card-grid">
|
||||
{websites.map((w) => (
|
||||
<a
|
||||
key={w.id}
|
||||
className="link-card"
|
||||
href={w.url}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
<div className="link-card__icon">🌐</div>
|
||||
<div>
|
||||
<div className="link-card__title">{w.name}</div>
|
||||
<div className="link-card__desc">{w.description || w.url}</div>
|
||||
</div>
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user