walletPageComponent.tsx 8.0 KB

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