import { View, Text, ScrollView, TouchableOpacity, Image, RefreshControl, ActivityIndicator } from 'react-native' import React, { useState, useEffect, useCallback } from 'react' import { 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 { useNavigation, useFocusEffect } from '@react-navigation/native' import { StarIcon } from 'react-native-heroicons/solid' import { getFavorites, toggleFavorite, fetchProducts } from '../api' import TabBar from '../themes/TabBar' export default function FlashSalesScreen() { const navigation = useNavigation(); const [favorites, setFavorites] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [flashSales, setFlashSales] = useState([]); const [error, setError] = useState(null); const loadFlashSales = useCallback(async () => { try { // Fetch products from API with a 'flash_sale' filter if your API supports it // This is an example - modify this to match your actual API const products = await fetchProducts('flash_sale'); setFlashSales(products); setError(null); } catch (error) { console.error('Error loading flash sales:', error); setError('Failed to load flash sales. Please try again.'); } }, []); const loadFavorites = useCallback(async () => { try { const favoriteProducts = await getFavorites(); setFavorites(favoriteProducts); } catch (error) { console.error('Error loading favorites:', error); } }, []); const loadData = useCallback(async () => { setLoading(true); try { await Promise.all([loadFlashSales(), loadFavorites()]); } catch (error) { console.error('Error loading data:', error); } finally { setLoading(false); setRefreshing(false); } }, [loadFlashSales, loadFavorites]); // Load data when component mounts and when screen is focused useFocusEffect( useCallback(() => { loadData(); }, [loadData]) ); const onRefresh = useCallback(() => { setRefreshing(true); loadData(); }, [loadData]); const handleFavouriteToggle = async (product) => { try { const updatedFavorites = await toggleFavorite(product); setFavorites(updatedFavorites); } catch (error) { console.error('Error toggling favorite:', error); } }; const isProductFavorited = (productId) => { return favorites.some(fav => fav.id === productId); }; const renderItem = (item, index) => { const isFavorite = isProductFavorited(item.id); return ( navigation.navigate('ProductDetails', { productId: item.id })} > { e.stopPropagation(); handleFavouriteToggle(item); }} className={`absolute top-3 right-3 z-10 bg-white rounded-full p-1`} > {isFavorite ? ( ) : ( )} {item.name || item.title} {item.rating || 0} Rs {item.price} ); }; if (loading && !refreshing) { return ( Loading... ); } return ( } > {error ? ( {error} Try Again ) : flashSales.length === 0 && !loading ? ( No flash sales available at the moment. ) : ( {flashSales.map((item, index) => renderItem(item, index))} )} ); }