import { View, Text, ScrollView, Image, TextInput, TouchableOpacity, Pressable, ActivityIndicator, Alert, RefreshControl, KeyboardAvoidingView, Platform } from 'react-native' import React, { useEffect, useState, useCallback } from 'react' import TabBar from '../themes/TabBar' import { ChevronLeftIcon, HeartIcon, MinusIcon, PlusIcon, XMarkIcon } from 'react-native-heroicons/outline' import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen'; import { useNavigation, useFocusEffect } from '@react-navigation/native' import { deleteCartItem, getCart, updateCartItemQuantity, processImageUrl } from '../api' import EvIcons from "react-native-vector-icons/EvilIcons" import AsyncStorage from '@react-native-async-storage/async-storage'; import { useCart } from '../context/CartContext'; export default function MyCartScreen() { const navigation = useNavigation(); const [cartItems, setCartItems] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [refreshing, setRefreshing] = useState(false); const [promoCode, setPromoCode] = useState(''); const [isAuthenticated, setIsAuthenticated] = useState(false); const { cartTotal, appliedCoupon, couponDiscount, applyCoupon, removeCoupon, refreshCart } = useCart(); // Check authentication status const checkAuth = async () => { try { const token = await AsyncStorage.getItem('userToken'); const isAuth = !!token; setIsAuthenticated(isAuth); if (!isAuth) { setError('Please log in to view your cart'); setLoading(false); setRefreshing(false); return false; } return true; } catch (error) { console.error('Error checking authentication:', error); return false; } }; // Process cart data to ensure all items have the required properties const processCartData = async (cartData) => { if (!cartData || !cartData.items) { return { items: [], total: 0 }; } // Process each item to ensure all required properties exist const processedItems = await Promise.all(cartData.items.map(async item => { // Process the image URL let processedImageUrl = null; if (item.product_image) { try { console.log('Original image URL:', item.product_image); // If the URL is already absolute, use it as is if (item.product_image.startsWith('http')) { processedImageUrl = item.product_image; } else { // Otherwise, process it through the API processedImageUrl = await processImageUrl(item.product_image); } console.log('Processed image URL:', processedImageUrl); } catch (error) { console.error('Error processing image URL:', error); } } // Get the price from the correct field const price = typeof item.product_price === 'number' ? item.product_price : typeof item.price === 'number' ? item.price : parseFloat(item.product_price || item.price || 0); // Calculate subtotal based on price and quantity const quantity = parseInt(item.quantity || 1); const subtotal = price * quantity; // Ensure all required properties exist return { id: item.id, product_id: item.product, product_name: item.product_name, product_image: processedImageUrl, price: price, quantity: quantity, subtotal: subtotal }; })); // Calculate total from processed items const total = processedItems.reduce((sum, item) => sum + item.subtotal, 0); return { items: processedItems, total: total }; }; const fetchCartData = async () => { try { setLoading(true); setError(null); const data = await getCart(); setCartItems(data.items || []); } catch (error) { console.error('Error fetching cart:', error); if (error.message === 'Session expired. Please login again.') { Alert.alert( 'Session Expired', 'Your session has expired. Please login again.', [ { text: 'OK', onPress: () => navigation.navigate('Login') } ] ); } else { setError('Failed to load cart items. Please try again.'); } } finally { setLoading(false); setRefreshing(false); } }; const handleUpdateQuantity = async (itemId, newQuantity) => { if (newQuantity < 1) return; // Find the cart item to get the product ID const cartItem = cartItems.find(item => item.id === itemId); if (!cartItem) { Alert.alert('Error', 'Cart item not found'); return; } // Optimistic update setCartItems(prevItems => prevItems.map(item => item.id === itemId ? { ...item, quantity: newQuantity } : item ) ); try { await updateCartItemQuantity(cartItem.product, newQuantity); // Refresh cart data in background fetchCartData(); } catch (error) { console.error('Error updating quantity:', error); // Revert optimistic update on error fetchCartData(); // Handle different types of errors if (error.message === 'Session expired. Please login again.') { Alert.alert( 'Session Expired', 'Your session has expired. Please login again.', [ { text: 'OK', onPress: () => navigation.navigate('SignIn') } ] ); } else if (error.message === 'You do not have permission to update this cart item.') { Alert.alert( 'Permission Error', 'You do not have permission to update this cart item. Please try again later.' ); } else if (error.message === 'Cart item not found') { Alert.alert( 'Error', 'This item is no longer in your cart. The cart will be refreshed.' ); fetchCartData(); // Refresh the cart to get the current state } else { Alert.alert( 'Error', 'Failed to update quantity. Please try again.' ); } } }; const handleDeleteItem = async (itemId) => { try { await deleteCartItem(itemId); // Optimistic update setCartItems(prevItems => prevItems.filter(item => item.id !== itemId)); // Refresh cart data in background fetchCartData(); } catch (error) { console.error('Error deleting item:', error); // Revert optimistic update on error fetchCartData(); } }; const handleCheckout = () => { if (cartItems.length === 0) { Alert.alert('Empty Cart', 'Your cart is empty. Add some items first.'); return; } navigation.navigate('Checkout'); }; const onRefresh = () => { setRefreshing(true); fetchCartData(); }; useEffect(() => { const unsubscribe = navigation.addListener('focus', () => { fetchCartData(); }); return unsubscribe; }, [navigation]); const handleApplyPromoCode = async () => { if (!promoCode.trim()) { Alert.alert('Error', 'Please enter a promo code'); return; } try { setLoading(true); const result = await applyCoupon(promoCode); if (result.success) { Alert.alert('Success', `Coupon applied! You saved ${result.discount.toFixed(2)}`); setPromoCode(''); } else { // Display the specific error message from the server Alert.alert('Invalid Coupon', result.message || 'This coupon cannot be applied to your cart.'); } } catch (error) { console.error('Error applying promo code:', error); // Check if the error has a response with a message const errorMessage = error.response?.data?.message || error.message || 'Failed to apply promo code'; Alert.alert('Error', errorMessage); } finally { setLoading(false); } }; const handleRemoveCoupon = async () => { try { setLoading(true); // Remove the coupon from context removeCoupon(); // Clear the promo code input setPromoCode(''); // Fetch fresh cart data and wait for it to complete await fetchCartData(); // Force a refresh of the cart context refreshCart(); setLoading(false); Alert.alert('Success', 'Coupon removed successfully'); } catch (error) { console.error('Error removing coupon:', error); setLoading(false); Alert.alert('Error', 'Failed to remove coupon. Please try again.'); } }; // Error banner component for non-blocking errors const ErrorBanner = ({ message, onDismiss }) => ( {message} Dismiss ); // Move CartItem outside of the main component and memoize it const CartItem = React.memo(({ item, onUpdateQuantity, onDelete }) => { // Calculate the price, ensuring it's a number const price = typeof item.price === 'number' ? item.price : typeof item.product_price === 'number' ? item.product_price : parseFloat(item.price || item.product_price || 0); return ( {item.product_name} Rs {price.toFixed(2)} onUpdateQuantity(item.id, item.quantity - 1)} style={{ backgroundColor: '#e5e7eb', padding: wp('2%'), borderRadius: 9999 }} > {item.quantity} onUpdateQuantity(item.id, item.quantity + 1)} style={{ backgroundColor: '#e5e7eb', padding: wp('2%'), borderRadius: 9999 }} > onDelete(item.id)} style={{ marginLeft: 'auto' }} > ); }, (prevProps, nextProps) => { // Only re-render if these props change return ( prevProps.item.id === nextProps.item.id && prevProps.item.quantity === nextProps.item.quantity && prevProps.item.product_image === nextProps.item.product_image ); }); if (loading && !refreshing) { return ( ); } if (error && cartItems.length === 0) { return ( {error} Retry {!isAuthenticated && ( navigation.navigate('SignIn')} className="mt-4 bg-primary py-2 px-4 rounded-lg" > Sign In )} ); } const deliveryFee = cartItems.length > 0 ? 10 : 0; const discountPercentage = 6; const subtotal = cartItems.reduce((total, item) => { const itemSubtotal = parseFloat(item.subtotal || 0); return total + (isNaN(itemSubtotal) ? 0 : itemSubtotal); }, 0); const discount = (subtotal * discountPercentage) / 100; const totalCost = subtotal + deliveryFee - discount - couponDiscount; return ( } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> {/* Error banner for non-blocking errors */} {error && cartItems.length > 0 && ( setError(null)} /> )} {!cartItems || cartItems.length === 0 ? ( Your cart is empty { // Navigate to Main (BottomTabBar) and then to Home tab navigation.goBack(); }} style={{ marginTop: hp('2%'), backgroundColor: '#704f38', paddingVertical: hp('1%'), paddingHorizontal: wp('4%'), borderRadius: wp('2%') }} > Continue Shopping ) : ( <> } className="flex-1" > {cartItems.map((item, index) => ( ))} Promo Code Apply {appliedCoupon && ( Coupon "{appliedCoupon.code}" applied! You saved {couponDiscount.toFixed(2)} Remove Coupon )} Order Summary Subtotal Rs {subtotal.toFixed(2)} Delivery Fee Rs {deliveryFee.toFixed(2)} Discount - Rs {discount.toFixed(2)} {couponDiscount > 0 && ( Coupon Discount - Rs {couponDiscount.toFixed(2)} )} Total Rs {totalCost.toFixed(2)} Proceed to Checkout )} ); }