walletPageComponent.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. View,
  3. Text,
  4. ScrollView,
  5. Pressable,
  6. ImageBackground,
  7. ActivityIndicator
  8. } from 'react-native';
  9. import { SafeAreaView } from 'react-native-safe-area-context';
  10. import { router } from 'expo-router';
  11. import { CrossLogoSvg } from '../global/SVG';
  12. import { useEffect, useState } from 'react';
  13. import { walletService } from '../../service/walletService';
  14. import { formatCouponDate, formatDate } from '../../util/lib';
  15. export const IndividualCouponComponent = ({
  16. title,
  17. price,
  18. detail,
  19. date
  20. }: {
  21. title: string;
  22. price: string;
  23. detail: string;
  24. date: string;
  25. }) => {
  26. return (
  27. <View className="bg-[#e7f2f8] h-[124px] rounded-xl flex-row mb-3">
  28. <View className="bg-white mx-3 my-3 w-[28%] rounded-xl">
  29. <View className="flex-row justify-center items-center pr-4 pt-4 ">
  30. <Text className="color-[#02677d] text-2xl pl-2 pr-1">
  31. $
  32. </Text>
  33. <Text
  34. className="color-[#02677d] text-3xl font-bold"
  35. adjustsFontSizeToFit={true}
  36. >
  37. {price}
  38. </Text>
  39. </View>
  40. <View className="items-center justify-center">
  41. <Text className="text-base mt-1">{title}</Text>
  42. </View>
  43. </View>
  44. {/* //dash line */}
  45. <View style={{ overflow: 'hidden' }}>
  46. <View
  47. style={{
  48. borderStyle: 'dashed',
  49. borderWidth: 1,
  50. borderColor: '#CCCCCC',
  51. margin: -1,
  52. width: 0,
  53. marginRight: 0,
  54. height: '100%'
  55. }}
  56. >
  57. <View style={{ height: 60 }}></View>
  58. </View>
  59. </View>
  60. <View className="flex-col flex-1 m-[5%] justify-center ">
  61. <Text className="text-lg">{title}</Text>
  62. <Text className="color-[#888888] text-sm">{detail}</Text>
  63. <View className="flex-row items-center ">
  64. <Text className="text-base">有效期 </Text>
  65. <Text className="text-base font-bold text-[#02677d]">
  66. {' '}
  67. {date}
  68. </Text>
  69. </View>
  70. </View>
  71. </View>
  72. );
  73. };
  74. const WalletPageComponent = () => {
  75. const [walletBalance, setWalletBalance] = useState<string | null>(null);
  76. const [loading, setLoading] = useState<boolean>(false);
  77. const [coupons, setCoupons] = useState([]);
  78. useEffect(() => {
  79. try {
  80. setLoading(true);
  81. const fetchWalletBalanceAndCoupon = async () => {
  82. await new Promise((resolve) => setTimeout(resolve, 5000));
  83. const balance = await walletService.getWalletBalance();
  84. const coupon = await walletService.getCoupon();
  85. console.log(JSON.stringify(coupon));
  86. setCoupons(coupon);
  87. setWalletBalance(balance);
  88. setLoading(false);
  89. };
  90. fetchWalletBalanceAndCoupon();
  91. } catch (error) {
  92. console.log(error);
  93. }
  94. }, []);
  95. const formatMoney = (amount: any) => {
  96. if (typeof amount !== 'number') {
  97. amount = Number(amount);
  98. }
  99. return amount.toLocaleString('en-US');
  100. };
  101. return (
  102. <SafeAreaView
  103. className="flex-1 bg-white"
  104. edges={['top', 'right', 'left']}
  105. >
  106. <ScrollView className="flex-1 ">
  107. <View className="flex-1 mx-[5%]">
  108. <View style={{ marginTop: 25 }}>
  109. <Pressable
  110. onPress={() => {
  111. if (router.canGoBack()) {
  112. router.back();
  113. } else {
  114. router.replace('/accountMainPage');
  115. }
  116. }}
  117. >
  118. <CrossLogoSvg />
  119. </Pressable>
  120. <Text style={{ fontSize: 45, marginVertical: 25 }}>
  121. 錢包
  122. </Text>
  123. </View>
  124. <View>
  125. <ImageBackground
  126. className="flex-col-reverse shadow-lg"
  127. style={{ height: 200 }}
  128. source={require('../../assets/walletCard1.png')}
  129. resizeMode="contain"
  130. >
  131. <View className="mx-[5%] pb-6">
  132. <Text className="text-white text-xl">
  133. 餘額 (HKD)
  134. </Text>
  135. <View className="flex-row items-center justify-between ">
  136. <Text
  137. style={{ fontSize: 52 }}
  138. className=" text-white font-bold"
  139. >
  140. {loading ? (
  141. <View className="items-center">
  142. <ActivityIndicator />
  143. </View>
  144. ) : (
  145. formatMoney(walletBalance)
  146. )}
  147. </Text>
  148. <Pressable
  149. className="rounded-2xl items-center justify-center p-3 px-5 pr-6 "
  150. style={{
  151. backgroundColor:
  152. 'rgba(231, 242, 248, 0.2)'
  153. }}
  154. onPress={() => console.log('增值')}
  155. >
  156. <Text className="text-white font-bold">
  157. + 增值
  158. </Text>
  159. </Pressable>
  160. </View>
  161. </View>
  162. </ImageBackground>
  163. </View>
  164. <View className="flex-row-reverse mt-2 mb-6">
  165. <Text className="text-[#02677D] text-lg underline">
  166. 更多錢包資訊
  167. </Text>
  168. </View>
  169. </View>
  170. <View className="w-full h-1 bg-[#DBE4E8]" />
  171. <View className="flex-row justify-between mx-[5%] pt-6 pb-3">
  172. <Text className="text-xl">優惠券</Text>
  173. <Pressable onPress={() => router.push('couponPage')}>
  174. <Text className="text-xl text-[#888888]">顯示所有</Text>
  175. </Pressable>
  176. </View>
  177. <View className="flex-1 flex-col mx-[5%]">
  178. {coupons
  179. .filter(
  180. (coupon) =>
  181. coupon.is_consumned === false &&
  182. new Date(coupon.expire_date) > new Date()
  183. )
  184. .slice(0, 2)
  185. .map((coupon, index) => (
  186. <IndividualCouponComponent
  187. key={index}
  188. title={coupon.name}
  189. price={coupon.amount}
  190. detail={coupon.description}
  191. date={formatCouponDate(coupon.expire_date)}
  192. />
  193. ))}
  194. </View>
  195. </ScrollView>
  196. </SafeAreaView>
  197. );
  198. };
  199. export default WalletPageComponent;