feat: Add personal reference values management in settings and API
Deploy Development / deploy (push) Successful in 50s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

- Introduced new routes and API endpoints for managing personal reference values.
- Updated the SettingsPage to include a section for reference values with navigation to manage them.
- Enhanced the backend to support reference values in the data layer and versioning.
- Added necessary imports and UI components for a seamless user experience.
This commit is contained in:
Lars committed 2026-04-06 19:45:06 +02:00
1 parent e7dedd527f
commit f0e6fd04fb
23 files changed
+1719 -2

No files matched your search

+2
View File
@@ -22,6 +22,7 @@ import NutritionPage from './pages/NutritionPage'
import ActivityPage from './pages/ActivityPage'
import Analysis from './pages/Analysis'
import SettingsPage from './pages/SettingsPage'
import ProfileReferenceValuesPage from './pages/ProfileReferenceValuesPage'
import GuidePage from './pages/GuidePage'
import AdminTierLimitsPage from './pages/AdminTierLimitsPage'
import AdminFeaturesPage from './pages/AdminFeaturesPage'
@@ -225,6 +226,7 @@ function AppShell() {
<Route path="/goals" element={<GoalsPage/>}/>
<Route path="/analysis" element={<Analysis/>}/>
<Route path="/settings" element={<SettingsPage/>}/>
<Route path="/settings/reference-values" element={<ProfileReferenceValuesPage/>}/>
<Route element={<RequireAdmin />}>
<Route path="admin" element={<AdminShell />}>
<Route index element={<AdminHomePage />} />
@@ -0,0 +1,377 @@
import { useState, useEffect, useCallback } from 'react'
import { Link } from 'react-router-dom'
import { ArrowLeft, Gauge, Pencil, Trash2, Plus } from 'lucide-react'
import { api } from '../utils/api'
function splitValueInput(raw) {
const s = String(raw).trim()
if (!s) return { value_numeric: null, value_text: null }
const normalized = s.replace(',', '.')
if (/^-?\d+(\.\d+)?$/.test(normalized)) {
const n = Number(normalized)
if (!Number.isNaN(n)) return { value_numeric: n, value_text: null }
}
return { value_numeric: null, value_text: s }
}
function formatEntryValue(row) {
if (row.value_numeric != null && row.value_numeric !== '') {
const n = Number(row.value_numeric)
return Number.isFinite(n) ? String(n) : String(row.value_numeric)
}
return row.value_text != null ? String(row.value_text) : '–'
}
export default function ProfileReferenceValuesPage() {
const [types, setTypes] = useState([])
const [selectedKey, setSelectedKey] = useState('')
const [entries, setEntries] = useState([])
const [loading, setLoading] = useState(true)
const [listLoading, setListLoading] = useState(false)
const [error, setError] = useState(null)
const [editingId, setEditingId] = useState(null)
const [form, setForm] = useState({
effective_date: new Date().toISOString().split('T')[0],
value: '',
unit: '',
notes: '',
})
const selectedType = types.find((t) => t.key === selectedKey)
const loadTypes = useCallback(async () => {
try {
setLoading(true)
const data = await api.listReferenceValueTypes()
setTypes(Array.isArray(data) ? data : [])
setError(null)
} catch (e) {
setError(e.message || 'Typen konnten nicht geladen werden')
setTypes([])
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
loadTypes()
}, [loadTypes])
useEffect(() => {
if (types.length && !selectedKey) {
setSelectedKey(types[0].key)
}
}, [types, selectedKey])
const loadEntries = useCallback(async () => {
if (!selectedKey) return
try {
setListLoading(true)
const data = await api.listProfileReferenceValues(selectedKey)
setEntries(Array.isArray(data) ? data : [])
setError(null)
} catch (e) {
setError(e.message || 'Einträge konnten nicht geladen werden')
setEntries([])
} finally {
setListLoading(false)
}
}, [selectedKey])
useEffect(() => {
setEditingId(null)
loadEntries()
}, [loadEntries])
const resetForm = () => {
setEditingId(null)
setForm({
effective_date: new Date().toISOString().split('T')[0],
value: '',
unit: selectedType?.default_unit || '',
notes: '',
})
}
useEffect(() => {
if (selectedType && !editingId) {
setForm((f) => ({
...f,
unit: selectedType.default_unit || f.unit || '',
}))
}
}, [selectedType, editingId])
const handleSubmit = async (e) => {
e.preventDefault()
if (!selectedKey) return
const parts = splitValueInput(form.value)
if (parts.value_numeric == null && !parts.value_text) {
setError('Bitte einen Wert eingeben.')
return
}
const unit = (form.unit || '').trim() || (selectedType?.default_unit || '').trim()
if (!unit) {
setError('Bitte eine Einheit angeben.')
return
}
try {
setError(null)
const payload = {
reference_value_type_key: selectedKey,
effective_date: form.effective_date,
value_numeric: parts.value_numeric,
value_text: parts.value_text,
unit,
notes: form.notes.trim() || null,
}
if (editingId) {
await api.updateProfileReferenceValue(editingId, {
effective_date: payload.effective_date,
value_numeric: payload.value_numeric,
value_text: payload.value_text,
unit: payload.unit,
notes: payload.notes,
})
} else {
await api.createProfileReferenceValue(payload)
}
resetForm()
await loadEntries()
} catch (err) {
setError(err.message || 'Speichern fehlgeschlagen')
}
}
const startEdit = (row) => {
setEditingId(row.id)
setForm({
effective_date: String(row.effective_date || '').slice(0, 10),
value: formatEntryValue(row),
unit: row.unit || selectedType?.default_unit || '',
notes: row.notes || '',
})
}
const handleDelete = async (id) => {
if (!confirm('Diesen Eintrag wirklich löschen?')) return
try {
setError(null)
await api.deleteProfileReferenceValue(id)
if (editingId === id) resetForm()
await loadEntries()
} catch (err) {
setError(err.message || 'Löschen fehlgeschlagen')
}
}
if (loading) {
return (
<div style={{ padding: 24, textAlign: 'center' }}>
<div className="spinner" />
</div>
)
}
return (
<div style={{ paddingBottom: 88 }}>
<div style={{ marginBottom: 16 }}>
<Link
to="/settings"
className="btn btn-secondary"
style={{ display: 'inline-flex', alignItems: 'center', gap: 8, textDecoration: 'none', marginBottom: 12 }}
>
<ArrowLeft size={16} /> Zurück zu Einstellungen
</Link>
<h1 className="page-title" style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
<Gauge size={26} color="var(--accent)" />
Referenzwerte
</h1>
<p style={{ fontSize: 14, color: 'var(--text2)', lineHeight: 1.6, marginTop: 8 }}>
Persönliche Kennwerte für das aktive Profil – historisch gespeichert, typgesteuert. Neue Typen
erscheinen automatisch, sobald sie im System ergänzt werden.
</p>
</div>
{error && (
<div
className="card"
style={{
marginBottom: 16,
background: '#FCEBEB',
color: '#991B1B',
fontSize: 14,
border: '1px solid #FECACA',
}}
>
{error}
</div>
)}
{types.length === 0 ? (
<div className="card" style={{ textAlign: 'center', padding: 32 }}>
<p style={{ color: 'var(--text2)', margin: 0 }}>Keine Referenztypen definiert.</p>
</div>
) : (
<>
<div className="card section-gap">
<div className="card-title">Referenztyp</div>
<label className="form-label" htmlFor="ref-type-select">
Wähle einen Kennwert
</label>
<select
id="ref-type-select"
className="form-input"
value={selectedKey}
onChange={(e) => {
setSelectedKey(e.target.value)
setEditingId(null)
}}
>
{types.map((t) => (
<option key={t.key} value={t.key}>
{t.label}
</option>
))}
</select>
{selectedType?.description && (
<p style={{ fontSize: 13, color: 'var(--text2)', marginTop: 10, lineHeight: 1.5 }}>
{selectedType.description}
</p>
)}
</div>
<div className="card section-gap">
<div className="card-title" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Plus size={16} />
{editingId ? 'Eintrag bearbeiten' : 'Neuer Eintrag'}
</div>
<form onSubmit={handleSubmit}>
<div className="form-row" style={{ flexDirection: 'column', gap: 12 }}>
<div>
<label className="form-label" htmlFor="ref-date">
Datum
</label>
<input
id="ref-date"
type="date"
className="form-input"
required
value={form.effective_date}
onChange={(e) => setForm((f) => ({ ...f, effective_date: e.target.value }))}
/>
</div>
<div>
<label className="form-label" htmlFor="ref-value">
Wert
</label>
<input
id="ref-value"
type="text"
className="form-input"
placeholder="Zahl oder Text (z. B. 178 oder mittel)"
value={form.value}
onChange={(e) => setForm((f) => ({ ...f, value: e.target.value }))}
/>
</div>
<div>
<label className="form-label" htmlFor="ref-unit">
Einheit
</label>
<input
id="ref-unit"
type="text"
className="form-input"
placeholder={selectedType?.default_unit || 'z. B. bpm'}
value={form.unit}
onChange={(e) => setForm((f) => ({ ...f, unit: e.target.value }))}
/>
</div>
<div>
<label className="form-label" htmlFor="ref-notes">
Notiz (optional)
</label>
<textarea
id="ref-notes"
className="form-input"
rows={2}
value={form.notes}
onChange={(e) => setForm((f) => ({ ...f, notes: e.target.value }))}
placeholder="Kontext, Messmethode …"
/>
</div>
</div>
<div style={{ display: 'flex', gap: 8, marginTop: 14, flexWrap: 'wrap' }}>
<button type="submit" className="btn btn-primary" style={{ flex: 1, minWidth: 120 }}>
{editingId ? 'Speichern' : 'Hinzufügen'}
</button>
{editingId && (
<button type="button" className="btn btn-secondary" onClick={resetForm}>
Abbrechen
</button>
)}
</div>
</form>
</div>
<div className="card section-gap">
<div className="card-title">Verlauf</div>
{listLoading ? (
<div style={{ textAlign: 'center', padding: 20 }}>
<div className="spinner" />
</div>
) : entries.length === 0 ? (
<p style={{ color: 'var(--text2)', fontSize: 14, margin: 0 }}>
Noch keine Einträge für diesen Typ. Lege oben einen ersten Wert an.
</p>
) : (
<div style={{ overflowX: 'auto' }}>
<table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 14 }}>
<thead>
<tr style={{ textAlign: 'left', borderBottom: '1px solid var(--border)', color: 'var(--text2)' }}>
<th style={{ padding: '8px 6px' }}>Datum</th>
<th style={{ padding: '8px 6px' }}>Wert</th>
<th style={{ padding: '8px 6px' }}>Einheit</th>
<th style={{ padding: '8px 6px', width: 100 }} />
</tr>
</thead>
<tbody>
{entries.map((row) => (
<tr key={row.id} style={{ borderBottom: '1px solid var(--border)' }}>
<td style={{ padding: '10px 6px', whiteSpace: 'nowrap' }}>
{String(row.effective_date || '').slice(0, 10)}
</td>
<td style={{ padding: '10px 6px' }}>{formatEntryValue(row)}</td>
<td style={{ padding: '10px 6px', color: 'var(--text2)' }}>{row.unit}</td>
<td style={{ padding: '6px', textAlign: 'right' }}>
<button
type="button"
className="btn btn-secondary"
style={{ padding: '6px 10px', marginRight: 6 }}
title="Bearbeiten"
onClick={() => startEdit(row)}
>
<Pencil size={14} />
</button>
<button
type="button"
className="btn btn-secondary"
style={{ padding: '6px 10px', color: 'var(--danger)' }}
title="Löschen"
onClick={() => handleDelete(row.id)}
>
<Trash2 size={14} />
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</>
)}
</div>
)
}
+18 -1
View File
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react'
import { Save, Download, Upload, Check, LogOut, Key, BarChart3, Target } from 'lucide-react'
import { Save, Download, Upload, Check, LogOut, Key, BarChart3, Target, Gauge } from 'lucide-react'
import { Link } from 'react-router-dom'
import { useProfile } from '../context/ProfileContext'
import { useAuth } from '../context/AuthContext'
@@ -428,6 +428,23 @@ export default function SettingsPage() {
</button>
</div>
<div className="card section-gap">
<div className="card-title" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Gauge size={15} color="var(--accent)" /> Referenzwerte
</div>
<p style={{ fontSize: 13, color: 'var(--text2)', marginBottom: 12, lineHeight: 1.6 }}>
Persönliche Kennwerte (z. B. HF-Schwellen, Trainingshäufigkeit) – historisch zum Profil gespeichert,
keine Admin-Konfiguration.
</p>
<Link
to="/settings/reference-values"
className="btn btn-secondary btn-full"
style={{ textAlign: 'center', textDecoration: 'none', boxSizing: 'border-box' }}
>
Referenzwerte verwalten
</Link>
</div>
<div className="card section-gap">
<div className="card-title" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<Target size={15} color="var(--accent)" /> Strategische Ziele
+8
View File
@@ -40,6 +40,14 @@ export const api = {
getProfile: () => req('/profile'),
updateActiveProfile:(d)=> req('/profile', jput(d)),
// Persönliche Referenzwerte (Profil, historisch)
listReferenceValueTypes: () => req('/reference-value-types'),
listProfileReferenceValues: (typeKey) =>
req(`/profile-reference-values?type_key=${encodeURIComponent(typeKey)}`),
createProfileReferenceValue: (d) => req('/profile-reference-values', json(d)),
updateProfileReferenceValue: (id, d) => req(`/profile-reference-values/${id}`, jput(d)),
deleteProfileReferenceValue: (id) => req(`/profile-reference-values/${id}`, { method: 'DELETE' }),
// Weight
listWeight: (l=365) => req(`/weight?limit=${l}`),
upsertWeight: (date,weight,note='') => req('/weight',json({date,weight,note})),