import { View, Text, ScrollView, TouchableOpacity, Alert } from 'react-native' import React, { useState, useEffect } from 'react' import { widthPercentageToDP as wp, heightPercentageToDP as hp, heightPercentageToDP } from 'react-native-responsive-screen'; import TabBar from '../themes/TabBar'; import { ChevronLeftIcon } from 'react-native-heroicons/outline'; import Icon from "react-native-vector-icons/MaterialCommunityIcons" import { getCoupons, validateCoupon } from '../api'; import { useNavigation } from '@react-navigation/native'; import { useCart } from '../context/CartContext'; import { useAuth } from '../context/AuthContext'; export default function CouponScreen() { const [coupons, setCoupons] = useState([]); const [loading, setLoading] = useState(true); const navigation = useNavigation(); const { cartTotal } = useCart(); const { isAuthenticated } = useAuth(); useEffect(() => { fetchCoupons(); }, []); const fetchCoupons = async () => { try { setLoading(true); const data = await getCoupons(); setCoupons(data); } catch (error) { console.error('Error fetching coupons:', error); Alert.alert('Error', 'Failed to load coupons. Please try again later.'); } finally { setLoading(false); } }; const handleCopyCode = async (coupon) => { try { if (!isAuthenticated) { Alert.alert('Login Required', 'Please login to use coupons'); navigation.navigate('Login'); return; } const result = await validateCoupon(coupon.code, cartTotal); if (result.valid) { // Copy the code to clipboard // You might want to use Clipboard API here Alert.alert('Success', `Coupon code ${coupon.code} copied to clipboard!`); } else { Alert.alert('Invalid Coupon', result.message || 'This coupon cannot be applied to your cart.'); } } catch (error) { console.error('Error validating coupon:', error); Alert.alert('Error', 'Failed to validate coupon. Please try again later.'); } }; return ( } titleStyle={{ color: "black", fontWeight: "bold", fontSize: 20 }} prefixStyle={{ backgroundColor: "transparent" }} suffixStyle={{ backgroundColor: "white", borderWidth: 1, borderColor: "black" }} containerStyle="" suffixAction={() => navigation.goBack()} /> Best offers for you {loading ? ( Loading coupons... ) : coupons.length > 0 ? ( coupons.map((item, index) => { return ( {item.code} Add items worth ${item.min_purchase} more to unlock {item.discount_type === 'percentage' ? `${item.discount_value}% OFF` : `$${item.discount_value} OFF`} handleCopyCode(item)} > COPY CODE {/* this line of code make the semicircle i both side of container */} ) }) ) : ( No coupons available at the moment. )} ) }