feat: add CSV import for Vitals (Omron + Apple Health)
- Import endpoints for Omron blood pressure CSV (German date format)
- Import endpoints for Apple Health vitals CSV
- Import UI tab in VitalsPage with drag & drop for both sources
- German month mapping for Omron date parsing ("13 März 2026")
- Upsert logic preserves manual entries (source != 'manual')
- Import result feedback (inserted/updated/skipped/errors)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a55f11bc96
commit
548a5a481d
3 files changed
+501
-3
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { Pencil, Trash2, X, TrendingUp, TrendingDown, Minus } from 'lucide-react'
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { Pencil, Trash2, X, TrendingUp, TrendingDown, Minus, Upload } from 'lucide-react'
|
||||
import { api } from '../utils/api'
|
||||
import dayjs from 'dayjs'
|
||||
import 'dayjs/locale/de'
|
||||
@@ -235,6 +235,15 @@ export default function VitalsPage() {
|
||||
const [saved, setSaved] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
|
||||
// Import states
|
||||
const [importingOmron, setImportingOmron] = useState(false)
|
||||
const [importingApple, setImportingApple] = useState(false)
|
||||
const [draggingOmron, setDraggingOmron] = useState(false)
|
||||
const [draggingApple, setDraggingApple] = useState(false)
|
||||
const [importResult, setImportResult] = useState(null)
|
||||
const omronFileInputRef = useRef(null)
|
||||
const appleFileInputRef = useRef(null)
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const [e, s] = await Promise.all([api.listVitals(90), api.getVitalsStats(30)])
|
||||
@@ -340,6 +349,71 @@ export default function VitalsPage() {
|
||||
return <Minus size={14} style={{ color: 'var(--text3)' }} />
|
||||
}
|
||||
|
||||
// Import handlers
|
||||
const handleOmronImport = async (file) => {
|
||||
if (!file || !file.name.endsWith('.csv')) {
|
||||
setError('Bitte eine CSV-Datei auswählen')
|
||||
setTimeout(() => setError(null), 3000)
|
||||
return
|
||||
}
|
||||
|
||||
setImportingOmron(true)
|
||||
setImportResult(null)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
const result = await api.importVitalsOmron(file)
|
||||
setImportResult({
|
||||
type: 'omron',
|
||||
inserted: result.inserted || 0,
|
||||
updated: result.updated || 0,
|
||||
skipped: result.skipped || 0,
|
||||
errors: result.errors || 0
|
||||
})
|
||||
await load()
|
||||
} catch (err) {
|
||||
setError('Omron Import fehlgeschlagen: ' + err.message)
|
||||
setTimeout(() => setError(null), 5000)
|
||||
} finally {
|
||||
setImportingOmron(false)
|
||||
if (omronFileInputRef.current) {
|
||||
omronFileInputRef.current.value = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleAppleImport = async (file) => {
|
||||
if (!file || !file.name.endsWith('.csv')) {
|
||||
setError('Bitte eine CSV-Datei auswählen')
|
||||
setTimeout(() => setError(null), 3000)
|
||||
return
|
||||
}
|
||||
|
||||
setImportingApple(true)
|
||||
setImportResult(null)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
const result = await api.importVitalsAppleHealth(file)
|
||||
setImportResult({
|
||||
type: 'apple_health',
|
||||
inserted: result.inserted || 0,
|
||||
updated: result.updated || 0,
|
||||
skipped: result.skipped || 0,
|
||||
errors: result.errors || 0
|
||||
})
|
||||
await load()
|
||||
} catch (err) {
|
||||
setError('Apple Health Import fehlgeschlagen: ' + err.message)
|
||||
setTimeout(() => setError(null), 5000)
|
||||
} finally {
|
||||
setImportingApple(false)
|
||||
if (appleFileInputRef.current) {
|
||||
appleFileInputRef.current.value = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1 className="page-title">Vitalwerte</h1>
|
||||
@@ -351,6 +425,9 @@ export default function VitalsPage() {
|
||||
<button className={'tab' + (tab === 'add' ? ' active' : '')} onClick={() => setTab('add')}>
|
||||
+ Erfassen
|
||||
</button>
|
||||
<button className={'tab' + (tab === 'import' ? ' active' : '')} onClick={() => setTab('import')}>
|
||||
Import
|
||||
</button>
|
||||
<button className={'tab' + (tab === 'stats' ? ' active' : '')} onClick={() => setTab('stats')}>
|
||||
Statistik
|
||||
</button>
|
||||
@@ -470,6 +547,156 @@ export default function VitalsPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tab === 'import' && (
|
||||
<div>
|
||||
{error && (
|
||||
<div style={{
|
||||
padding: '10px',
|
||||
background: '#FCEBEB',
|
||||
border: '1px solid #D85A30',
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
color: '#D85A30',
|
||||
marginBottom: 12
|
||||
}}>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{importResult && (
|
||||
<div style={{
|
||||
padding: '12px',
|
||||
background: '#E8F7F0',
|
||||
border: '1px solid #1D9E75',
|
||||
borderRadius: 8,
|
||||
fontSize: 13,
|
||||
color: '#085041',
|
||||
marginBottom: 12
|
||||
}}>
|
||||
<strong>Import erfolgreich ({importResult.type === 'omron' ? 'Omron' : 'Apple Health'}):</strong><br />
|
||||
{importResult.inserted} neu · {importResult.updated} aktualisiert · {importResult.skipped} übersprungen
|
||||
{importResult.errors > 0 && <> · {importResult.errors} Fehler</>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Omron Import */}
|
||||
<div className="card" style={{ marginBottom: 12 }}>
|
||||
<div className="card-title">Omron Blutdruckmessgerät</div>
|
||||
<p style={{ fontSize: 12, color: 'var(--text2)', marginBottom: 12, lineHeight: 1.5 }}>
|
||||
Exportiere CSV aus der Omron Connect App:<br />
|
||||
• Blutdruck (Systolisch/Diastolisch)<br />
|
||||
• Puls<br />
|
||||
• Unregelmäßiger Herzschlag & AFib-Warnungen
|
||||
</p>
|
||||
<div
|
||||
onDragOver={e => { e.preventDefault(); setDraggingOmron(true) }}
|
||||
onDragLeave={() => setDraggingOmron(false)}
|
||||
onDrop={e => {
|
||||
e.preventDefault()
|
||||
setDraggingOmron(false)
|
||||
const file = e.dataTransfer.files[0]
|
||||
if (file) handleOmronImport(file)
|
||||
}}
|
||||
onClick={() => omronFileInputRef.current?.click()}
|
||||
style={{
|
||||
border: `2px dashed ${draggingOmron ? 'var(--accent)' : 'var(--border)'}`,
|
||||
borderRadius: 10,
|
||||
padding: '20px 16px',
|
||||
textAlign: 'center',
|
||||
background: draggingOmron ? 'var(--accent)14' : 'var(--surface2)',
|
||||
cursor: importingOmron ? 'not-allowed' : 'pointer',
|
||||
transition: 'all 0.15s',
|
||||
opacity: importingOmron ? 0.6 : 1
|
||||
}}>
|
||||
{importingOmron ? (
|
||||
<>
|
||||
<div className="spinner" style={{ width: 24, height: 24, margin: '0 auto 8px' }} />
|
||||
<div style={{ fontSize: 14, fontWeight: 500, color: 'var(--text2)' }}>
|
||||
Importiere Omron-Daten...
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={28} style={{ color: draggingOmron ? 'var(--accent)' : 'var(--text3)', marginBottom: 8 }} />
|
||||
<div style={{ fontSize: 14, fontWeight: 500, color: draggingOmron ? 'var(--accent-dark)' : 'var(--text2)' }}>
|
||||
{draggingOmron ? 'CSV loslassen...' : 'Omron CSV hierher ziehen oder tippen'}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<input
|
||||
ref={omronFileInputRef}
|
||||
type="file"
|
||||
accept=".csv"
|
||||
onChange={e => {
|
||||
const file = e.target.files[0]
|
||||
if (file) handleOmronImport(file)
|
||||
}}
|
||||
style={{ display: 'none' }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Apple Health Import */}
|
||||
<div className="card">
|
||||
<div className="card-title">Apple Health</div>
|
||||
<p style={{ fontSize: 12, color: 'var(--text2)', marginBottom: 12, lineHeight: 1.5 }}>
|
||||
Exportiere Health-Daten von der Health-App:<br />
|
||||
• Ruhepuls (Resting Heart Rate)<br />
|
||||
• HRV (Heart Rate Variability)<br />
|
||||
• VO2 Max<br />
|
||||
• SpO2 (Blutsauerstoffsättigung)<br />
|
||||
• Atemfrequenz
|
||||
</p>
|
||||
<div
|
||||
onDragOver={e => { e.preventDefault(); setDraggingApple(true) }}
|
||||
onDragLeave={() => setDraggingApple(false)}
|
||||
onDrop={e => {
|
||||
e.preventDefault()
|
||||
setDraggingApple(false)
|
||||
const file = e.dataTransfer.files[0]
|
||||
if (file) handleAppleImport(file)
|
||||
}}
|
||||
onClick={() => appleFileInputRef.current?.click()}
|
||||
style={{
|
||||
border: `2px dashed ${draggingApple ? 'var(--accent)' : 'var(--border)'}`,
|
||||
borderRadius: 10,
|
||||
padding: '20px 16px',
|
||||
textAlign: 'center',
|
||||
background: draggingApple ? 'var(--accent)14' : 'var(--surface2)',
|
||||
cursor: importingApple ? 'not-allowed' : 'pointer',
|
||||
transition: 'all 0.15s',
|
||||
opacity: importingApple ? 0.6 : 1
|
||||
}}>
|
||||
{importingApple ? (
|
||||
<>
|
||||
<div className="spinner" style={{ width: 24, height: 24, margin: '0 auto 8px' }} />
|
||||
<div style={{ fontSize: 14, fontWeight: 500, color: 'var(--text2)' }}>
|
||||
Importiere Apple Health-Daten...
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={28} style={{ color: draggingApple ? 'var(--accent)' : 'var(--text3)', marginBottom: 8 }} />
|
||||
<div style={{ fontSize: 14, fontWeight: 500, color: draggingApple ? 'var(--accent-dark)' : 'var(--text2)' }}>
|
||||
{draggingApple ? 'CSV loslassen...' : 'Apple Health CSV hierher ziehen oder tippen'}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<input
|
||||
ref={appleFileInputRef}
|
||||
type="file"
|
||||
accept=".csv"
|
||||
onChange={e => {
|
||||
const file = e.target.files[0]
|
||||
if (file) handleAppleImport(file)
|
||||
}}
|
||||
style={{ display: 'none' }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tab === 'stats' && stats && (
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Trend-Analyse (14 Tage)</div>
|
||||
|
||||
Reference in new issue
Block a user