import { View, Text, Image, ActivityIndicator } from 'react-native' import React, { useState, useEffect } from 'react' import TabBar from '../themes/TabBar' import { ChevronLeftIcon, HeartIcon } from 'react-native-heroicons/outline' import { HeartIcon as SolidHeartIcon } 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 { TouchableOpacity } from 'react-native'; import { StarIcon } from 'react-native-heroicons/solid'; import Icon from 'react-native-vector-icons/FontAwesome' import { ScrollView } from 'react-native'; import { useNavigation, useRoute } from '@react-navigation/native'; import { fetchProductsByCategory } from '../api'; export default function JacketScreen() { const route = useRoute(); const { title, categoryId } = route.params; const navigation = useNavigation(); const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [favorites, setFavorites] = useState({}); const toggleFavorite = (productId) => { setFavorites(prev => ({ ...prev, [productId]: !prev[productId] })); }; useEffect(() => { const getProducts = async () => { try { setLoading(true); setError(null); // Validate categoryId if (!categoryId) { throw new Error('Category ID is required'); } // Ensure categoryId is a number const parsedCategoryId = parseInt(categoryId); if (isNaN(parsedCategoryId)) { throw new Error('Category ID must be a valid number'); } console.log('Fetching products for category:', parsedCategoryId); const data = await fetchProductsByCategory(parsedCategoryId); // Validate response data if (!data) { throw new Error('No data received from server'); } if (!Array.isArray(data)) { throw new Error('Invalid response format from server'); } setProducts(data); } catch (error) { console.error('Error fetching products:', error); setError(error.message || 'Failed to fetch products'); setProducts([]); } finally { setLoading(false); } }; getProducts(); }, [categoryId]); if (loading) { return ( Loading products... ); } if (error) { return ( {error} navigation.goBack()} className="mt-4 bg-blue-500 px-4 py-2 rounded-lg" > Go Back ); } return ( navigation.goBack()}> {title} {products.length === 0 ? ( No products found in this category ) : ( item.id.toString()} numColumns={2} showsVerticalScrollIndicator={false} renderItem={({ item }) => ( navigation.navigate('Product', { product: item })} > {item.name} ${item.price} {item.average_rating || 0} toggleFavorite(item.id)} > {favorites[item.id] ? ( ) : ( )} )} /> )} ); }