import { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import { Check } from 'lucide-react' import dayjs from 'dayjs' import { api } from '../../utils/api' /** * Schnelleingabe: Gewicht + Baseline Vitals (Ruhepuls, HRV, VO₂max) für heute. */ export default function PilotQuickCapture({ onSaved }) { const today = dayjs().format('YYYY-MM-DD') const [weightInput, setWeightInput] = useState('') const [weightSaving, setWeightSaving] = useState(false) const [weightSaved, setWeightSaved] = useState(false) const [weightErr, setWeightErr] = useState(null) const [vForm, setVForm] = useState({ id: null, resting_hr: '', hrv: '', vo2_max: '', }) const [vSaving, setVSaving] = useState(false) const [vErr, setVErr] = useState(null) const [vOk, setVOk] = useState(false) useEffect(() => { api.weightStats().then((s) => { if (s?.latest?.date === today) setWeightInput(String(s.latest.weight)) }).catch(() => {}) }, [today]) useEffect(() => { let cancelled = false ;(async () => { try { const existing = await api.getBaselineByDate(today) if (cancelled || !existing?.id) return setVForm({ id: existing.id, resting_hr: existing.resting_hr != null ? String(existing.resting_hr) : '', hrv: existing.hrv != null ? String(existing.hrv) : '', vo2_max: existing.vo2_max != null ? String(existing.vo2_max) : '', }) } catch (err) { const msg = String(err?.message || '') if (msg.includes('404') || msg.toLowerCase().includes('nicht gefunden')) { setVForm((f) => ({ ...f, id: null, resting_hr: '', hrv: '', vo2_max: '' })) } } })() return () => { cancelled = true } }, [today]) const saveWeight = async () => { const w = parseFloat(weightInput) if (!w || w < 20 || w > 300) return setWeightSaving(true) setWeightErr(null) try { await api.upsertWeight(today, w) setWeightSaved(true) onSaved?.() setTimeout(() => setWeightSaved(false), 2000) } catch (e) { setWeightErr(e.message || 'Fehler') } finally { setWeightSaving(false) } } const saveVitals = async () => { setVSaving(true) setVErr(null) setVOk(false) try { const payload = { date: today } if (vForm.resting_hr) payload.resting_hr = parseInt(vForm.resting_hr, 10) if (vForm.hrv) payload.hrv = parseInt(vForm.hrv, 10) if (vForm.vo2_max) payload.vo2_max = parseFloat(vForm.vo2_max) if (!payload.resting_hr && !payload.hrv && !payload.vo2_max) { setVErr('Mindestens Ruhepuls, HRV oder VO₂max angeben.') setVSaving(false) return } if (vForm.id) { await api.updateBaseline(vForm.id, payload) } else { const created = await api.createBaseline(payload) if (created?.id) setVForm((f) => ({ ...f, id: created.id })) } setVOk(true) onSaved?.() setTimeout(() => setVOk(false), 2000) } catch (e) { setVErr(e.message || 'Speichern fehlgeschlagen') } finally { setVSaving(false) } } const cellStyle = { flex: '1 1 140px', minWidth: 0, padding: 12, borderRadius: 10, border: '1px solid var(--border)', background: 'var(--surface2)', } return (
Gewicht separat; Vitalwerte typischerweise gemeinsam.{' '} Volle Vitalwerte-Seite →