// app/(auth)/(tabs)/(home)/optionPage.tsx import { View, Text, ScrollView, Pressable, Image, Dimensions, ImageBackground, BackHandler, Alert } from 'react-native'; import React, { useCallback, useEffect, useState } from 'react'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useFocusEffect, useNavigation } from 'expo-router'; import { PreviousPageBlackSvg } from '../../../../component/global/SVG'; import { useChargingStore } from '../../../../providers/scan_qr_payload_store'; import { walletService } from '../../../../service/walletService'; import { chargeStationService } from '../../../../service/chargeStationService'; import { useTranslation } from '../../../../util/hooks/useTranslation'; const optionPage = () => { const { t } = useTranslation(); const { total_power, setTotalPower, promotion_code, setPromotionCode, sum_of_coupon, setSumOfCoupon, setCouponDetail, current_price_store, setCurrentPriceStore, setProcessedCouponStore } = useChargingStore(); const buttonText = [ { power: t('chargingOption.option.kwh_20'), minute: t('chargingOption.option.min_25'), total_power: 20 }, { power: t('chargingOption.option.kwh_25'), minute: t('chargingOption.option.min_30'), total_power: 25 }, { power: t('chargingOption.option.kwh_30'), minute: t('chargingOption.option.min_40'), total_power: 30 }, { power: t('chargingOption.option.kwh_40'), minute: t('chargingOption.option.min_45'), total_power: 40 }, { power: t('chargingOption.option.full_charge'), minute: t('chargingOption.option.up_to_80kwh'), total_power: 80 } ]; const screenWidth = Dimensions.get('window').width; const [useableCoupon, setUseableCoupon] = useState([]); const navigation = useNavigation(); //forbid user leftswipping useEffect(() => { navigation.setOptions({ gestureEnabled: false }); }, [navigation]); // 优惠券注释 useEffect(() => { const fetchData = async () => { try { const info = await walletService.getCustomerInfo(); const coupon = await walletService.getCouponForSpecificUser(info.id); //filter by expire date is not past today.and is_consumed is fa.se. const useableConpon = coupon.filter((couponObj: any) => { const today = new Date(); if (couponObj.expire_date === null) { return couponObj.is_consumed === false; } const expireDate = new Date(couponObj.expire_date); return expireDate > today && couponObj.is_consumed === false; }); setUseableCoupon(useableConpon); } catch (error) {} }; fetchData(); }, []); const cleanupData = () => { setTotalPower(null); setSumOfCoupon(0); setPromotionCode([]); setCouponDetail([]); setProcessedCouponStore([]); }; // 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(); }, []) ); const displayedText = promotion_code.length > 0 ? t('chargingOption.option.coupon_value', { value: sum_of_coupon }) : useableCoupon.length > 0 ? t('chargingOption.option.available_coupon') : t('chargingOption.option.no_coupon'); return ( { if (router.canGoBack()) { cleanupData(); router.back(); } else { cleanupData(); router.replace('/scanQrPage'); } }} > {/* top word and logo */} {t('chargingOption.option.select_charge_amount')} {/* middle button */} {/* not showing full power because it needs to be full width */} {buttonText .filter((item) => item.power != t('chargingOption.option.full_charge')) .map((buttonTextObj: any, index) => ( { setTotalPower(buttonTextObj.total_power); }} > {buttonTextObj.power}({buttonTextObj.minute}) ))} {/* this is 充滿停機only */} {buttonText .filter((item) => item.power === t('chargingOption.option.full_charge')) .map((item, index) => ( { setTotalPower(item.total_power); }} className={`${ total_power === item.total_power ? 'bg-[#02677D]' : '' } w-[94%] border border-[#02677D] p-1 md:p-2 lg:p-3 xl:p-4 py-4 lg:py-6 rounded-xl`} > {item.power}({item.minute}) ))} {/* coupon row */} { if (!total_power) { Alert.alert( t('chargingOption.option.alert.select_plan_title'), t('chargingOption.option.alert.select_plan_message'), [{ text: t('common.confirm'), style: 'default' }], { cancelable: true } ); } else { Alert.alert( t('chargingOption.option.alert.reminder_title'), t('chargingOption.option.alert.reminder_message'), [ { text: t('common.confirm'), onPress: () => router.push('/selectCoupon') } ]); } }} style={{ flex: 1, borderRadius: 10, overflow: 'hidden', marginTop: 10 }} > {displayedText} {/* confirm and cancel */} { cleanupData(); if (router.canGoBack()) { router.back(); } else { router.replace('/scanQrPage'); } }} className="w-[45%] border border-[#02677D] p-1 md:p-2 lg:p-3 xl:p-4 py-4 lg:py-6 rounded-xl " > {t('common.cancel')} { if (!total_power) { Alert.alert( t('chargingOption.option.alert.select_plan_title'), t('chargingOption.option.alert.confirm_plan_message'), [{ text: t('common.confirm'), style: 'default' }], { cancelable: true } ); } else { router.push('/totalPayment'); } }} className="w-[45%] border border-[#02677D] p-1 md:p-2 lg:p-3 xl:p-4 py-4 lg:py-6 rounded-xl " > {t('common.confirm')} ); }; export default optionPage;