displayedOnlyCouponTabView.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. Pressable,
  12. Alert
  13. } from 'react-native';
  14. import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
  15. import { IndividualCouponComponent } from '../accountPages/walletPageComponent';
  16. import { formatCouponDate } from '../../util/lib';
  17. import { useCallback, useEffect, useRef, useState } from 'react';
  18. import { walletService } from '../../service/walletService';
  19. import { useChargingStore } from '../../providers/scan_qr_payload_store';
  20. import { chargeStationService } from '../../service/chargeStationService';
  21. import { router } from 'expo-router';
  22. import axios from 'axios';
  23. export interface TabItem {
  24. imgURL: ImageSourcePropType;
  25. date: string;
  26. time: string;
  27. chargeStationName: string;
  28. chargeStationAddress: string;
  29. distance: string;
  30. }
  31. interface TabViewComponentProps {
  32. titles: string[];
  33. }
  34. const FirstRoute = ({
  35. coupons,
  36. loading,
  37. handleCouponClick
  38. }: {
  39. coupons: any;
  40. loading: boolean;
  41. handleCouponClick: any;
  42. }) => {
  43. return (
  44. <View className="flex-1">
  45. <ScrollView
  46. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  47. contentContainerStyle={{ paddingBottom: 120 }}
  48. >
  49. <View className="flex-1 flex-col">
  50. {loading ? (
  51. <View className="items-center justify-center">
  52. <ActivityIndicator />
  53. </View>
  54. ) : (
  55. <View className="">
  56. <View>
  57. {coupons.filter(
  58. (coupon: any) =>
  59. coupon.is_consumed === false && new Date(coupon.expire_date) > new Date()
  60. ).length === 0 ? (
  61. <Text className="pl-4">暫時戶口沒有優惠券。</Text>
  62. ) : (
  63. coupons
  64. .filter(
  65. (coupon: any) =>
  66. coupon.is_consumed === false &&
  67. new Date(coupon.expire_date) > new Date()
  68. )
  69. .sort(
  70. (a: any, b: any) =>
  71. new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime()
  72. )
  73. .slice(0, 30)
  74. .map((coupon: any, index: any) => (
  75. <IndividualCouponComponent
  76. onCouponClick={handleCouponClick}
  77. // key={coupon.redeem_code}
  78. key={`${coupon.id}-${index}`}
  79. title={coupon.coupon.name}
  80. price={coupon.coupon.amount}
  81. detail={coupon.coupon.description}
  82. date={formatCouponDate(coupon.expire_date)}
  83. setOpacity={false}
  84. noCircle={true}
  85. redeem_code={coupon.id}
  86. />
  87. ))
  88. )}
  89. </View>
  90. </View>
  91. )}
  92. </View>
  93. </ScrollView>
  94. </View>
  95. );
  96. };
  97. const SecondRoute = ({ coupons }: { coupons: any }) => (
  98. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  99. <View className="flex-1 flex-col">
  100. {coupons
  101. .filter((coupon: any) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date())
  102. .slice(0, 30)
  103. .map((coupon: any, index: any) => (
  104. <IndividualCouponComponent
  105. key={`${coupon.id}-${index}`}
  106. title={coupon.coupon.name}
  107. price={coupon.coupon.amount}
  108. detail={coupon.coupon.description}
  109. date={formatCouponDate(coupon.expire_date)}
  110. setOpacity={true}
  111. noCircle={true}
  112. />
  113. ))}
  114. </View>
  115. </ScrollView>
  116. );
  117. const DisplayedOnlyCouponTabView: React.FC<TabViewComponentProps> = ({ titles }) => {
  118. const layout = useWindowDimensions();
  119. const [loading, setLoading] = useState(false);
  120. const [coupons, setCoupons] = useState([]);
  121. const [userID, setUserID] = useState('');
  122. const [modalVisible, setModalVisible] = useState(false);
  123. useEffect(() => {
  124. const fetchData = async () => {
  125. try {
  126. setLoading(true);
  127. const info = await walletService.getCustomerInfo();
  128. const coupon = await walletService.getCouponForSpecificUser(info.id);
  129. setUserID(info.id);
  130. setCoupons(coupon);
  131. } catch (error) {
  132. console.log(error);
  133. } finally {
  134. setLoading(false);
  135. }
  136. };
  137. fetchData();
  138. }, []);
  139. const handleCouponClick = async (clickedCoupon: string) => {
  140. Alert.alert(
  141. '立即使用優惠券', // Title
  142. '按確認打開相機,掃描充電站上的二維碼以使用優惠券', // Message
  143. [
  144. {
  145. text: '取消',
  146. style: 'cancel'
  147. },
  148. {
  149. text: '確認',
  150. onPress: () => router.push('scanQrPage')
  151. }
  152. ]
  153. );
  154. };
  155. const renderScene = useCallback(
  156. ({ route }: { route: any }) => {
  157. switch (route.key) {
  158. case 'firstRoute':
  159. return <FirstRoute coupons={coupons} loading={loading} handleCouponClick={handleCouponClick} />;
  160. case 'secondRoute':
  161. return <SecondRoute coupons={coupons} />;
  162. default:
  163. return null;
  164. }
  165. },
  166. [coupons, loading, handleCouponClick]
  167. );
  168. const [routes] = React.useState([
  169. { key: 'firstRoute', title: titles[0] },
  170. { key: 'secondRoute', title: titles[1] }
  171. ]);
  172. const [index, setIndex] = React.useState(0);
  173. const renderTabBar = (props: any) => (
  174. <TabBar
  175. {...props}
  176. renderLabel={({ route, focused }) => (
  177. <Text
  178. style={{
  179. color: focused ? '#025c72' : '#888888',
  180. fontWeight: focused ? '900' : 'thin',
  181. fontSize: 20
  182. }}
  183. >
  184. {route.title}
  185. </Text>
  186. )}
  187. indicatorStyle={{
  188. backgroundColor: '#025c72'
  189. }}
  190. style={{
  191. backgroundColor: 'white',
  192. borderColor: '#DBE4E8',
  193. elevation: 0,
  194. marginHorizontal: 15,
  195. borderBottomWidth: 0.5
  196. }}
  197. />
  198. );
  199. return (
  200. <TabView
  201. navigationState={{ index, routes }}
  202. renderScene={renderScene}
  203. onIndexChange={setIndex}
  204. initialLayout={{ width: layout.width }}
  205. renderTabBar={renderTabBar}
  206. />
  207. );
  208. };
  209. export default DisplayedOnlyCouponTabView;
  210. const styles = StyleSheet.create({
  211. container: { flexDirection: 'row' },
  212. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  213. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  214. floatingButton: {
  215. elevation: 5,
  216. shadowColor: '#000',
  217. shadowOffset: { width: 0, height: 2 },
  218. shadowOpacity: 0.25,
  219. shadowRadius: 3.84
  220. }
  221. });