import { View, Text, ScrollView, TextInput, TouchableOpacity, Image, ActivityIndicator, Dimensions } from 'react-native' import React, { useState, useEffect } from 'react' import TabBar from '../themes/TabBar' import { ChevronLeftIcon, HeartIcon, MagnifyingGlassIcon } from 'react-native-heroicons/outline' import { HeartIcon as SolidHeartIcon, StarIcon } from 'react-native-heroicons/solid' import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen'; import MasonryList from '@react-native-seoul/masonry-list'; import { useNavigation, useRoute } from '@react-navigation/native'; import { getCurrentUser, isProductFavorited, toggleFavorite, getFavorites } from '../api'; import { Ionicons } from '@expo/vector-icons'; import COLORS from '../constants/clothColor'; import Icon from "react-native-vector-icons/FontAwesome"; const { width } = Dimensions.get('window'); const SearchResultScreen = () => { const route = useRoute(); const navigation = useNavigation(); const { products, searchTerm } = route.params; const [user, setUser] = useState(null); const [favorites, setFavorites] = useState([]); const [productList, setProductList] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { loadCurrentUser(); setProductList(products || []); loadFavorites(); }, [products]); const loadCurrentUser = async () => { const currentUser = await getCurrentUser(); setUser(currentUser); }; const loadFavorites = async () => { try { setLoading(true); const favoriteProducts = await getFavorites(); setFavorites(favoriteProducts || []); } catch (error) { console.error('Error loading favorites:', error); setFavorites([]); } finally { setLoading(false); } }; const handleToggleFavorite = async (product) => { try { const updatedFavorites = await toggleFavorite(product); setFavorites(updatedFavorites || []); } catch (error) { console.error('Error toggling favorite:', error); } }; const isProductFavorited = (productId) => { if (!Array.isArray(favorites)) return false; return favorites.some(fav => fav.id === productId); }; const getRating = (item) => { if (item.rating) { if (typeof item.rating === 'object' && item.rating.rate !== undefined) { return item.rating.rate; } if (typeof item.rating === 'number') { return item.rating; } } if (item.average_rating !== undefined) { return item.average_rating; } return 0; }; const renderItem = ({ item, i }) => { if (!item) return null; const isFavorite = isProductFavorited(item.id); const isEven = item.id % 2 === 0; return ( navigation.navigate('ProductDetails', { id: item.id })} className="relative" > { e.stopPropagation(); handleToggleFavorite(item); }} className="absolute right-4 top-4 bg-white/50 p-2 rounded-full" > {isFavorite ? ( ) : ( )} {item.name || item.title} {getRating(item)} Rs {item.price || 0} ); }; return ( } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> Result for "{searchTerm}" {productList?.length || 0} founds {loading ? ( ) : !productList || productList.length === 0 ? ( No products found Try a different search term ) : ( item?.id?.toString() || Math.random().toString()} numColumns={2} showsVerticalScrollIndicator={false} renderItem={renderItem} contentContainerStyle={styles.listContent} /> )} ) } const styles = { loader: { flex: 1, justifyContent: 'center', alignItems: 'center', }, emptyContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20, }, emptyText: { fontSize: 18, fontWeight: 'bold', marginBottom: 10, }, emptySubtext: { fontSize: 14, color: COLORS.grey, }, listContent: { padding: 5, }, } export default SearchResultScreen