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 { 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);
const data = await fetchProductsByCategory(categoryId);
setProducts(data);
setError(null);
} catch (error) {
console.error('Error fetching products by category:', error);
setError(error.message || 'Failed to fetch products');
} finally {
setLoading(false);
}
};
getProducts();
}, [categoryId]);
if (loading) {
return (
);
}
if (error) {
return (
{error}
{
setLoading(true);
fetchProductsByCategory(categoryId)
.then(data => {
setProducts(data);
setError(null);
})
.catch(err => setError(err.message || 'Failed to fetch products'))
.finally(() => setLoading(false));
}}
className="mt-4 bg-primary py-2 px-4 rounded-lg"
>
Retry
);
}
return (
}
titleStyle={{
color: "black",
fontWeight: "bold",
fontSize: 20
}}
prefixStyle={{
backgroundColor: "transparent"
}}
suffixStyle={{
backgroundColor: "white",
borderWidth: 1,
borderColor: "black"
}}
containerStyle=""
suffixAction={() => navigation.goBack()}
/>
{products.length === 0 ? (
No products found in this category
) : (
item.id.toString()}
numColumns={2}
showsVerticalScrollIndicator={false}
renderItem={({ item, index }) => (
)}
className="mx-4"
/>
)}
)
}
const CardItem = ({ item, index, toggleFavorite, isFavorite, navigation }) => {
let isEven = index % 2 === 0;
return (
{
navigation.navigate('ProductDetails', { id: item.id })
}}
className="relative"
>
toggleFavorite(item.id)}
className="absolute right-4 top-4 bg-white/50 p-2 rounded-full"
>
{isFavorite ? (
) : (
)}
{item.name}
{item.average_rating || 0}
Rs {item.price}
)
}