couponTabView.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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: 200 }}
  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 &&
  60. (coupon.expire_date === null || new Date(coupon.expire_date) > new Date())
  61. ).length === 0 ? (
  62. <Text className="pl-4">暫時戶口沒有優惠券。</Text>
  63. ) : (
  64. coupons
  65. .filter(
  66. (coupon: any) =>
  67. coupon.is_consumed === false &&
  68. (coupon.expire_date === null ||
  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={handleCouponClick}
  79. // key={coupon.redeem_code}
  80. key={`${coupon.id}-${index}`}
  81. title={coupon.coupon.name}
  82. price={coupon.coupon.amount}
  83. detail={coupon.coupon.description}
  84. date={formatCouponDate(coupon.expire_date)}
  85. setOpacity={false}
  86. redeem_code={coupon.id}
  87. />
  88. ))
  89. )}
  90. </View>
  91. </View>
  92. )}
  93. </View>
  94. </ScrollView>
  95. </View>
  96. );
  97. };
  98. const SecondRoute = ({ coupons }: { coupons: any }) => (
  99. <ScrollView style={{ flex: 1, backgroundColor: 'white', marginTop: 14 }}>
  100. <View className="flex-1 flex-col">
  101. {coupons
  102. .filter(
  103. (coupon: any) =>
  104. coupon.is_consumed === true ||
  105. (coupon.expire_date !== null && new Date(coupon.expire_date) < new Date())
  106. )
  107. .slice(0, 30)
  108. .map((coupon: any, index: any) => (
  109. <IndividualCouponComponent
  110. key={`${coupon.id}-${index}`}
  111. title={coupon.coupon.name}
  112. price={coupon.coupon.amount}
  113. detail={coupon.coupon.description}
  114. date={formatCouponDate(coupon.expire_date)}
  115. setOpacity={true}
  116. noCircle={true}
  117. />
  118. ))}
  119. </View>
  120. </ScrollView>
  121. );
  122. const CouponTabViewComponent: React.FC<TabViewComponentProps> = ({ titles }) => {
  123. const layout = useWindowDimensions();
  124. const [loading, setLoading] = useState(false);
  125. const [coupons, setCoupons] = useState([]);
  126. const [userID, setUserID] = useState('');
  127. const {
  128. current_price_store,
  129. setCurrentPriceStore,
  130. stationID,
  131. promotion_code,
  132. setPromotionCode,
  133. setCouponDetail,
  134. coupon_detail,
  135. total_power,
  136. setTotalPower,
  137. setProcessedCouponStore,
  138. setSumOfCoupon
  139. } = useChargingStore();
  140. useEffect(() => {
  141. const fetchData = async () => {
  142. try {
  143. setLoading(true);
  144. const info = await walletService.getCustomerInfo();
  145. const coupon = await walletService.getCouponForSpecificUser(info.id);
  146. setUserID(info.id);
  147. setCoupons(coupon);
  148. } catch (error) {
  149. console.log(error);
  150. } finally {
  151. setLoading(false);
  152. }
  153. };
  154. fetchData();
  155. }, []);
  156. //fetch current price for coupon valid calculation
  157. useEffect(() => {
  158. const fetchCurrentPrice = async () => {
  159. try {
  160. const response = await chargeStationService.getOriginalPriceInPay(stationID);
  161. setCurrentPriceStore(response);
  162. } catch (error) {
  163. // More specific error handling
  164. if (axios.isAxiosError(error)) {
  165. const errorMessage = error.response?.data?.message || 'Network error occurred';
  166. Alert.alert('Error', `Unable to fetch price: ${errorMessage}`, [
  167. {
  168. text: 'OK',
  169. onPress: () => {
  170. cleanupData();
  171. router.push('/mainPage');
  172. }
  173. }
  174. ]);
  175. } else {
  176. Alert.alert('Error', 'An unexpected error occurred while fetching the price', [
  177. {
  178. text: 'OK',
  179. onPress: () => {
  180. cleanupData();
  181. router.push('/mainPage');
  182. }
  183. }
  184. ]);
  185. }
  186. }
  187. };
  188. fetchCurrentPrice();
  189. }, []);
  190. const handleCouponClick = async (clickedCoupon: string) => {
  191. let temp_promotion_code = [...promotion_code];
  192. if (!current_price_store) {
  193. Alert.alert('Error', 'Unable to fetch price', [
  194. {
  195. text: 'OK',
  196. onPress: () => {
  197. cleanupData();
  198. router.push('/mainPage');
  199. }
  200. }
  201. ]);
  202. return;
  203. }
  204. let orderAmount = current_price_store * total_power;
  205. //when i click on a coupon, if coupone doesnt already exist in the stack, i add it to the stack
  206. if (!promotion_code.includes(clickedCoupon)) {
  207. const found_coupon = coupons.find((coupon: any) => coupon.id === clickedCoupon);
  208. temp_promotion_code = [...promotion_code, found_coupon.id];
  209. try {
  210. const valid = await chargeStationService.validateCoupon(temp_promotion_code, orderAmount);
  211. if (valid === true) {
  212. setCouponDetail([...coupon_detail, found_coupon]);
  213. setPromotionCode([...promotion_code, clickedCoupon]);
  214. } else {
  215. Alert.alert('不符合使用優惠券的條件', '請查看優惠卷的詳情,例如是否需要滿足最低消費金額。');
  216. }
  217. } catch (error) {
  218. console.log(error);
  219. }
  220. } else {
  221. //coupon already exists, this de-select the coupon
  222. const index_of_clicked_coupon = promotion_code.findIndex((i) => i === clickedCoupon);
  223. const newPromotionCode = [...promotion_code];
  224. newPromotionCode.splice(index_of_clicked_coupon, 1);
  225. setPromotionCode(newPromotionCode);
  226. const newCouponDetail = coupon_detail.filter((detail: any) => detail.id !== clickedCoupon);
  227. setCouponDetail(newCouponDetail);
  228. }
  229. };
  230. const cleanupData = () => {
  231. setPromotionCode([]);
  232. setCouponDetail([]);
  233. setProcessedCouponStore([]);
  234. setSumOfCoupon(0);
  235. setTotalPower(null);
  236. };
  237. const renderScene = useCallback(
  238. ({ route }: { route: any }) => {
  239. switch (route.key) {
  240. case 'firstRoute':
  241. return <FirstRoute coupons={coupons} loading={loading} handleCouponClick={handleCouponClick} />;
  242. case 'secondRoute':
  243. return <SecondRoute coupons={coupons} />;
  244. default:
  245. return null;
  246. }
  247. },
  248. [coupons, loading, handleCouponClick]
  249. );
  250. const [routes] = React.useState([
  251. { key: 'firstRoute', title: titles[0] },
  252. { key: 'secondRoute', title: titles[1] }
  253. ]);
  254. const [index, setIndex] = React.useState(0);
  255. const renderTabBar = (props: any) => (
  256. <TabBar
  257. {...props}
  258. renderLabel={({ route, focused }) => (
  259. <Text
  260. style={{
  261. color: focused ? '#025c72' : '#888888',
  262. fontWeight: focused ? '900' : 'thin',
  263. fontSize: 20
  264. }}
  265. >
  266. {route.title}
  267. </Text>
  268. )}
  269. indicatorStyle={{
  270. backgroundColor: '#025c72'
  271. }}
  272. style={{
  273. backgroundColor: 'white',
  274. borderColor: '#DBE4E8',
  275. elevation: 0,
  276. marginHorizontal: 15,
  277. borderBottomWidth: 0.5
  278. }}
  279. />
  280. );
  281. return (
  282. <TabView
  283. navigationState={{ index, routes }}
  284. renderScene={renderScene}
  285. onIndexChange={setIndex}
  286. initialLayout={{ width: layout.width }}
  287. renderTabBar={renderTabBar}
  288. />
  289. );
  290. };
  291. export default CouponTabViewComponent;
  292. const styles = StyleSheet.create({
  293. container: { flexDirection: 'row' },
  294. image: { width: 100, height: 100, margin: 15, borderRadius: 10 },
  295. textContainer: { flexDirection: 'column', gap: 8, marginTop: 20 },
  296. floatingButton: {
  297. elevation: 5,
  298. shadowColor: '#000',
  299. shadowOffset: { width: 0, height: 2 },
  300. shadowOpacity: 0.25,
  301. shadowRadius: 3.84
  302. }
  303. });