feat: add ZIP import functionality
- Backend: POST /api/import/zip endpoint with validation and rollback - CSV import with ON CONFLICT DO NOTHING for duplicate detection - Photo import with existence check - AI insights import - Frontend: file upload UI in SettingsPage - Import summary showing count per category - Full transaction rollback on error Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
e10e9d7eb9
commit
115d975335
3 files changed
+507
-1
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useState } from 'react'
|
||||
import { Save, Download, Trash2, Plus, Check, Pencil, X, LogOut, Shield, Key } from 'lucide-react'
|
||||
import { Save, Download, Upload, Trash2, Plus, Check, Pencil, X, LogOut, Shield, Key } from 'lucide-react'
|
||||
import { useProfile } from '../context/ProfileContext'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import { Avatar } from './ProfileSelect'
|
||||
@@ -123,6 +123,73 @@ export default function SettingsPage() {
|
||||
// editingId: string ID of profile being edited, or 'new' for new profile, or null
|
||||
const [editingId, setEditingId] = useState(null)
|
||||
const [saved, setSaved] = useState(false)
|
||||
const [importing, setImporting] = useState(false)
|
||||
const [importMsg, setImportMsg] = useState(null)
|
||||
|
||||
const handleImport = async (e) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (!file) return
|
||||
|
||||
if (!confirm(`Backup "${file.name}" importieren? Vorhandene Einträge werden nicht überschrieben.`)) {
|
||||
e.target.value = '' // Reset file input
|
||||
return
|
||||
}
|
||||
|
||||
setImporting(true)
|
||||
setImportMsg(null)
|
||||
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
const token = localStorage.getItem('bodytrack_token')||''
|
||||
const pid = localStorage.getItem('bodytrack_active_profile')||''
|
||||
|
||||
const res = await fetch('/api/import/zip', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Auth-Token': token,
|
||||
'X-Profile-Id': pid
|
||||
},
|
||||
body: formData
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(data.detail || 'Import fehlgeschlagen')
|
||||
}
|
||||
|
||||
// Show success message with stats
|
||||
const stats = data.stats
|
||||
const lines = []
|
||||
if (stats.weight > 0) lines.push(`${stats.weight} Gewicht`)
|
||||
if (stats.circumferences > 0) lines.push(`${stats.circumferences} Umfänge`)
|
||||
if (stats.caliper > 0) lines.push(`${stats.caliper} Caliper`)
|
||||
if (stats.nutrition > 0) lines.push(`${stats.nutrition} Ernährung`)
|
||||
if (stats.activity > 0) lines.push(`${stats.activity} Aktivität`)
|
||||
if (stats.photos > 0) lines.push(`${stats.photos} Fotos`)
|
||||
if (stats.insights > 0) lines.push(`${stats.insights} KI-Analysen`)
|
||||
|
||||
setImportMsg({
|
||||
type: 'success',
|
||||
text: `✓ Import erfolgreich: ${lines.join(', ')}`
|
||||
})
|
||||
|
||||
// Refresh data (in case new entries were added)
|
||||
await refreshProfiles()
|
||||
|
||||
} catch (err) {
|
||||
setImportMsg({
|
||||
type: 'error',
|
||||
text: `✗ ${err.message}`
|
||||
})
|
||||
} finally {
|
||||
setImporting(false)
|
||||
e.target.value = '' // Reset file input
|
||||
setTimeout(() => setImportMsg(null), 5000)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSave = async (form, profileId) => {
|
||||
const data = {}
|
||||
@@ -307,6 +374,55 @@ export default function SettingsPage() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Import */}
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Backup importieren</div>
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:12,lineHeight:1.6}}>
|
||||
Importiere einen ZIP-Export zurück in <strong>{activeProfile?.name}</strong>.
|
||||
Vorhandene Einträge werden nicht überschrieben.
|
||||
</p>
|
||||
<div style={{display:'flex',flexDirection:'column',gap:8}}>
|
||||
{!canExport && (
|
||||
<div style={{padding:'10px 12px',background:'#FCEBEB',borderRadius:8,
|
||||
fontSize:13,color:'#D85A30',marginBottom:8}}>
|
||||
🔒 Import ist für dein Profil nicht freigeschaltet. Bitte den Admin kontaktieren.
|
||||
</div>
|
||||
)}
|
||||
{canExport && (
|
||||
<>
|
||||
<label className="btn btn-primary btn-full"
|
||||
style={{cursor:importing?'wait':'pointer',opacity:importing?0.6:1}}>
|
||||
<input type="file" accept=".zip" onChange={handleImport}
|
||||
disabled={importing}
|
||||
style={{display:'none'}}/>
|
||||
{importing ? (
|
||||
<>Importiere...</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={14}/> ZIP-Backup importieren
|
||||
</>
|
||||
)}
|
||||
</label>
|
||||
{importMsg && (
|
||||
<div style={{
|
||||
padding:'10px 12px',
|
||||
background: importMsg.type === 'success' ? '#E1F5EE' : '#FCEBEB',
|
||||
borderRadius:8,
|
||||
fontSize:12,
|
||||
color: importMsg.type === 'success' ? 'var(--accent)' : '#D85A30',
|
||||
lineHeight:1.4
|
||||
}}>
|
||||
{importMsg.text}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<p style={{fontSize:11,color:'var(--text3)',marginTop:8}}>
|
||||
Der Import erkennt automatisch das Format und importiert nur neue Einträge.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{saved && (
|
||||
<div style={{position:'fixed',bottom:80,left:'50%',transform:'translateX(-50%)',
|
||||
background:'var(--accent)',color:'white',padding:'8px 20px',borderRadius:20,
|
||||
|
||||
Reference in new issue
Block a user