// app/(auth)/(tabs)/(home)/selectCoupon.tsx import { Image, View, Text, Pressable, Dimensions, StyleSheet, Modal, Animated, ScrollView, BackHandler } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useFocusEffect } from 'expo-router'; import { CrossLogoSvg } from '../../../../component/global/SVG'; import CouponTabViewComponent from '../../../../component/global/couponTabView'; import { useCallback, useEffect, useState } from 'react'; import { useChargingStore } from '../../../../providers/scan_qr_payload_store'; import { ArrowRightSvg } from '../../../../component/global/SVG'; import { useRef } from 'react'; import NormalButton from '../../../../component/global/normal_button'; import { useTranslation } from '../../../../util/hooks/useTranslation'; //this is from optionPage => 優惠券 const SelectCouponComponent = () => { const { t } = useTranslation(); const screenHeight = Dimensions.get('window').height; const { promotion_code, coupon_detail, stationID, setSumOfCoupon, setCurrentPriceStore, current_price_store, setProcessedCouponStore, setPromotionCode, setCouponDetail, setTotalPower } = useChargingStore(); const [scaleValue, setScaleValue] = useState(1); const [isBottomSheetVisible, setIsBottomSheetVisible] = useState(false); const [processedCoupons, setProcessedCoupons] = useState([]); const translateY = useRef(new Animated.Value(300)).current; const showBottomSheet = () => { setIsBottomSheetVisible(true); Animated.timing(translateY, { toValue: 0, duration: 300, useNativeDriver: true }).start(); }; const hideBottomSheet = () => { Animated.timing(translateY, { toValue: 0, duration: 0, useNativeDriver: true }).start(() => setIsBottomSheetVisible(false)); }; //process coupon so that coupons with same expire date and amount are grouped together to show abcoupon x 2 useEffect(() => { if (Array.isArray(coupon_detail) && coupon_detail.length > 0) { const processed = processCoupons(coupon_detail); setProcessedCoupons(processed); setProcessedCouponStore(processed); } }, [coupon_detail]); //fetch original price for coupon valid calculation const processCoupons = (coupon_details_array: any) => { //coupon_details_array contains all information. i skim it down here. if (!coupon_details_array || coupon_details_array.length === 0) { return []; } const skimmedDownArray = coupon_details_array?.map((couponDetailObj: any) => ({ amount: couponDetailObj.coupon.amount, id: couponDetailObj.id, expire_date: couponDetailObj.expire_date || t('common.permanent') })); const totalCouponAmount = skimmedDownArray.reduce((acc: number, coupon: any) => acc + coupon.amount, 0); setSumOfCoupon(totalCouponAmount); //process the skimmed array by combining coupons with same expire_date and amount const processedArray = (skimmedDownArray: { amount: number; id: string; expire_date: string }[]) => { const groupedCoupons: { [key: string]: any } = {}; for (const coupon of skimmedDownArray) { const key = `${coupon.amount}-${coupon.expire_date}`; if (!groupedCoupons[key]) { groupedCoupons[key] = { coupon_detail: { amount: coupon.amount, expire_date: coupon.expire_date }, frequency: 1 }; } else { groupedCoupons[key].frequency++; } } return Object.values(groupedCoupons); }; return processedArray(skimmedDownArray); }; const cleanupData = () => { setPromotionCode([]); setCouponDetail([]); setProcessedCouponStore([]); setSumOfCoupon(0); setTotalPower(null); }; // Add this effect to handle Android back button useFocusEffect( useCallback(() => { const onBackPress = () => { cleanupData(); if (router.canGoBack()) { router.back(); } else { router.replace('/scanQrPage'); } return true; }; const subscription = BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => subscription.remove() }, []) ); return ( { cleanupData(); if (router.canGoBack()) { router.back(); } else { router.replace('/optionPage'); } }} > {t('selectCoupons.coupons.title')} {promotion_code.length > 0 && ( setScaleValue(0.96)} onPressOut={() => setScaleValue(1)} onPress={showBottomSheet} > {t('common.use_now')} {promotion_code.length} )} {t('selectCoupons.coupon_details')} {/* coupon row */} {processedCoupons && processedCoupons?.map((couponObj: any) => ( ${couponObj.coupon_detail.amount} {t('selectCoupons.coupons.cash_voucher')} {t('selectCoupons.coupons.valid_until')}{' '} {couponObj.coupon_detail.expire_date === t('common.permanent') ? t('common.permanent') : t('common.to_date', { date: couponObj.coupon_detail.expire_date.slice(0, 10) })} {/* x 1 */} X {couponObj.frequency} ))} {/* 服務條款 */} {t('common.use_now')}} onPress={() => { setIsBottomSheetVisible(false); router.push('/totalPayment'); }} extendedStyle={{ marginTop: 12, marginBottom: 24 }} /> {t('selectCoupons.coupon.terms_and_conditions')} {t('selectCoupons.coupon.term1')} {t('selectCoupons.coupon.term2')} {t('selectCoupons.coupon.term3')} {t('selectCoupons.coupon.term4')} {t('selectCoupons.coupon.term5')} {t('selectCoupons.coupon.term6')} ); }; const styles = StyleSheet.create({ floatingButton: { elevation: 5, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84 } }); export default SelectCouponComponent;