feat: Rezepte beim Zuordnen anlegen und offene Zutaten listen
Lebensmittel oder Rezept wählbar; gemappte Zutaten werden übernommen, offene erscheinen in der Liste. Dazu Fettgehalt-Suche, EPA-Stoffe, Rezept-CRUD und Wechsel bestehender Zuordnungen. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
132a364a3a
commit
a7e5511039
26 files changed
+1234
-147
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
import FoodNutrientFields, { foodCreatePayload } from './FoodNutrientFields'
|
||||
import { RecipeForm } from './FoodRecipeEditor'
|
||||
|
||||
const COUNT_UNITS = new Set(['stück', 'scheibe', 'portion', 'becher', 'tasse'])
|
||||
const UNIT_LABEL = {
|
||||
@@ -24,17 +26,20 @@ function detectUnit(...texts) {
|
||||
return null
|
||||
}
|
||||
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, onSelect, onClose }) {
|
||||
export default function FoodSearchModal({ title, initialQuery, quantityHint, allowRecipe, learned, onSelect, onCreateRecipe, onClose }) {
|
||||
const [q, setQ] = useState(initialQuery || '')
|
||||
const [hits, setHits] = useState([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState(null)
|
||||
const [creating, setCreating] = useState(false)
|
||||
const [showCreate, setShowCreate] = useState(false)
|
||||
const [createKind, setCreateKind] = useState(null)
|
||||
const [manual, setManual] = useState({
|
||||
name_de: initialQuery || '',
|
||||
kcal: '', protein_g: '', fat_g: '', carbs_g: '',
|
||||
})
|
||||
const [nutrientExtras, setNutrientExtras] = useState([])
|
||||
const [servingG, setServingG] = useState('100')
|
||||
const suggestedUnit = detectUnit(quantityHint, initialQuery)
|
||||
const [gramsPerUnit, setGramsPerUnit] = useState(
|
||||
suggestedUnit === 'el' ? '15' : suggestedUnit === 'tl' ? '5' : ''
|
||||
@@ -44,7 +49,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
const abortRef = useRef(null)
|
||||
const seqRef = useRef(0)
|
||||
|
||||
const extras = () => {
|
||||
const unitExtras = () => {
|
||||
const g = parseFloat(String(gramsPerUnit).replace(',', '.'))
|
||||
if (!suggestedUnit || !g || g <= 0) return {}
|
||||
return { grams_per_unit: g, source_unit: suggestedUnit }
|
||||
@@ -111,16 +116,8 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
setCreating(true)
|
||||
setError(null)
|
||||
try {
|
||||
const food = await api.createUserFood({
|
||||
name_de: name,
|
||||
macros_per_100g: {
|
||||
kcal: parseFloat(String(manual.kcal).replace(',', '.')) || 0,
|
||||
protein_g: parseFloat(String(manual.protein_g).replace(',', '.')) || 0,
|
||||
fat_g: parseFloat(String(manual.fat_g).replace(',', '.')) || 0,
|
||||
carbs_g: parseFloat(String(manual.carbs_g).replace(',', '.')) || 0,
|
||||
},
|
||||
})
|
||||
onSelect(food, extras())
|
||||
const food = await api.createUserFood(foodCreatePayload(name, manual, nutrientExtras, servingG))
|
||||
onSelect(food, unitExtras())
|
||||
} catch (e) {
|
||||
setError(e.message)
|
||||
} finally {
|
||||
@@ -160,12 +157,12 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
ref={inputRef}
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left' }}
|
||||
placeholder="Name eingeben, z. B. Haferflocken"
|
||||
placeholder="Name und Fettgehalt, z. B. Joghurt 10%"
|
||||
value={q}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '8px 0 0' }}>
|
||||
Suche nach dem Namen. Fehlt der Treffer, legst du unten einen eigenen Eintrag an.
|
||||
Fettgehalt mit eingeben, z. B. Joghurt 10 % oder 9,5 %. Ohne Zahl erscheint oft nur die 3,5 %-Klasse. Fehlt der Treffer, eigenen Eintrag anlegen.
|
||||
</p>
|
||||
{suggestedUnit && (
|
||||
<label style={{ display: 'block', marginTop: 10, fontSize: 13 }}>
|
||||
@@ -196,7 +193,7 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
type="button"
|
||||
className="btn btn-secondary btn-full"
|
||||
style={{ marginTop: 8, justifyContent: 'flex-start', textAlign: 'left', height: 'auto', padding: '10px 12px' }}
|
||||
onClick={() => onSelect(h, extras())}
|
||||
onClick={() => onSelect(h, unitExtras())}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ display: 'block' }}>{h.name_de}</strong>
|
||||
@@ -213,45 +210,62 @@ export default function FoodSearchModal({ title, initialQuery, quantityHint, onS
|
||||
{!showCreate ? (
|
||||
<button type="button" className="btn btn-secondary btn-full" onClick={() => {
|
||||
setShowCreate(true)
|
||||
setCreateKind(allowRecipe && onCreateRecipe ? null : 'food')
|
||||
setManual((s) => ({ ...s, name_de: s.name_de || q || initialQuery || '' }))
|
||||
}}>
|
||||
Eigenes Lebensmittel anlegen
|
||||
{allowRecipe && onCreateRecipe ? 'Lebensmittel oder Rezept anlegen' : 'Eigenes Lebensmittel anlegen'}
|
||||
</button>
|
||||
) : !createKind ? (
|
||||
<div>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Was möchtest du anlegen?</p>
|
||||
<p style={{ fontSize: 12, color: 'var(--text2)', margin: '0 0 8px' }}>Lebensmittel = ein Katalogeintrag. Rezept = mehrere Zutaten; offene Zutaten erscheinen danach in der Liste.</p>
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
|
||||
<button type="button" className="btn btn-primary" onClick={() => setCreateKind('food')}>Lebensmittel</button>
|
||||
<button type="button" className="btn btn-secondary" onClick={() => setCreateKind('recipe')}>Rezept</button>
|
||||
<button type="button" className="btn btn-secondary" onClick={() => { setShowCreate(false); setCreateKind(null) }}>Zurück</button>
|
||||
</div>
|
||||
</div>
|
||||
) : createKind === 'recipe' ? (
|
||||
<RecipeForm
|
||||
defaultName={manual.name_de || q || initialQuery || ''}
|
||||
learned={learned}
|
||||
disabled={creating}
|
||||
submitLabel="Rezept anlegen und zuordnen"
|
||||
onCancel={() => setCreateKind(null)}
|
||||
onSave={onCreateRecipe}
|
||||
/>
|
||||
) : (
|
||||
<div>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Eigener Eintrag (pro 100 g)</p>
|
||||
<p style={{ fontSize: 13, fontWeight: 600, marginBottom: 8 }}>Eigener Eintrag</p>
|
||||
<input
|
||||
className="form-input"
|
||||
style={{ width: '100%', textAlign: 'left', marginBottom: 8 }}
|
||||
placeholder="Name"
|
||||
placeholder="Name, z. B. Norsan Omega-3 Algenöl"
|
||||
value={manual.name_de}
|
||||
onChange={(e) => setManual({ ...manual, name_de: e.target.value })}
|
||||
/>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
|
||||
{[['kcal', 'kcal'], ['protein_g', 'Protein g'], ['fat_g', 'Fett g'], ['carbs_g', 'Kohlenhydrate g']].map(([key, label]) => (
|
||||
<label key={key} style={{ fontSize: 12, color: 'var(--text2)' }}>
|
||||
{label}
|
||||
<input
|
||||
className="form-input"
|
||||
type="number"
|
||||
min="0"
|
||||
step="0.1"
|
||||
style={{ width: '100%', textAlign: 'left', marginTop: 4 }}
|
||||
value={manual[key]}
|
||||
onChange={(e) => setManual({ ...manual, [key]: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
))}
|
||||
<FoodNutrientFields
|
||||
macros={manual}
|
||||
setMacros={setManual}
|
||||
extras={nutrientExtras}
|
||||
setExtras={setNutrientExtras}
|
||||
servingG={servingG}
|
||||
setServingG={setServingG}
|
||||
/>
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 10 }}>
|
||||
{allowRecipe && onCreateRecipe && (
|
||||
<button type="button" className="btn btn-secondary" disabled={creating} onClick={() => setCreateKind(null)}>Zurück</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary"
|
||||
style={{ flex: 1 }}
|
||||
disabled={creating}
|
||||
onClick={createManual}
|
||||
>
|
||||
{creating ? 'Speichere…' : 'Anlegen und zuordnen'}
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-primary btn-full"
|
||||
style={{ marginTop: 10 }}
|
||||
disabled={creating}
|
||||
onClick={createManual}
|
||||
>
|
||||
{creating ? 'Speichere…' : 'Anlegen und zuordnen'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user