Files
Shoppy/src/screens/AlternativesScreen.js
T
Frederik Spirgi 7ed7ace80a
Android Release Build / build-android (push) Successful in 13m23s
remove unnecessary text
2026-11-18 14:10:30 +01:00

114 lines
4.1 KiB
JavaScript

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 { alternativeSearchIssue, findAlternatives } from '../services/alternativesService';
import { calculateProductRating } from '../services/ratingService';
import { colors } from '../utils/constants';
const DISPLAY_ALTERNATIVE_LIMIT = 8;
export default function AlternativesScreen({ product, preferences, productCache, shoppingList, dailyLog, 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);
setSearchError('');
try {
const results = await findAlternatives(product, preferences, productCache, { shoppingList, dailyLog });
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);
}
}
loadAlternatives();
return () => {
isMounted = false;
};
}, [product, preferences, productCache, shoppingList, dailyLog, retryCount]);
return (
<ScreenContainer>
<AppHeader
title="Alternativen"
subtitle={`Ähnliche, besser bewertete Produkte für ${product.name}`}
onBack={onBack}
/>
{isLoading ? <Text style={styles.note}>{preferences.offlineModeEnabled
? 'Gespeicherte Produkte werden verglichen...'
: 'Open Food Facts wird nach besseren Produkten derselben Produktart 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.</Text>
) : null}
{!isLoading && !searchError && alternatives.length === 0 ? (
<View style={styles.empty}>
<Text style={styles.emptyTitle}>Keine Alternative gefunden</Text>
<Text style={styles.emptyCopy}>{preferences.offlineModeEnabled
? 'Im lokalen Cache ist noch keine bessere Option derselben Produktart gespeichert. Deaktiviere den Offline-Modus für die Online-Suche.'
: alternativeSearchIssue(product, calculateProductRating(product, preferences, { shoppingList, dailyLog }))
|| 'Unter den geprüften Open Food Facts Treffern war keine belegbar bessere Option derselben Produktart mit ausreichenden Angaben.'}</Text>
</View>
) : null}
{alternatives.slice(0, DISPLAY_ALTERNATIVE_LIMIT).map((item) => (
<AlternativeProductCard
item={item}
key={item.product.id}
onChoose={() => {
if (!item.requiresAllergenCheck) {
onChoose(item.product);
return;
}
onOpenProduct(item.product);
}}
onOpen={() => onOpenProduct(item.product)}
/>
))}
</ScreenContainer>
);
}
const styles = StyleSheet.create({
note: {
color: colors.muted,
fontSize: 14
},
empty: {
backgroundColor: colors.surface,
borderColor: colors.border,
borderRadius: 18,
borderWidth: 1,
padding: 16
},
emptyTitle: {
color: colors.text,
fontSize: 16,
fontWeight: '800'
},
emptyCopy: {
color: colors.muted,
fontSize: 14,
lineHeight: 20,
marginTop: 4
}
});