import React, { useState, useCallback } from 'react'; import { View, Text, FlatList, TouchableOpacity, StatusBar, ToastAndroid, Platform, } from 'react-native'; import { useFocusEffect } from '@react-navigation/native'; import { getStyles } from './offersScreen.styles'; import { Header } from '@components'; import { useAppTheme } from '@theme'; import { useAppDispatch, useAppSelector, getAllOffersThunk } from '@store'; import { Promotion, PromotionType } from '@interfaces'; import Clipboard from '@react-native-clipboard/clipboard'; const getPromoVisuals = (type: PromotionType) => { switch (type) { case 'FREE_DELIVERY': return { emoji: '🛵', bgColor: '#E8F5E9', badgeBg: '#C8E6C9', textColor: '#1B5E20', }; case 'PERCENTAGE_DISCOUNT': return { emoji: '🏷️', bgColor: '#FFF3E0', badgeBg: '#FFE0B2', textColor: '#E65100', }; case 'FLAT_DISCOUNT': return { emoji: '💰', bgColor: '#E3F2FD', badgeBg: '#BBDEFB', textColor: '#0D47A1', }; case 'BUY_ONE_GET_ONE': return { emoji: '🎁', bgColor: '#F3E5F5', badgeBg: '#E1BEE7', textColor: '#4A148C', }; default: return { emoji: '🎉', bgColor: '#F5F5F5', badgeBg: '#E0E0E0', textColor: '#333333', }; } }; export const OffersScreen: React.FC = () => { const { colors, isDarkMode } = useAppTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const { offers, isLoading } = useAppSelector(state => state.offer); const [copiedId, setCopiedId] = useState(null); useFocusEffect( useCallback(() => { dispatch(getAllOffersThunk()); }, [dispatch]), ); const handleCopy = (item: Promotion) => { Clipboard.setString(item.badgeText); setCopiedId(item.id); // const textToCopy = item.badgeText || item.title; // if (Platform.OS === 'android') { // ToastAndroid.show(`Copied "${textToCopy}"`, ToastAndroid.SHORT); // } setTimeout(() => { setCopiedId(prev => (prev === item.id ? null : prev)); }, 2000); }; const renderOfferCard = ({ item }: { item: Promotion }) => { const visuals = getPromoVisuals(item.promotionType); const isCopied = copiedId === item.id; const hasFooter = (item.minimumOrderAmount && item.minimumOrderAmount > 0) || (item.maximumDiscount && item.maximumDiscount > 0) || (item.applicableCategories && item.applicableCategories.length > 0) || (item.applicableProducts && item.applicableProducts.length > 0); return ( {/* Main Card Header */} {visuals.emoji} {!!item.badgeText && ( {item.badgeText} )} {item.title} {!!item.description && ( {item.description} )} handleCopy(item)} // onPress={() => { }} activeOpacity={0.8} > {isCopied ? 'Copied ✓' : 'Copy'} {/* Dynamic Meta Details Footer (Only if real data exists) */} {hasFooter && ( {!!item.minimumOrderAmount && item.minimumOrderAmount > 0 && ( Min order ₹{item.minimumOrderAmount} )} {!!item.maximumDiscount && item.maximumDiscount > 0 && ( Max discount ₹{item.maximumDiscount} )} {!!item.applicableCategories?.length && ( {item.applicableCategories.length}{' '} {item.applicableCategories.length === 1 ? 'category' : 'categories'} )} {!!item.applicableProducts?.length && ( {item.applicableProducts.length}{' '} {item.applicableProducts.length === 1 ? 'item' : 'items'} )} )} ); }; const renderSkeleton = () => ( {[1, 2, 3].map(key => ( ))} ); const renderEmpty = () => ( 🏷️ No Offers Available There are currently no active promotions.{'\n'}Check back later for exciting discounts and deals! ); return (
{isLoading && (!offers || offers.length === 0) ? ( renderSkeleton() ) : ( item.id} renderItem={renderOfferCard} ListEmptyComponent={renderEmpty} contentContainerStyle={styles.list} showsVerticalScrollIndicator={false} /> )} ); };