displayedOnlyCouponTabView.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { router } from 'expo-router';
  20. import { useTranslation } from '../../util/hooks/useTranslation';
  21. export interface TabItem {
  22. imgURL: ImageSourcePropType;
  23. date: string;
  24. time: string;
  25. chargeStationName: string;
  26. chargeStationAddress: string;
  27. distance: string;
  28. }
  29. interface TabViewComponentProps {
  30. titles: string[];
  31. }
  32. const FirstRoute = ({
  33. coupons,
  34. loading,
  35. handleCouponClick
  36. }: {
  37. coupons: any;
  38. loading: boolean;
  39. handleCouponClick: (couponName: string, couponDescription: string) => void;
  40. }) => {
  41. const { t, getCurrentLanguageConfig } = useTranslation(); // 使用翻译钩子
  42. const isEn = getCurrentLanguageConfig()?.code === 'en';
  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>
  56. <View>
  57. {coupons.filter(
  58. (coupon: any) =>
  59. coupon.permission == true &&
  60. coupon.is_consumed === false &&
  61. new Date(coupon.expire_date) > new Date()
  62. ).length === 0 ? (
  63. <Text className="pl-4">{t('wallet.coupon.noCoupon')}</Text>
  64. ) : (
  65. coupons
  66. .filter(
  67. (coupon: any) =>
  68. coupon.permission == true &&
  69. coupon.is_consumed === false &&
  70. new Date(coupon.expire_date) > new Date()
  71. )
  72. .sort(
  73. (a: any, b: any) =>
  74. new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime()
  75. )
  76. .slice(0, 30)
  77. .map((coupon: any, index: any) => (
  78. <IndividualCouponComponent
  79. onCouponClick={() =>
  80. handleCouponClick(isEn? coupon.coupon.name_en:coupon.coupon.name, isEn?coupon.coupon.description_en:coupon.coupon.description)
  81. }
  82. // key={coupon.redeem_code}
  83. key={`${coupon.id}-${index}`}
  84. title={isEn? coupon.coupon.name_en:coupon.coupon.name}
  85. price={coupon.coupon.amount}
  86. detail={isEn?coupon.coupon.description_en:coupon.coupon.description}
  87. date={formatCouponDate(coupon.expire_date)}
  88. setOpacity={false}
  89. noCircle={true}
  90. redeem_code={coupon.id}
  91. />
  92. ))
  93. )}
  94. </View>
  95. </View>
  96. )}
  97. </View>
  98. </ScrollView>
  99. </View>
  100. );
  101. };
  102. const SecondRoute = ({ coupons }: { coupons: any }) => {
  103. const { t, getCurrentLanguageConfig } = useTranslation(); // 使用翻译钩子
  104. const isEn = getCurrentLanguageConfig()?.code === 'en';
  105. return (
  106. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  107. <View className="flex-1 flex-col">
  108. {coupons
  109. .filter((coupon: any) => coupon.is_consumed === true || new Date(coupon.expire_date) < new Date())
  110. .slice(0, 30)
  111. .map((coupon: any, index: any) => (
  112. <IndividualCouponComponent
  113. key={`${coupon.id}-${index}`}
  114. title={isEn? coupon.coupon.name_en:coupon.coupon.name}
  115. price={coupon.coupon.amount}
  116. detail={isEn?coupon.coupon.description_en:coupon.coupon.description}
  117. date={formatCouponDate(coupon.expire_date)}
  118. setOpacity={true}
  119. noCircle={true}
  120. />
  121. ))}
  122. </View>
  123. </ScrollView>
  124. );
  125. }
  126. const DisplayedOnlyCouponTabView: React.FC<TabViewComponentProps> = ({ titles }) => {
  127. const layout = useWindowDimensions();
  128. const [loading, setLoading] = useState(false);
  129. const [coupons, setCoupons] = useState([]);
  130. const [userID, setUserID] = useState('');
  131. const [useableCoupons, setUseableCoupons] = useState([]);
  132. useEffect(() => {
  133. const fetchData = async () => {
  134. try {
  135. setLoading(true);
  136. const info = await walletService.getCustomerInfo();
  137. const coupon = await walletService.getCouponForSpecificUser(info.id);
  138. const useableConpon = coupon.filter((couponObj: any) => {
  139. const today = new Date();
  140. if (couponObj.expire_date === null) {
  141. return couponObj.is_consumed === false;
  142. }
  143. const expireDate = new Date(couponObj.expire_date);
  144. return expireDate > today && couponObj.is_consumed === false;
  145. });
  146. setUserID(info.id);
  147. setCoupons(coupon);
  148. setUseableCoupons(useableConpon);
  149. } catch (error) {
  150. console.log(error);
  151. } finally {
  152. setLoading(false);
  153. }
  154. };
  155. fetchData();
  156. }, []);
  157. const handleCouponClick = async (clickedCoupon: string, clickedCouponDescription: string) => {
  158. router.push({
  159. pathname: '/couponDetailPage',
  160. params: {
  161. couponName: clickedCoupon,
  162. couponDescription: clickedCouponDescription
  163. }
  164. });
  165. };
  166. const renderScene = useCallback(
  167. ({ route }: { route: any }) => {
  168. switch (route.key) {
  169. case 'firstRoute':
  170. return (
  171. <FirstRoute coupons={useableCoupons} loading={loading} handleCouponClick={handleCouponClick} />
  172. );
  173. case 'secondRoute':
  174. return <SecondRoute coupons={coupons} />;
  175. default:
  176. return null;
  177. }
  178. },
  179. [coupons, loading, handleCouponClick]
  180. );
  181. const [routes] = React.useState([
  182. { key: 'firstRoute', title: titles[0] },
  183. { key: 'secondRoute', title: titles[1] }
  184. ]);
  185. const [index, setIndex] = React.useState(0);
  186. const renderTabBar = (props: any) => (
  187. <TabBar
  188. {...props}
  189. indicatorStyle={{
  190. backgroundColor: '#025c72'
  191. }}
  192. style={{
  193. backgroundColor: 'white',
  194. borderColor: '#DBE4E8',
  195. elevation: 0,
  196. marginHorizontal: 15,
  197. borderBottomWidth: 0.5
  198. }}
  199. />
  200. );
  201. return (
  202. <TabView
  203. navigationState={{ index, routes }}
  204. renderScene={renderScene}
  205. onIndexChange={setIndex}
  206. initialLayout={{ width: layout.width }}
  207. renderTabBar={renderTabBar}
  208. commonOptions={{
  209. label: ({ route, focused }) => (
  210. <Text
  211. style={{
  212. color: focused ? '#025c72' : '#888888',
  213. fontWeight: focused ? '900' : 'thin',
  214. fontSize: 20
  215. }}
  216. >
  217. {route.title}
  218. </Text>
  219. )
  220. }}
  221. />
  222. );
  223. };
  224. export default DisplayedOnlyCouponTabView;
  225. const styles = StyleSheet.create({
  226. container: { flexDirection: 'row' },
  227. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  228. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  229. floatingButton: {
  230. elevation: 5,
  231. shadowColor: '#000',
  232. shadowOffset: { width: 0, height: 2 },
  233. shadowOpacity: 0.25,
  234. shadowRadius: 3.84
  235. }
  236. });