//the size of the TabView will follow its parent-container's size. import * as React from 'react'; import { View, Text, useWindowDimensions, StyleSheet, ImageSourcePropType, ScrollView, ActivityIndicator, Pressable, Alert } from 'react-native'; import { TabView, SceneMap, TabBar } from 'react-native-tab-view'; import { IndividualCouponComponent } from '../accountPages/walletPageComponent'; import { formatCouponDate } from '../../util/lib'; import { useCallback, useEffect, useRef, useState } from 'react'; import { walletService } from '../../service/walletService'; import { useChargingStore } from '../../providers/scan_qr_payload_store'; import { chargeStationService } from '../../service/chargeStationService'; import { router } from 'expo-router'; import axios from 'axios'; export interface TabItem { imgURL: ImageSourcePropType; date: string; time: string; chargeStationName: string; chargeStationAddress: string; distance: string; } interface TabViewComponentProps { titles: string[]; } const FirstRoute = ({ coupons, loading, handleCouponClick }: { coupons: any; loading: boolean; handleCouponClick: any; }) => { return ( {loading ? ( ) : ( {coupons.filter( (coupon: any) => coupon.is_consumed === false && new Date(coupon.expire_date) > new Date() ).length === 0 ? ( 暫時戶口沒有優惠券。 ) : ( coupons .filter( (coupon: any) => coupon.is_consumed === false && new Date(coupon.expire_date) > new Date() ) .sort( (a: any, b: any) => new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime() ) .slice(0, 30) .map((coupon: any, index: any) => ( )) )} )} ); }; const SecondRoute = ({ coupons }: { coupons: any }) => ( {coupons .filter((coupon: any) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date()) .slice(0, 30) .map((coupon: any, index: any) => ( ))} ); const DisplayedOnlyCouponTabView: React.FC = ({ titles }) => { const layout = useWindowDimensions(); const [loading, setLoading] = useState(false); const [coupons, setCoupons] = useState([]); const [userID, setUserID] = useState(''); const [modalVisible, setModalVisible] = useState(false); useEffect(() => { const fetchData = async () => { try { setLoading(true); const info = await walletService.getCustomerInfo(); const coupon = await walletService.getCouponForSpecificUser(info.id); setUserID(info.id); setCoupons(coupon); } catch (error) { console.log(error); } finally { setLoading(false); } }; fetchData(); }, []); const handleCouponClick = async (clickedCoupon: string) => { Alert.alert( '立即使用優惠券', // Title '按確認打開相機,掃描充電站上的二維碼以使用優惠券', // Message [ { text: '取消', style: 'cancel' }, { text: '確認', onPress: () => router.push('scanQrPage') } ] ); }; const renderScene = useCallback( ({ route }: { route: any }) => { switch (route.key) { case 'firstRoute': return ; case 'secondRoute': return ; default: return null; } }, [coupons, loading, handleCouponClick] ); const [routes] = React.useState([ { key: 'firstRoute', title: titles[0] }, { key: 'secondRoute', title: titles[1] } ]); const [index, setIndex] = React.useState(0); const renderTabBar = (props: any) => ( ( {route.title} )} indicatorStyle={{ backgroundColor: '#025c72' }} style={{ backgroundColor: 'white', borderColor: '#DBE4E8', elevation: 0, marginHorizontal: 15, borderBottomWidth: 0.5 }} /> ); return ( ); }; export default DisplayedOnlyCouponTabView; const styles = StyleSheet.create({ container: { flexDirection: 'row' }, image: { width: 100, height: 100, margin: 15, borderRadius: 10 }, textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 }, floatingButton: { elevation: 5, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84 } });