This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { useState } from 'react';
|
||||
import { StyleSheet, Text, TextInput, View } from 'react-native';
|
||||
import PrimaryButton from '../components/PrimaryButton';
|
||||
import AppHeader from '../components/layout/AppHeader';
|
||||
import ScreenContainer from '../components/layout/ScreenContainer';
|
||||
import { colors } from '../utils/constants';
|
||||
|
||||
const nutrientFields = [['Protein (g/100 g)', 'proteinPer100g'], ['Zucker (g/100 g)', 'sugarPer100g'],
|
||||
['Kohlenhydrate (g/100 g)', 'carbohydratesPer100g'], ['Ballaststoffe (g/100 g)', 'fiberPer100g'],
|
||||
['Vitamin B12 (µg/100 g)', 'vitaminB12MicrogramsPer100g'],
|
||||
['Energie (kJ/100 g)', 'energyKjPer100g'], ['Gesättigte Fettsäuren (g/100 g)', 'saturatedFatPer100g'],
|
||||
['Salz (g/100 g)', 'saltPer100g']];
|
||||
|
||||
export default function ProductEditScreen({ product, isNew, onBack, onSave }) {
|
||||
const [form, setForm] = useState({ name: product.name || '', brand: product.brand || '',
|
||||
category: product.category || '', ingredients: (product.ingredients || []).join(', '),
|
||||
allergens: (product.allergens || []).join(', '),
|
||||
...Object.fromEntries(nutrientFields.map(([, key]) => [key, String(product.nutrition?.[key] ?? '')])) });
|
||||
const [error, setError] = useState('');
|
||||
function save() {
|
||||
if (!form.name.trim()) { setError('Bitte gib einen Produktnamen ein.'); return; }
|
||||
const nutrition = {};
|
||||
for (const [, key] of nutrientFields) {
|
||||
if (!form[key].trim()) continue;
|
||||
const value = Number(form[key].replace(',', '.'));
|
||||
if (!Number.isFinite(value) || value < 0) { setError('Bitte gib gültige Nährwerte ein.'); return; }
|
||||
nutrition[key] = value;
|
||||
}
|
||||
const list = (value) => value.split(',').map((part) => part.trim()).filter(Boolean);
|
||||
onSave({ name: form.name.trim(), brand: form.brand.trim(), category: form.category.trim(),
|
||||
ingredients: list(form.ingredients), allergens: list(form.allergens), nutrition });
|
||||
}
|
||||
return <ScreenContainer><AppHeader title={isNew ? 'Produkt erfassen' : 'Produkt korrigieren'} onBack={onBack} />
|
||||
<Text style={styles.hint}>Übertrage die Angaben von der Verpackung. Alle Änderungen bleiben lokal.</Text>
|
||||
{[["Name", 'name'], ['Marke', 'brand'], ['Kategorie', 'category'], ['Zutaten (kommagetrennt)', 'ingredients'],
|
||||
['Allergene (kommagetrennt)', 'allergens'], ...nutrientFields].map(([label, key]) =>
|
||||
<View key={key}><Text style={styles.label}>{label}</Text><TextInput accessibilityLabel={label} value={form[key]}
|
||||
keyboardType={nutrientFields.some(([, field]) => field === key) ? 'decimal-pad' : 'default'}
|
||||
onChangeText={(value) => setForm({ ...form, [key]: value })} style={styles.input} /></View>)}
|
||||
{error ? <Text accessibilityRole="alert" style={styles.error}>{error}</Text> : null}
|
||||
<PrimaryButton label="Angaben speichern" onPress={save} />
|
||||
</ScreenContainer>;
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
hint: { color: colors.muted, fontSize: 14, lineHeight: 20 },
|
||||
label: { color: colors.text, fontWeight: '700', marginBottom: 5, marginTop: 12 },
|
||||
input: { backgroundColor: colors.surface, borderColor: colors.border, borderRadius: 10, borderWidth: 1, color: colors.text, fontSize: 16, padding: 10 },
|
||||
error: { color: colors.danger }
|
||||
});
|
||||
Reference in New Issue
Block a user