This commit is contained in:
@@ -1,27 +1,37 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { StyleSheet, Text, View } from 'react-native';
|
||||
|
||||
import PrimaryButton from '../components/PrimaryButton';
|
||||
import AppHeader from '../components/layout/AppHeader';
|
||||
import ScreenContainer from '../components/layout/ScreenContainer';
|
||||
import AlternativeProductCard from '../components/product/AlternativeProductCard';
|
||||
import { findAlternatives } from '../services/alternativesService';
|
||||
import { colors } from '../utils/constants';
|
||||
|
||||
const DISPLAY_ALTERNATIVE_LIMIT = 5;
|
||||
const DISPLAY_ALTERNATIVE_LIMIT = 8;
|
||||
|
||||
export default function AlternativesScreen({ product, preferences, productCache, onBack, onChoose, onOpenProduct }) {
|
||||
export default function AlternativesScreen({ product, preferences, productCache, shoppingList, onBack, onChoose, onOpenProduct }) {
|
||||
const [alternatives, setAlternatives] = useState([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [searchError, setSearchError] = useState('');
|
||||
const [retryCount, setRetryCount] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
async function loadAlternatives() {
|
||||
setIsLoading(true);
|
||||
const results = await findAlternatives(product, preferences, productCache);
|
||||
if (isMounted) {
|
||||
setAlternatives(results);
|
||||
setIsLoading(false);
|
||||
setSearchError('');
|
||||
try {
|
||||
const results = await findAlternatives(product, preferences, productCache, { shoppingList });
|
||||
if (isMounted) setAlternatives(results);
|
||||
} catch (error) {
|
||||
if (isMounted) {
|
||||
setAlternatives([]);
|
||||
setSearchError('Open Food Facts ist gerade nicht erreichbar. Versuche es später erneut oder nutze den Offline-Modus.');
|
||||
}
|
||||
} finally {
|
||||
if (isMounted) setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,21 +39,30 @@ export default function AlternativesScreen({ product, preferences, productCache,
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, [product, preferences, productCache]);
|
||||
}, [product, preferences, productCache, shoppingList, retryCount]);
|
||||
|
||||
return (
|
||||
<ScreenContainer>
|
||||
<AppHeader
|
||||
title="Alternativen"
|
||||
subtitle={`Bessere Optionen für ${product.name}`}
|
||||
subtitle={`Gesündere Optionen für ${product.name}`}
|
||||
onBack={onBack}
|
||||
/>
|
||||
|
||||
{isLoading ? <Text style={styles.note}>Alternativen werden berechnet...</Text> : null}
|
||||
{!isLoading && alternatives.length === 0 ? (
|
||||
{isLoading ? <Text style={styles.note}>{preferences.offlineModeEnabled
|
||||
? 'Gespeicherte Produkte werden verglichen...'
|
||||
: 'Open Food Facts wird nach gesünderen Produkten derselben Kategorie durchsucht...'}</Text> : null}
|
||||
{searchError ? <Text style={styles.note}>{searchError}</Text> : null}
|
||||
{searchError ? <PrimaryButton label="Erneut suchen" onPress={() => setRetryCount((count) => count + 1)} /> : null}
|
||||
{!isLoading && alternatives.length > 0 ? (
|
||||
<Text style={styles.note}>{alternatives.length} mögliche Alternative{alternatives.length === 1 ? '' : 'n'} gefunden. Die Ernährungswertung und weitere Kriterien stehen in den Produktdetails.</Text>
|
||||
) : null}
|
||||
{!isLoading && !searchError && alternatives.length === 0 ? (
|
||||
<View style={styles.empty}>
|
||||
<Text style={styles.emptyTitle}>Keine Alternative gefunden</Text>
|
||||
<Text style={styles.emptyCopy}>Für dieses Produkt wurde in ähnlichen Kategorien noch keine klar bessere Option gefunden.</Text>
|
||||
<Text style={styles.emptyCopy}>{preferences.offlineModeEnabled
|
||||
? 'Im lokalen Cache ist noch keine gesündere Option aus derselben Kategorie gespeichert. Deaktiviere den Offline-Modus für die Online-Suche.'
|
||||
: 'In dieser Kategorie wurde keine gesündere oder insgesamt besser bewertete Option mit ausreichenden Angaben gefunden.'}</Text>
|
||||
</View>
|
||||
) : null}
|
||||
|
||||
@@ -52,7 +71,7 @@ export default function AlternativesScreen({ product, preferences, productCache,
|
||||
item={item}
|
||||
key={item.product.id}
|
||||
onChoose={() => {
|
||||
if (typeof item.product.price === 'number') {
|
||||
if (!item.requiresAllergenCheck && typeof item.product.price === 'number') {
|
||||
onChoose(item.product);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user