displayedOnlyCouponTabView.tsx 9.3 KB

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