couponTabView.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //the size of the TabView will follow its parent-container's size.
  2. import * as React from 'react';
  3. import {
  4. View,
  5. Text,
  6. useWindowDimensions,
  7. StyleSheet,
  8. ImageSourcePropType,
  9. ScrollView,
  10. ActivityIndicator
  11. } from 'react-native';
  12. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  13. import { IndividualCouponComponent } from '../accountPages/walletPageComponent';
  14. import { formatCouponDate } from '../../util/lib';
  15. import { useEffect, useState } from 'react';
  16. import { walletService } from '../../service/walletService';
  17. export interface TabItem {
  18. imgURL: ImageSourcePropType;
  19. date: string;
  20. time: string;
  21. chargeStationName: string;
  22. chargeStationAddress: string;
  23. distance: string;
  24. }
  25. interface TabViewComponentProps {
  26. titles: string[];
  27. }
  28. const CouponTabViewComponent: React.FC<TabViewComponentProps> = ({ titles }) => {
  29. const layout = useWindowDimensions();
  30. const [loading, setLoading] = useState(false);
  31. const [coupons, setCoupons] = useState([]);
  32. const [userID, setUserID] = useState('');
  33. useEffect(() => {
  34. const fetchData = async () => {
  35. try {
  36. setLoading(true);
  37. const info = await walletService.getCustomerInfo();
  38. const coupon = await walletService.getCouponForSpecificUser(userID);
  39. setUserID(info.id);
  40. setCoupons(coupon);
  41. } catch (error) {
  42. console.log(error);
  43. } finally {
  44. setLoading(false);
  45. }
  46. };
  47. fetchData();
  48. }, []);
  49. //tab 1
  50. const FirstRoute = () => (
  51. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  52. <View className="flex-1 flex-col">
  53. {loading ? (
  54. <View className="items-center justify-center">
  55. <ActivityIndicator />
  56. </View>
  57. ) : (
  58. <View>
  59. {coupons.filter(
  60. (coupon) => coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  61. ).length === 0 ? (
  62. <Text className='pl-4'>暫時戶口沒有優惠券。</Text>
  63. ) : (
  64. coupons
  65. .filter(
  66. (coupon) =>
  67. coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  68. )
  69. .slice(0, 2)
  70. .map((coupon, index) => (
  71. <IndividualCouponComponent
  72. key={index}
  73. title={coupon.name}
  74. price={coupon.amount}
  75. detail={coupon.description}
  76. date={formatCouponDate(coupon.expire_date)}
  77. />
  78. ))
  79. )}
  80. </View>
  81. )}
  82. </View>
  83. </ScrollView>
  84. );
  85. //tab 2
  86. const SecondRoute = () => (
  87. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  88. <View className="flex-1 flex-col">
  89. {coupons
  90. .filter((coupon) => coupon.is_consumed === true && new Date(coupon.expire_date) < new Date())
  91. .slice(0, 2)
  92. .map((coupon, index) => (
  93. <IndividualCouponComponent
  94. key={index}
  95. title={coupon.name}
  96. price={coupon.amount}
  97. detail={coupon.description}
  98. date={formatCouponDate(coupon.expire_date)}
  99. />
  100. ))}
  101. </View>
  102. </ScrollView>
  103. );
  104. const renderScene = SceneMap({
  105. firstRoute: FirstRoute,
  106. secondRoute: SecondRoute
  107. });
  108. const [routes] = React.useState([
  109. { key: 'firstRoute', title: titles[0] },
  110. { key: 'secondRoute', title: titles[1] }
  111. ]);
  112. const [index, setIndex] = React.useState(0);
  113. const renderTabBar = (props: any) => (
  114. <TabBar
  115. {...props}
  116. renderLabel={({ route, focused }) => (
  117. <Text
  118. style={{
  119. color: focused ? '#025c72' : '#888888',
  120. fontWeight: focused ? '900' : 'thin',
  121. fontSize: 20
  122. }}
  123. >
  124. {route.title}
  125. </Text>
  126. )}
  127. indicatorStyle={{
  128. backgroundColor: '#025c72'
  129. }}
  130. style={{
  131. backgroundColor: 'white',
  132. borderColor: '#DBE4E8',
  133. elevation: 0,
  134. marginHorizontal: 15,
  135. borderBottomWidth: 0.5
  136. }}
  137. />
  138. );
  139. return (
  140. <TabView
  141. navigationState={{ index, routes }}
  142. renderScene={renderScene}
  143. onIndexChange={setIndex}
  144. initialLayout={{ width: layout.width }}
  145. renderTabBar={renderTabBar}
  146. />
  147. );
  148. };
  149. export default CouponTabViewComponent;
  150. const styles = StyleSheet.create({
  151. container: { flexDirection: 'row' },
  152. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  153. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 }
  154. });