feat: admin CRUD for training types + distribution chart in ActivityPage
Deploy Development / deploy (push) Successful in 50s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 13s

Backend (v9d Phase 1b):
- Migration 006: Add abilities JSONB column + descriptions
- admin_training_types.py: Full CRUD endpoints for training types
  - List, Get, Create, Update, Delete
  - Abilities taxonomy endpoint (5 dimensions: koordinativ, konditionell, kognitiv, psychisch, taktisch)
  - Validation: Cannot delete types in use
- Register admin_training_types router in main.py

Frontend:
- AdminTrainingTypesPage: Full CRUD UI
  - Create/edit form with all fields (category, subcategory, names, icon, descriptions, sort_order)
  - List grouped by category with color coding
  - Delete with usage check
  - Note about abilities mapping coming in v9f
- Add TrainingTypeDistribution to ActivityPage stats tab
- Add admin link in AdminPanel (v9d section)
- Update api.js with admin training types methods

Notes:
- Abilities mapping UI deferred to v9f (flexible prompt system)
- Placeholders (abilities column) in place for future AI analysis

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 4.6 committed 2026-03-21 15:32:32 +01:00
1 parent d164ab932d
commit eecc00e824
8 files changed
+730 -2

No files matched your search

+6
View File
@@ -5,6 +5,7 @@ import { api } from '../utils/api'
import UsageBadge from '../components/UsageBadge'
import TrainingTypeSelect from '../components/TrainingTypeSelect'
import BulkCategorize from '../components/BulkCategorize'
import TrainingTypeDistribution from '../components/TrainingTypeDistribution'
import dayjs from 'dayjs'
import 'dayjs/locale/de'
dayjs.locale('de')
@@ -302,6 +303,11 @@ export default function ActivityPage() {
{tab==='stats' && stats && (
<div>
<div className="card section-gap">
<div className="card-title">🏋️ Trainingstyp-Verteilung (30 Tage)</div>
<TrainingTypeDistribution days={30} />
</div>
{chartData.length>=2 && (
<div className="card section-gap">
<div className="card-title">Aktive Kalorien pro Tag</div>
+17
View File
@@ -424,6 +424,23 @@ export default function AdminPanel() {
</Link>
</div>
</div>
{/* v9d Training Types Management */}
<div className="card section-gap" style={{marginTop:16}}>
<div style={{fontWeight:700,fontSize:14,marginBottom:12,display:'flex',alignItems:'center',gap:6}}>
<Settings size={16} color="var(--accent)"/> Trainingstypen (v9d)
</div>
<div style={{fontSize:12,color:'var(--text3)',marginBottom:12,lineHeight:1.5}}>
Verwalte Trainingstypen, Kategorien und Fähigkeiten-Mapping.
</div>
<div style={{display:'grid',gap:8}}>
<Link to="/admin/training-types">
<button className="btn btn-secondary btn-full">
🏋️ Trainingstypen verwalten
</button>
</Link>
</div>
</div>
</div>
)
}
@@ -0,0 +1,384 @@
import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { Pencil, Trash2, Plus, Save, X, ArrowLeft } from 'lucide-react'
import { api } from '../utils/api'
/**
* AdminTrainingTypesPage - CRUD for training types
* v9d Phase 1b - Basic CRUD without abilities mapping
*/
export default function AdminTrainingTypesPage() {
const nav = useNavigate()
const [types, setTypes] = useState([])
const [categories, setCategories] = useState({})
const [loading, setLoading] = useState(true)
const [editingId, setEditingId] = useState(null)
const [formData, setFormData] = useState(null)
const [error, setError] = useState(null)
const [saving, setSaving] = useState(false)
useEffect(() => {
load()
}, [])
const load = () => {
setLoading(true)
Promise.all([
api.adminListTrainingTypes(),
api.getTrainingCategories()
]).then(([typesData, catsData]) => {
setTypes(typesData)
setCategories(catsData)
setLoading(false)
}).catch(err => {
console.error('Failed to load training types:', err)
setError(err.message)
setLoading(false)
})
}
const startCreate = () => {
setFormData({
category: 'cardio',
subcategory: '',
name_de: '',
name_en: '',
icon: '',
description_de: '',
description_en: '',
sort_order: 0
})
setEditingId('new')
}
const startEdit = (type) => {
setFormData({
category: type.category,
subcategory: type.subcategory || '',
name_de: type.name_de,
name_en: type.name_en,
icon: type.icon || '',
description_de: type.description_de || '',
description_en: type.description_en || '',
sort_order: type.sort_order
})
setEditingId(type.id)
}
const cancelEdit = () => {
setEditingId(null)
setFormData(null)
setError(null)
}
const handleSave = async () => {
if (!formData.name_de || !formData.name_en) {
setError('Name (DE) und Name (EN) sind Pflichtfelder')
return
}
setSaving(true)
setError(null)
try {
if (editingId === 'new') {
await api.adminCreateTrainingType(formData)
} else {
await api.adminUpdateTrainingType(editingId, formData)
}
await load()
cancelEdit()
} catch (err) {
console.error('Save failed:', err)
setError(err.message)
} finally {
setSaving(false)
}
}
const handleDelete = async (id, name) => {
if (!confirm(`Trainingstyp "${name}" wirklich löschen?\n\nHinweis: Löschen ist nur möglich wenn keine Aktivitäten diesen Typ verwenden.`)) {
return
}
try {
await api.adminDeleteTrainingType(id)
await load()
} catch (err) {
alert('Löschen fehlgeschlagen: ' + err.message)
}
}
// Group by category
const grouped = {}
types.forEach(type => {
if (!grouped[type.category]) {
grouped[type.category] = []
}
grouped[type.category].push(type)
})
if (loading) {
return (
<div style={{ padding: 20, textAlign: 'center' }}>
<div className="spinner" style={{ width: 32, height: 32, margin: '0 auto' }} />
</div>
)
}
return (
<div style={{ padding: '16px 16px 80px' }}>
<div style={{ marginBottom: 20, display: 'flex', alignItems: 'center', gap: 12 }}>
<button
onClick={() => nav('/settings')}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
padding: 6,
display: 'flex',
color: 'var(--text2)'
}}
>
<ArrowLeft size={20} />
</button>
<h1 style={{ margin: 0, fontSize: 20, fontWeight: 700 }}>Trainingstypen verwalten</h1>
</div>
{error && (
<div className="card" style={{ padding: 12, marginBottom: 16, background: '#FCEBEB', color: '#D85A30' }}>
{error}
</div>
)}
{/* Create new button */}
{!editingId && (
<button
onClick={startCreate}
className="btn btn-primary btn-full"
style={{ marginBottom: 16 }}
>
<Plus size={16} /> Neuen Trainingstyp anlegen
</button>
)}
{/* Edit form */}
{editingId && formData && (
<div className="card" style={{ padding: 16, marginBottom: 16 }}>
<div style={{ fontWeight: 600, marginBottom: 12 }}>
{editingId === 'new' ? '➕ Neuer Trainingstyp' : '✏️ Trainingstyp bearbeiten'}
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
<div>
<label className="form-label">Kategorie *</label>
<select
className="form-input"
value={formData.category}
onChange={e => setFormData({ ...formData, category: e.target.value })}
>
{Object.keys(categories).map(cat => (
<option key={cat} value={cat}>
{categories[cat].icon} {categories[cat].name_de}
</option>
))}
</select>
</div>
<div>
<label className="form-label">Subkategorie</label>
<input
className="form-input"
value={formData.subcategory}
onChange={e => setFormData({ ...formData, subcategory: e.target.value })}
placeholder="z.B. running, hypertrophy, meditation"
/>
<div style={{ fontSize: 11, color: 'var(--text3)', marginTop: 4 }}>
Kleingeschrieben, ohne Leerzeichen, eindeutig
</div>
</div>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
<div>
<label className="form-label">Name (Deutsch) *</label>
<input
className="form-input"
value={formData.name_de}
onChange={e => setFormData({ ...formData, name_de: e.target.value })}
placeholder="z.B. Laufen"
/>
</div>
<div>
<label className="form-label">Name (English) *</label>
<input
className="form-input"
value={formData.name_en}
onChange={e => setFormData({ ...formData, name_en: e.target.value })}
placeholder="e.g. Running"
/>
</div>
</div>
<div>
<label className="form-label">Icon (Emoji)</label>
<input
className="form-input"
value={formData.icon}
onChange={e => setFormData({ ...formData, icon: e.target.value })}
placeholder="🏃"
maxLength={10}
/>
</div>
<div>
<label className="form-label">Sortierung</label>
<input
type="number"
className="form-input"
value={formData.sort_order}
onChange={e => setFormData({ ...formData, sort_order: parseInt(e.target.value) })}
/>
<div style={{ fontSize: 11, color: 'var(--text3)', marginTop: 4 }}>
Niedrigere Zahlen werden zuerst angezeigt
</div>
</div>
<div>
<label className="form-label">Beschreibung (Deutsch)</label>
<textarea
className="form-input"
value={formData.description_de}
onChange={e => setFormData({ ...formData, description_de: e.target.value })}
placeholder="Optional: Beschreibung für KI-Analyse"
rows={2}
/>
</div>
<div>
<label className="form-label">Beschreibung (English)</label>
<textarea
className="form-input"
value={formData.description_en}
onChange={e => setFormData({ ...formData, description_en: e.target.value })}
placeholder="Optional: Description for AI analysis"
rows={2}
/>
</div>
<div style={{ display: 'flex', gap: 8, marginTop: 8 }}>
<button
onClick={handleSave}
disabled={saving}
className="btn btn-primary"
style={{ flex: 1 }}
>
{saving ? (
<>
<div className="spinner" style={{ width: 14, height: 14 }} />
Speichere...
</>
) : (
<>
<Save size={16} /> Speichern
</>
)}
</button>
<button
onClick={cancelEdit}
disabled={saving}
className="btn btn-secondary"
style={{ flex: 1 }}
>
<X size={16} /> Abbrechen
</button>
</div>
</div>
</div>
)}
{/* List grouped by category */}
{Object.entries(grouped).sort((a, b) => {
const orderA = categories[a[0]]?.sort_order || 999
const orderB = categories[b[0]]?.sort_order || 999
return orderA - orderB
}).map(([cat, catTypes]) => (
<div key={cat} className="card" style={{ padding: 16, marginBottom: 12 }}>
<div style={{
fontWeight: 600,
fontSize: 14,
marginBottom: 12,
color: categories[cat]?.color || 'var(--text1)'
}}>
{categories[cat]?.icon} {categories[cat]?.name_de}
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
{catTypes.sort((a, b) => a.sort_order - b.sort_order).map(type => (
<div
key={type.id}
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: 8,
background: 'var(--surface)',
borderRadius: 6
}}
>
<div style={{ fontSize: 18 }}>{type.icon}</div>
<div style={{ flex: 1 }}>
<div style={{ fontSize: 13, fontWeight: 500 }}>
{type.name_de} <span style={{ color: 'var(--text3)' }}>/ {type.name_en}</span>
</div>
{type.subcategory && (
<div style={{ fontSize: 11, color: 'var(--text3)' }}>
Subkategorie: {type.subcategory}
</div>
)}
</div>
<button
onClick={() => startEdit(type)}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
padding: 6,
color: 'var(--accent)'
}}
title="Bearbeiten"
>
<Pencil size={16} />
</button>
<button
onClick={() => handleDelete(type.id, type.name_de)}
style={{
background: 'none',
border: 'none',
cursor: 'pointer',
padding: 6,
color: '#D85A30'
}}
title="Löschen"
>
<Trash2 size={16} />
</button>
</div>
))}
</div>
</div>
))}
<div style={{
marginTop: 20,
padding: 12,
background: 'var(--surface2)',
borderRadius: 8,
fontSize: 12,
color: 'var(--text3)'
}}>
<strong>Hinweis:</strong> Das Fähigkeiten-Mapping (koordinativ, konditionell, etc.) wird in einer späteren Version hinzugefügt.
</div>
</div>
)
}