couponTabView.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // component/global/couponTabView.tsx
  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. import { useTranslation } from '../../util/hooks/useTranslation';
  24. export interface TabItem {
  25. imgURL: ImageSourcePropType;
  26. date: string;
  27. time: string;
  28. chargeStationName: string;
  29. chargeStationAddress: string;
  30. distance: string;
  31. }
  32. interface TabViewComponentProps {
  33. titles: string[];
  34. }
  35. const FirstRoute = ({
  36. coupons,
  37. loading,
  38. handleCouponClick
  39. }: {
  40. coupons: any;
  41. loading: boolean;
  42. handleCouponClick: any;
  43. }) => {
  44. const { t, getCurrentLanguageConfig } = useTranslation(); // 使用翻译钩子
  45. const isEn = getCurrentLanguageConfig()?.code === 'en';
  46. return (
  47. <View className="flex-1">
  48. <ScrollView
  49. style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}
  50. contentContainerStyle={{ paddingBottom: 200 }}
  51. >
  52. <View className="flex-1 flex-col">
  53. {loading ? (
  54. <View className="items-center justify-center">
  55. <ActivityIndicator />
  56. </View>
  57. ) : (
  58. <View className="">
  59. <View>
  60. {coupons.filter(
  61. (coupon: any) =>
  62. coupon.permission == true &&
  63. coupon.is_consumed === false &&
  64. (coupon.expire_date === null || new Date(coupon.expire_date) > new Date())
  65. ).length === 0 ? (
  66. <Text className="pl-4">{t('selectCoupons.coupon.noCoupon')}</Text>
  67. ) : (
  68. coupons
  69. .filter(
  70. (coupon: any) =>
  71. coupon.permission == true &&
  72. coupon.is_consumed === false &&
  73. (coupon.expire_date === null ||
  74. new Date(coupon.expire_date) > new Date())
  75. )
  76. .sort(
  77. (a: any, b: any) =>
  78. new Date(a.expire_date).getTime() - new Date(b.expire_date).getTime()
  79. )
  80. .slice(0, 30)
  81. .map((coupon: any, index: any) => (
  82. <IndividualCouponComponent
  83. onCouponClick={handleCouponClick}
  84. // key={coupon.redeem_code}
  85. key={`${coupon.id}-${index}`}
  86. title={isEn? coupon.coupon.name_en:coupon.coupon.name}
  87. price={coupon.coupon.amount}
  88. detail={isEn?coupon.coupon.description_en:coupon.coupon.description}
  89. date={formatCouponDate(coupon.expire_date)}
  90. setOpacity={false}
  91. redeem_code={coupon.id}
  92. />
  93. ))
  94. )}
  95. </View>
  96. </View>
  97. )}
  98. </View>
  99. </ScrollView>
  100. </View>
  101. );
  102. };
  103. const SecondRoute = ({ coupons }: { coupons: any }) => {
  104. const { t, getCurrentLanguageConfig } = useTranslation(); // 使用翻译钩子
  105. const isEn = getCurrentLanguageConfig()?.code === 'en';
  106. return (
  107. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  108. <View className="flex-1 flex-col">
  109. {coupons
  110. .filter(
  111. (coupon: any) =>
  112. coupon.is_consumed === true ||
  113. (coupon.expire_date !== null && new Date(coupon.expire_date) < new Date())
  114. )
  115. .slice(0, 30)
  116. .map((coupon: any, index: any) => (
  117. <IndividualCouponComponent
  118. key={`${coupon.id}-${index}`}
  119. title={isEn? coupon.coupon.name_en:coupon.coupon.name}
  120. price={coupon.coupon.amount}
  121. detail={isEn?coupon.coupon.description_en:coupon.coupon.description}
  122. date={formatCouponDate(coupon.expire_date)}
  123. setOpacity={true}
  124. noCircle={true}
  125. />
  126. ))}
  127. </View>
  128. </ScrollView>
  129. );
  130. };
  131. const CouponTabViewComponent: React.FC<TabViewComponentProps> = ({ titles }) => {
  132. const { t } = useTranslation();
  133. const layout = useWindowDimensions();
  134. const [loading, setLoading] = useState(false);
  135. const [coupons, setCoupons] = useState([]);
  136. const [userID, setUserID] = useState('');
  137. const {
  138. current_price_store,
  139. setCurrentPriceStore,
  140. stationID,
  141. promotion_code,
  142. setPromotionCode,
  143. setCouponDetail,
  144. coupon_detail,
  145. total_power,
  146. setTotalPower,
  147. setProcessedCouponStore,
  148. setSumOfCoupon
  149. } = useChargingStore();
  150. useEffect(() => {
  151. const fetchData = async () => {
  152. try {
  153. setLoading(true);
  154. const info = await walletService.getCustomerInfo();
  155. const coupon = await walletService.getCouponForSpecificUser(info.id);
  156. setUserID(info.id);
  157. setCoupons(coupon);
  158. } catch (error) {
  159. console.log(error);
  160. } finally {
  161. setLoading(false);
  162. }
  163. };
  164. fetchData();
  165. }, []);
  166. //fetch current price for coupon valid calculation
  167. useEffect(() => {
  168. const fetchCurrentPrice = async () => {
  169. try {
  170. const response = await chargeStationService.getOriginalPriceInPay(stationID);
  171. setCurrentPriceStore(response);
  172. } catch (error) {
  173. // More specific error handling
  174. if (axios.isAxiosError(error)) {
  175. const errorMessage = error.response?.data?.message || 'Network error occurred';
  176. Alert.alert(t('common.error'), `${t('selectCoupons.error_fetching_balance')}: ${errorMessage}`, [
  177. {
  178. text: t('common.ok'),
  179. onPress: () => {
  180. cleanupData();
  181. router.push('/mainPage');
  182. }
  183. }
  184. ]);
  185. } else {
  186. Alert.alert(t('common.error'), t('selectCoupons.error_fetching_balance'), [
  187. {
  188. text: t('common.ok'),
  189. onPress: () => {
  190. cleanupData();
  191. router.push('/mainPage');
  192. }
  193. }
  194. ]);
  195. }
  196. }
  197. };
  198. fetchCurrentPrice();
  199. }, []);
  200. const handleCouponClick = async (clickedCoupon: string) => {
  201. let temp_promotion_code = [...promotion_code];
  202. if (!current_price_store) {
  203. Alert.alert(t('common.error'), t('selectCoupons.error_fetching_balance'), [
  204. {
  205. text: t('common.ok'),
  206. onPress: () => {
  207. cleanupData();
  208. router.push('/mainPage');
  209. }
  210. }
  211. ]);
  212. return;
  213. }
  214. let orderAmount = current_price_store * total_power;
  215. //when i click on a coupon, if coupone doesnt already exist in the stack, i add it to the stack
  216. if (!promotion_code.includes(clickedCoupon)) {
  217. const found_coupon = coupons.find((coupon: any) => coupon.id === clickedCoupon);
  218. temp_promotion_code = [...promotion_code, found_coupon.id];
  219. try {
  220. const valid = await chargeStationService.validateCoupon(temp_promotion_code, orderAmount);
  221. if (valid === true) {
  222. setCouponDetail([...coupon_detail, found_coupon]);
  223. setPromotionCode([...promotion_code, clickedCoupon]);
  224. } else {
  225. Alert.alert(t('selectCoupons.coupon.invalid_condition_title'), t('selectCoupons.coupon.invalid_condition_message'));
  226. }
  227. } catch (error) {
  228. console.log(error);
  229. }
  230. } else {
  231. //coupon already exists, this de-select the coupon
  232. const index_of_clicked_coupon = promotion_code.findIndex((i) => i === clickedCoupon);
  233. const newPromotionCode = [...promotion_code];
  234. newPromotionCode.splice(index_of_clicked_coupon, 1);
  235. setPromotionCode(newPromotionCode);
  236. const newCouponDetail = coupon_detail.filter((detail: any) => detail.id !== clickedCoupon);
  237. setCouponDetail(newCouponDetail);
  238. }
  239. };
  240. const cleanupData = () => {
  241. setPromotionCode([]);
  242. setCouponDetail([]);
  243. setProcessedCouponStore([]);
  244. setSumOfCoupon(0);
  245. setTotalPower(null);
  246. };
  247. const renderScene = useCallback(
  248. ({ route }: { route: any }) => {
  249. switch (route.key) {
  250. case 'firstRoute':
  251. return <FirstRoute coupons={coupons} loading={loading} handleCouponClick={handleCouponClick} />;
  252. case 'secondRoute':
  253. return <SecondRoute coupons={coupons} />;
  254. default:
  255. return null;
  256. }
  257. },
  258. [coupons, loading, handleCouponClick]
  259. );
  260. const [routes] = React.useState([
  261. { key: 'firstRoute', title: titles[0] },
  262. { key: 'secondRoute', title: titles[1] }
  263. ]);
  264. const [index, setIndex] = React.useState(0);
  265. const renderTabBar = (props: any) => (
  266. <TabBar
  267. {...props}
  268. indicatorStyle={{
  269. backgroundColor: '#025c72'
  270. }}
  271. style={{
  272. backgroundColor: 'white',
  273. borderColor: '#DBE4E8',
  274. elevation: 0,
  275. marginHorizontal: 15,
  276. borderBottomWidth: 0.5
  277. }}
  278. />
  279. );
  280. return (
  281. <TabView
  282. navigationState={{ index, routes }}
  283. renderScene={renderScene}
  284. onIndexChange={setIndex}
  285. initialLayout={{ width: layout.width }}
  286. renderTabBar={renderTabBar}
  287. commonOptions={{
  288. label: ({ route, focused }) => (
  289. <Text
  290. style={{
  291. color: focused ? '#025c72' : '#888888',
  292. fontWeight: focused ? '900' : 'thin',
  293. fontSize: 20
  294. }}
  295. >
  296. {route.title}
  297. </Text>
  298. )
  299. }}
  300. />
  301. );
  302. };
  303. export default CouponTabViewComponent;
  304. const styles = StyleSheet.create({
  305. container: { flexDirection: 'row' },
  306. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  307. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  308. floatingButton: {
  309. elevation: 5,
  310. shadowColor: '#000',
  311. shadowOffset: { width: 0, height: 2 },
  312. shadowOpacity: 0.25,
  313. shadowRadius: 3.84
  314. }
  315. });