import React, { useState } from 'react'; import { View, Text, StyleSheet, ScrollView, RefreshControl, TouchableOpacity, Image, ActivityIndicator } from 'react-native'; import { getFavorites, toggleFavorite } from '../api'; import { useFocusEffect } from '@react-navigation/native'; import { ChevronLeftIcon, HeartIcon } from 'react-native-heroicons/outline'; import { StarIcon } from 'react-native-heroicons/solid'; import Icon from 'react-native-vector-icons/FontAwesome'; import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen'; import TabBar from '../themes/TabBar'; const WishlistScreen = ({ navigation }) => { const [favorites, setFavorites] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [activeCategory, setActiveCategory] = useState("All"); const categories = ["All", "Shirt", "Paint", "T-shirt", "Jacket", "Suite", "Shoes"]; const loadFavorites = async () => { try { const data = await getFavorites(); setFavorites(data || []); } catch (error) { console.error('Error loading favorites:', error); setFavorites([]); } finally { setLoading(false); setRefreshing(false); } }; useFocusEffect( React.useCallback(() => { loadFavorites(); }, []) ); const onRefresh = React.useCallback(() => { setRefreshing(true); loadFavorites(); }, []); const handleFavoriteToggle = async (product) => { try { await toggleFavorite(product); // Reload favorites after toggling loadFavorites(); } catch (error) { console.error('Error toggling favorite:', error); } }; // Ensure favorites is always an array const safeFavorites = Array.isArray(favorites) ? favorites : []; const filteredFavorites = activeCategory === "All" ? safeFavorites : safeFavorites.filter(item => item.product && item.product.category_name === activeCategory); if (loading && !refreshing) { return ( ); } return ( } > } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> {categories.map((category, index) => { const isActive = category === activeCategory; return ( setActiveCategory(category)} style={[ styles.categoryButton, isActive && styles.activeCategoryButton ]} > {category} ); })} {filteredFavorites.length === 0 ? ( No items in your wishlist ) : ( {filteredFavorites.map((item, index) => ( navigation.navigate('ProductDetails', { id: item.product.id })} style={styles.card} > handleFavoriteToggle(item.product)} > {item.product.name} Rs {item.product.price} {item.product.average_rating || 0} ))} )} ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, categoriesContainer: { paddingVertical: 10, }, categoryButton: { borderWidth: 1, borderColor: '#666', paddingHorizontal: 16, paddingVertical: 8, borderRadius: 20, marginLeft: 10, }, activeCategoryButton: { backgroundColor: '#007AFF', borderColor: '#007AFF', }, categoryText: { fontWeight: 'bold', }, activeCategoryText: { color: '#fff', }, emptyContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', paddingTop: 50, }, emptyText: { fontSize: 16, color: '#666', }, gridContainer: { flexDirection: 'row', flexWrap: 'wrap', padding: 10, }, cardContainer: { width: '50%', padding: 5, }, leftCard: { paddingRight: 2.5, }, rightCard: { paddingLeft: 2.5, }, card: { backgroundColor: '#fff', borderRadius: 10, elevation: 2, overflow: 'hidden', }, imageContainer: { position: 'relative', }, image: { width: '100%', height: hp(20), resizeMode: 'cover', }, heartButton: { position: 'absolute', top: 10, right: 10, backgroundColor: 'rgba(255, 255, 255, 0.5)', padding: 8, borderRadius: 20, }, detailsContainer: { padding: 10, }, title: { fontSize: 14, fontWeight: 'bold', marginBottom: 5, }, priceRatingContainer: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, price: { fontSize: 14, fontWeight: 'bold', color: '#007AFF', }, ratingContainer: { flexDirection: 'row', alignItems: 'center', }, rating: { fontSize: 12, marginLeft: 4, color: '#666', }, }); export default WishlistScreen;