fix: BLS-Import-UI und Timeout beim Daten-Apply
Datei-Button volle Breite, Import als Aktionsbutton, Batch-Upsert statt Einzel-INSERTs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7ccae33844
commit
8e964509d7
7 files changed
+170
-145
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
function summarizeCheck(kind, res) {
|
||||
@@ -7,48 +7,8 @@ function summarizeCheck(kind, res) {
|
||||
return `${res.foods ?? 0} Lebensmittel, ${res.attribute_columns ?? 0} Stoffspalten`
|
||||
}
|
||||
|
||||
function ImportSwitch({ on, disabled, busy, onEnable }) {
|
||||
return (
|
||||
<label style={{ display: 'flex', alignItems: 'center', gap: 10, opacity: disabled ? 0.5 : 1 }}>
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={on}
|
||||
aria-label="Import"
|
||||
disabled={disabled || busy || on}
|
||||
onClick={() => { if (!on && !disabled && !busy) onEnable() }}
|
||||
style={{
|
||||
width: 44,
|
||||
height: 24,
|
||||
borderRadius: 12,
|
||||
border: 'none',
|
||||
padding: 0,
|
||||
background: on ? 'var(--accent)' : 'var(--border)',
|
||||
position: 'relative',
|
||||
cursor: disabled || busy || on ? 'not-allowed' : 'pointer',
|
||||
}}
|
||||
>
|
||||
<span
|
||||
style={{
|
||||
position: 'absolute',
|
||||
top: 2,
|
||||
left: on ? 22 : 2,
|
||||
width: 20,
|
||||
height: 20,
|
||||
borderRadius: '50%',
|
||||
background: '#fff',
|
||||
display: 'block',
|
||||
}}
|
||||
/>
|
||||
</button>
|
||||
<span style={{ fontSize: 14, color: 'var(--text1)' }}>
|
||||
{on ? 'Importiert' : 'Import'}
|
||||
</span>
|
||||
</label>
|
||||
)
|
||||
}
|
||||
|
||||
function FileImportBlock({ label, kind, onImported }) {
|
||||
const inputRef = useRef(null)
|
||||
const [file, setFile] = useState(null)
|
||||
const [check, setCheck] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
@@ -92,20 +52,29 @@ function FileImportBlock({ label, kind, onImported }) {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="form-row" style={{ flexDirection: 'column', alignItems: 'stretch', gap: 8, marginTop: 16 }}>
|
||||
<label className="form-label">{label}</label>
|
||||
<div className="settings-page__field">
|
||||
<label className="settings-page__field-label">{label}</label>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept=".xlsx"
|
||||
className="form-input"
|
||||
disabled={busy}
|
||||
disabled={!!busy}
|
||||
style={{ display: 'none' }}
|
||||
onChange={(e) => {
|
||||
const next = e.target.files?.[0] || null
|
||||
setFile(next)
|
||||
checkFile(next)
|
||||
}}
|
||||
/>
|
||||
{busy && <p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>{busy === 'apply' ? 'Importiere…' : 'Prüfe…'}</p>}
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
disabled={!!busy}
|
||||
onClick={() => inputRef.current?.click()}
|
||||
>
|
||||
{file ? file.name : 'Datei auswählen'}
|
||||
</button>
|
||||
{busy && <p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>{busy === 'apply' ? 'Importiere… das kann ein paar Minuten dauern.' : 'Prüfe…'}</p>}
|
||||
{error && <p style={{ color: 'var(--danger)', margin: 0 }}>{error}</p>}
|
||||
{check && !error && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
@@ -114,15 +83,18 @@ function FileImportBlock({ label, kind, onImported }) {
|
||||
)}
|
||||
{applyResult && (
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', margin: 0 }}>
|
||||
{applyResult.inserted ?? 0} neu · {applyResult.updated ?? 0} aktualisiert
|
||||
{(applyResult.inserted ?? applyResult.foods_inserted) ?? 0} neu · {(applyResult.updated ?? applyResult.foods_updated) ?? 0} aktualisiert
|
||||
{applyResult.values_written != null ? ` · ${applyResult.values_written} Werte` : ''}
|
||||
</p>
|
||||
)}
|
||||
<ImportSwitch
|
||||
on={imported}
|
||||
disabled={!check || !!error}
|
||||
busy={busy}
|
||||
onEnable={applyImport}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-full"
|
||||
disabled={!check || !!error || !!busy || imported}
|
||||
onClick={applyImport}
|
||||
>
|
||||
{imported ? 'Importiert' : 'Importieren'}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -141,7 +113,7 @@ export default function AdminBlsImportPage() {
|
||||
<h1 className="page-title">BLS 4.0 importieren</h1>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', lineHeight: 1.6 }}>
|
||||
Offizielle Dateien von blsdb.de (frei verfügbar, MRI). Codes bleiben erhalten.
|
||||
Datei wählen prüft automatisch. Der Schalter startet den Import. Zuerst Components, dann Daten.
|
||||
Datei wählen prüft automatisch. Danach Importieren. Zuerst Components, dann Daten.
|
||||
</p>
|
||||
{status && (
|
||||
<p style={{ fontSize: 13 }}>
|
||||
|
||||
@@ -73,6 +73,29 @@ async function req(path, opts={}) {
|
||||
}
|
||||
return res.json()
|
||||
}
|
||||
async function readJsonResponse(res) {
|
||||
const text = await res.text()
|
||||
const trimmed = (text || '').trim()
|
||||
if (!trimmed) {
|
||||
throw new Error(res.ok ? 'Leere Antwort' : `HTTP ${res.status}`)
|
||||
}
|
||||
if (trimmed.startsWith('<')) {
|
||||
throw new Error(
|
||||
`Server lieferte HTML statt JSON (HTTP ${res.status}). Meist Timeout oder Datei zu groß — erneut versuchen.`
|
||||
)
|
||||
}
|
||||
let parsed
|
||||
try {
|
||||
parsed = JSON.parse(trimmed)
|
||||
} catch {
|
||||
throw new Error(trimmed.slice(0, 180) || `HTTP ${res.status}`)
|
||||
}
|
||||
if (!res.ok) {
|
||||
throw new Error(formatFastApiDetail(parsed.detail, JSON.stringify(parsed)))
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
const json=(d)=>({method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(d)})
|
||||
const jput=(d)=>({method:'PUT', headers:{'Content-Type':'application/json'},body:JSON.stringify(d)})
|
||||
|
||||
@@ -231,12 +254,12 @@ export const api = {
|
||||
adminBlsImportComponents: async (file, dryRun=true) => {
|
||||
const fd=new FormData();fd.append('file',file)
|
||||
const r=await fetch(`${BASE}/admin/bls/import/components?dry_run=${dryRun}`,{method:'POST',body:fd,headers:hdrs()})
|
||||
const d=await r.json();if(!r.ok)throw new Error(formatFastApiDetail(d.detail, JSON.stringify(d)));return d
|
||||
return readJsonResponse(r)
|
||||
},
|
||||
adminBlsImportFoods: async (file, dryRun=true) => {
|
||||
const fd=new FormData();fd.append('file',file)
|
||||
const r=await fetch(`${BASE}/admin/bls/import/foods?dry_run=${dryRun}`,{method:'POST',body:fd,headers:hdrs()})
|
||||
const d=await r.json();if(!r.ok)throw new Error(formatFastApiDetail(d.detail, JSON.stringify(d)));return d
|
||||
return readJsonResponse(r)
|
||||
},
|
||||
adminBlsFoods: (q, kind) => req(`/admin/bls/foods?${q?('q='+encodeURIComponent(q)+'&'):''}${kind?('kind='+kind):''}`),
|
||||
adminBlsFoodDetail: (id) => req(`/admin/bls/foods/${id}`),
|
||||
|
||||
Reference in new issue
Block a user