Adminbereich mit Rollen und Nutzerauswertung ergänzen
Admin-API serverseitig absichern, Kennzahlen und paginierte Nutzersuche bereitstellen, Weboberfläche ergänzen und Einrichtung sowie spätere Kauf-Funktionen dokumentieren.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import { request } from './client';
|
||||
import type { AdminOverview, AdminUserPage } from './types';
|
||||
|
||||
export function fetchAdminOverview() {
|
||||
return request<AdminOverview>('/admin/overview');
|
||||
}
|
||||
|
||||
export function fetchAdminUsers(search: string, page: number) {
|
||||
const params = new URLSearchParams({ search, page: String(page) });
|
||||
return request<AdminUserPage>(`/admin/users?${params}`);
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import { request, setToken } from './client';
|
||||
import type { AuthResult } from './types';
|
||||
import type { AuthResult, LearnerProfile, User } from './types';
|
||||
|
||||
export function fetchMe() {
|
||||
return request<{ user: User; profile: LearnerProfile }>('/me');
|
||||
}
|
||||
|
||||
export async function register(email: string, password: string) {
|
||||
const result = await request<AuthResult>('/auth/register', {
|
||||
|
||||
@@ -2,9 +2,34 @@ export type User = {
|
||||
id: string;
|
||||
email: string;
|
||||
ui_language: string;
|
||||
role: 'learner' | 'admin';
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type AdminOverview = {
|
||||
users_total: number;
|
||||
users_active_7_days: number;
|
||||
attempts_total: number;
|
||||
exercises_generated: number;
|
||||
average_score: number;
|
||||
};
|
||||
|
||||
export type AdminUser = {
|
||||
id: string;
|
||||
email: string;
|
||||
role: 'learner' | 'admin';
|
||||
created_at: string;
|
||||
attempts: number;
|
||||
average_score: number;
|
||||
last_attempt?: string;
|
||||
};
|
||||
|
||||
export type AdminUserPage = {
|
||||
users: AdminUser[];
|
||||
total: number;
|
||||
page: number;
|
||||
};
|
||||
|
||||
export type LearnerProfile = {
|
||||
id: string;
|
||||
user_id: string;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { BookOpen, Bug, ChartNoAxesColumnIncreasing, LogOut, Mic, MessageCircle } from 'lucide-react';
|
||||
import { BookOpen, Bug, ChartNoAxesColumnIncreasing, LogOut, Mic, MessageCircle, Shield } from 'lucide-react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { clearToken, getToken } from '../api/client';
|
||||
import { fetchMe } from '../api/auth';
|
||||
import { ApiError, clearToken, getToken } from '../api/client';
|
||||
import { fetchDebugStatus } from '../api/debug';
|
||||
import type { Exercise, Lesson } from '../api/types';
|
||||
import type { Exercise, Lesson, User } from '../api/types';
|
||||
import { AdminPage } from '../pages/AdminPage';
|
||||
import { DashboardPage } from '../pages/DashboardPage';
|
||||
import { LoginPage } from '../pages/LoginPage';
|
||||
import { RegisterPage } from '../pages/RegisterPage';
|
||||
@@ -15,11 +17,38 @@ import { useHashRoute } from './useHashRoute';
|
||||
export function App() {
|
||||
const [route, setRoute] = useHashRoute(getToken() ? 'dashboard' : 'login');
|
||||
const [debugAvailable, setDebugAvailable] = useState(false);
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [authError, setAuthError] = useState('');
|
||||
const [authRevision, setAuthRevision] = useState(0);
|
||||
const [practiceSelection, setPracticeSelection] = useState<{ lesson: Lesson; exercise: Exercise } | null>(null);
|
||||
const authed = Boolean(getToken());
|
||||
|
||||
useEffect(() => {
|
||||
if (!authed) {
|
||||
setUser(null);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
setAuthError('');
|
||||
fetchMe().then(({ user: currentUser }) => {
|
||||
if (!cancelled) {
|
||||
setUser(currentUser);
|
||||
setAuthError('');
|
||||
}
|
||||
}).catch((error) => {
|
||||
if (cancelled) return;
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
clearToken();
|
||||
setRoute('login');
|
||||
} else {
|
||||
setAuthError(error instanceof Error ? error.message : 'Konto konnte nicht geladen werden.');
|
||||
}
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
}, [authed, authRevision]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!authed || user?.role !== 'admin') {
|
||||
setDebugAvailable(false);
|
||||
return;
|
||||
}
|
||||
@@ -34,18 +63,21 @@ export function App() {
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [authed]);
|
||||
}, [authed, user?.role]);
|
||||
|
||||
useEffect(() => {
|
||||
if (route !== 'practice') setPracticeSelection(null);
|
||||
}, [route]);
|
||||
|
||||
function handleAuthed() {
|
||||
setUser(null);
|
||||
setAuthRevision((value) => value + 1);
|
||||
setRoute('dashboard');
|
||||
}
|
||||
|
||||
function logout() {
|
||||
clearToken();
|
||||
setUser(null);
|
||||
setPracticeSelection(null);
|
||||
setRoute('login');
|
||||
}
|
||||
@@ -67,6 +99,23 @@ export function App() {
|
||||
);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return (
|
||||
<main className="auth-shell">
|
||||
<div className="auth-panel">
|
||||
<h1>Konto wird geladen</h1>
|
||||
{authError ? (
|
||||
<>
|
||||
<p className="form-error" role="alert">{authError}</p>
|
||||
<button className="primary-button" onClick={() => setAuthRevision((value) => value + 1)}>Erneut versuchen</button>
|
||||
<button className="link-button" onClick={logout}>Abmelden</button>
|
||||
</>
|
||||
) : <p className="muted">Bitte einen Moment warten.</p>}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app-shell">
|
||||
<aside className="sidebar" aria-label="Navigation">
|
||||
@@ -91,6 +140,11 @@ export function App() {
|
||||
<button className={route === 'progress' ? 'active' : ''} onClick={() => setRoute('progress')}>
|
||||
<ChartNoAxesColumnIncreasing size={18} /> Fortschritt
|
||||
</button>
|
||||
{user.role === 'admin' && (
|
||||
<button className={route === 'admin' ? 'active' : ''} onClick={() => setRoute('admin')}>
|
||||
<Shield size={18} /> Adminbereich
|
||||
</button>
|
||||
)}
|
||||
{debugAvailable && (
|
||||
<button className={route === 'debug' ? 'active' : ''} onClick={() => setRoute('debug')}>
|
||||
<Bug size={18} /> Debug
|
||||
@@ -108,7 +162,8 @@ export function App() {
|
||||
{route === 'practice' && <PracticePage initialSelection={practiceSelection} />}
|
||||
{route === 'conversation' && <ConversationPage />}
|
||||
{route === 'progress' && <ProgressPage />}
|
||||
{route === 'debug' && <DebugPage />}
|
||||
{route === 'admin' && (user.role === 'admin' ? <AdminPage /> : <p className="form-error">Kein Zugriff auf den Adminbereich.</p>)}
|
||||
{route === 'debug' && debugAvailable && <DebugPage />}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -340,6 +340,118 @@ input:focus {
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.admin-view {
|
||||
max-width: 1280px;
|
||||
}
|
||||
|
||||
.admin-metrics {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.admin-users,
|
||||
.admin-product {
|
||||
display: grid;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.admin-section-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.admin-section-head h2,
|
||||
.admin-product h2 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.admin-section-head p,
|
||||
.admin-product p {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.admin-count,
|
||||
.admin-role {
|
||||
display: inline-flex;
|
||||
padding: 5px 10px;
|
||||
border-radius: 999px;
|
||||
color: #0e5f47;
|
||||
background: #e5f4ec;
|
||||
font-size: 0.82rem;
|
||||
font-weight: 800;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.admin-role-staff {
|
||||
color: #4a397f;
|
||||
background: #eeeafb;
|
||||
}
|
||||
|
||||
.admin-search {
|
||||
max-width: 520px;
|
||||
}
|
||||
|
||||
.admin-search .action-row {
|
||||
flex-wrap: nowrap;
|
||||
}
|
||||
|
||||
.admin-table-wrap {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
min-width: 780px;
|
||||
border-collapse: collapse;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.admin-table th,
|
||||
.admin-table td {
|
||||
padding: 13px 10px;
|
||||
border-bottom: 1px solid #e2e7e4;
|
||||
}
|
||||
|
||||
.admin-table th {
|
||||
color: #526158;
|
||||
font-size: 0.82rem;
|
||||
}
|
||||
|
||||
.admin-table td small {
|
||||
display: block;
|
||||
margin-top: 3px;
|
||||
color: #657269;
|
||||
}
|
||||
|
||||
.admin-empty {
|
||||
padding: 15px 10px;
|
||||
}
|
||||
|
||||
.admin-pagination {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.admin-next-steps {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.admin-next-steps span {
|
||||
padding: 8px 10px;
|
||||
border-radius: 8px;
|
||||
background: #f1f4f2;
|
||||
color: #526158;
|
||||
font-weight: 700;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.debug-card {
|
||||
display: grid;
|
||||
align-content: start;
|
||||
@@ -547,4 +659,16 @@ input:focus {
|
||||
.target-text {
|
||||
font-size: 1.3rem;
|
||||
}
|
||||
|
||||
.admin-section-head {
|
||||
display: grid;
|
||||
}
|
||||
|
||||
.admin-search .action-row {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.admin-pagination {
|
||||
justify-content: space-between;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { useEffect, useState, type FormEvent } from 'react';
|
||||
import { fetchAdminOverview, fetchAdminUsers } from '../api/admin';
|
||||
import type { AdminOverview, AdminUserPage } from '../api/types';
|
||||
|
||||
function formatDate(value?: string) {
|
||||
return value ? new Date(value).toLocaleDateString('de-DE') : 'Noch kein Versuch';
|
||||
}
|
||||
|
||||
export function AdminPage() {
|
||||
const [overview, setOverview] = useState<AdminOverview | null>(null);
|
||||
const [users, setUsers] = useState<AdminUserPage | null>(null);
|
||||
const [searchInput, setSearchInput] = useState('');
|
||||
const [search, setSearch] = useState('');
|
||||
const [page, setPage] = useState(1);
|
||||
const [overviewError, setOverviewError] = useState('');
|
||||
const [usersError, setUsersError] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
fetchAdminOverview()
|
||||
.then((data) => { if (!cancelled) setOverview(data); })
|
||||
.catch((error) => { if (!cancelled) setOverviewError(error instanceof Error ? error.message : 'Kennzahlen konnten nicht geladen werden.'); });
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
fetchAdminUsers(search, page)
|
||||
.then((data) => { if (!cancelled) { setUsers(data); setUsersError(''); } })
|
||||
.catch((error) => { if (!cancelled) setUsersError(error instanceof Error ? error.message : 'Nutzer konnten nicht geladen werden.'); });
|
||||
return () => { cancelled = true; };
|
||||
}, [search, page]);
|
||||
|
||||
function submitSearch(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setPage(1);
|
||||
setSearch(searchInput.trim());
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="view-stack admin-view">
|
||||
<header className="view-header">
|
||||
<div>
|
||||
<p className="eyebrow">Verwaltung</p>
|
||||
<h1>Adminbereich</h1>
|
||||
<p className="muted">Nutzer und Lernaktivität im Blick.</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{overviewError && <p className="form-error" role="alert">{overviewError}</p>}
|
||||
<section className="admin-metrics" aria-label="Produktkennzahlen">
|
||||
<div className="panel metric"><span>Nutzer gesamt</span><strong>{overview?.users_total ?? '–'}</strong></div>
|
||||
<div className="panel metric"><span>Aktiv in 7 Tagen</span><strong>{overview?.users_active_7_days ?? '–'}</strong></div>
|
||||
<div className="panel metric"><span>Sprechversuche</span><strong>{overview?.attempts_total ?? '–'}</strong></div>
|
||||
<div className="panel metric"><span>Flowise-Übungen</span><strong>{overview?.exercises_generated ?? '–'}</strong></div>
|
||||
<div className="panel metric"><span>Ø Bewertung</span><strong>{overview ? Math.round(overview.average_score) : '–'}</strong></div>
|
||||
</section>
|
||||
|
||||
<section className="panel admin-users">
|
||||
<div className="admin-section-head">
|
||||
<div>
|
||||
<h2>Nutzer</h2>
|
||||
<p className="muted">Konten und Lernaktivität. Suche nach E-Mail-Adresse.</p>
|
||||
</div>
|
||||
<span className="admin-count">{users?.total ?? '–'} Treffer</span>
|
||||
</div>
|
||||
<form className="admin-search" onSubmit={submitSearch}>
|
||||
<label htmlFor="admin-user-search">E-Mail suchen</label>
|
||||
<div className="action-row">
|
||||
<input id="admin-user-search" type="search" value={searchInput} maxLength={100}
|
||||
onChange={(event) => setSearchInput(event.target.value)} placeholder="name@beispiel.de" />
|
||||
<button className="primary-button" type="submit">Suchen</button>
|
||||
</div>
|
||||
</form>
|
||||
{usersError && <p className="form-error" role="alert">{usersError}</p>}
|
||||
<div className="admin-table-wrap">
|
||||
<table className="admin-table">
|
||||
<thead><tr><th>Nutzer</th><th>Rolle</th><th>Registriert</th><th>Versuche</th><th>Ø Bewertung</th><th>Letzter Versuch</th></tr></thead>
|
||||
<tbody>
|
||||
{(users?.users ?? []).map((user) => (
|
||||
<tr key={user.id}>
|
||||
<td><strong>{user.email}</strong><small>{user.id}</small></td>
|
||||
<td><span className={user.role === 'admin' ? 'admin-role admin-role-staff' : 'admin-role'}>{user.role === 'admin' ? 'Admin' : 'Lernende'}</span></td>
|
||||
<td>{formatDate(user.created_at)}</td>
|
||||
<td>{user.attempts}</td>
|
||||
<td>{user.attempts > 0 ? Math.round(user.average_score) : '–'}</td>
|
||||
<td>{formatDate(user.last_attempt)}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{users && users.users.length === 0 && <p className="muted admin-empty">Keine passenden Nutzer gefunden.</p>}
|
||||
{!users && !usersError && <p className="muted admin-empty">Lade Nutzer...</p>}
|
||||
</div>
|
||||
<div className="admin-pagination">
|
||||
<button className="ghost-button" disabled={page <= 1} onClick={() => setPage(page - 1)}>Zurück</button>
|
||||
<span>Seite {page} von {Math.max(1, Math.ceil((users?.total ?? 0) / 25))}</span>
|
||||
<button className="ghost-button" disabled={!users || page * 25 >= users.total} onClick={() => setPage(page + 1)}>Weiter</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="panel admin-product">
|
||||
<div>
|
||||
<p className="eyebrow">Produktbetrieb</p>
|
||||
<h2>Verkauf und Zugänge</h2>
|
||||
<p>Tarife, Zahlungen und Berechtigungen für gekaufte Inhalte sind noch nicht angeschlossen. Deshalb zeigt dieser Bereich keine erfundenen Umsatz- oder Abozahlen.</p>
|
||||
</div>
|
||||
<div className="admin-next-steps">
|
||||
<span>Als Nächstes: Produktkatalog</span>
|
||||
<span>Dann: Zahlungsanbieter und Webhooks</span>
|
||||
<span>Danach: Zugangsrechte pro Kauf</span>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user