paymentRecordPageComponent.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { View, Text, Pressable, Image, ScrollView, Alert, ImageBackground, ActivityIndicator } from 'react-native';
  2. import { SafeAreaView } from 'react-native-safe-area-context';
  3. import { router, useLocalSearchParams } from 'expo-router';
  4. import { PreviousPageBlackSvg } from '../global/SVG';
  5. import { useEffect, useState } from 'react';
  6. import { authenticationService } from '../../service/authService';
  7. import { FlashList } from '@shopify/flash-list';
  8. import { walletService } from '../../service/walletService';
  9. import { convertToHKTime } from '../../util/lib';
  10. interface TransactionRecordItem {
  11. date: string;
  12. description: string;
  13. amount: number;
  14. actual_total_power: string | number;
  15. current_price?: number;
  16. }
  17. const TransactionRow: React.FC<TransactionRecordItem> = ({
  18. date,
  19. description,
  20. amount,
  21. actual_total_power,
  22. current_price
  23. }) => (
  24. <View className="flex flex-row w-full py-4 border-b border-[#CCCCCC]">
  25. <Text className="flex-[0.2] text-sm">{date}</Text>
  26. <Text className="flex-[0.2] text-center text-sm">{description}</Text>
  27. <Text className="flex-[0.2] text-sm text-right ">
  28. {actual_total_power !== '-' ? Number(actual_total_power).toFixed(1) : actual_total_power}
  29. </Text>
  30. <Text className="flex-[0.2] text-sm text-right">{current_price ? `$${current_price}` : '-'}</Text>
  31. <Text className="flex-[0.2] text-sm text-right">${amount}</Text>
  32. </View>
  33. );
  34. const PaymentRecordPageComponent = () => {
  35. const params = useLocalSearchParams();
  36. const walletBalance = params.walletBalance;
  37. const [transactionRecord, setTransactionRecord] = useState<TransactionRecordItem[]>([]);
  38. const [loading, setLoading] = useState(false);
  39. const [loading1, setLoading1] = useState(false);
  40. // useEffect(() => {
  41. // const fetchTransactionRecord = async () => {
  42. // try {
  43. // const response = await walletService.getTransactionRecord();
  44. // console.log('response', response);
  45. // const formattedData: TransactionRecordItem[] = response
  46. // .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
  47. // .map((item: any) => {
  48. // let description;
  49. // if (item.type === 'wallet') {
  50. // switch (item.goods_name) {
  51. // case 'withdrawl':
  52. // description = '充電完成-退回餘額';
  53. // break;
  54. // case 'Penalty':
  55. // description = '繳付罰款';
  56. // break;
  57. // case 'Book a connector':
  58. // description = '預約充電費用';
  59. // break;
  60. // case 'Walk In':
  61. // description = '充電費用';
  62. // break;
  63. // default:
  64. // description = '充電';
  65. // }
  66. // } else {
  67. // description = '錢包增值';
  68. // }
  69. // return {
  70. // date: convertToHKTime(item.createdAt).hkDate,
  71. // description: description,
  72. // amount: item.amount,
  73. // actual_total_power:
  74. // item.actual_total_power !== undefined &&
  75. // item.actual_total_power !== null &&
  76. // item.actual_total_power !== '' &&
  77. // !isNaN(Number(item.actual_total_power))
  78. // ? item.actual_total_power
  79. // : '-'
  80. // };
  81. // });
  82. // setTransactionRecord(formattedData.slice(0, 10));
  83. // console.log('transactionRecord', transactionRecord);
  84. // } catch (error) {
  85. // console.log(error);
  86. // }
  87. // };
  88. // fetchTransactionRecord();
  89. // }, []);
  90. //fetch transaction record
  91. useEffect(() => {
  92. const fetchTransactionRecord = async () => {
  93. try {
  94. const response = await walletService.getTransactionRecord();
  95. const formattedData: TransactionRecordItem[] = response
  96. .sort((a: any, b: any) => new Date(b.createdAt) - new Date(a.createdAt))
  97. .filter(
  98. (item: any) =>
  99. item.type !== 'wallet' ||
  100. item.goods_name === 'Penalty' ||
  101. (item.goods_name === 'Walk In' && Number(item.actual_total_power) >= 1) // Keep Walk In items only if power >= 1
  102. )
  103. .map((item: any) => {
  104. let description;
  105. if (item.type === 'wallet') {
  106. switch (item.goods_name) {
  107. case 'withdrawl':
  108. description = '充電完成-退回餘額';
  109. break;
  110. case 'Penalty':
  111. description = '繳付罰款';
  112. break;
  113. case 'Book a connector':
  114. description = '預約充電費用';
  115. break;
  116. case 'Walk In':
  117. description = '充電費用';
  118. break;
  119. case 'manual_refund':
  120. description = '系統退款';
  121. break;
  122. default:
  123. description = '充電';
  124. }
  125. } else if (item.type === 'qfpay') {
  126. description = '錢包增值';
  127. }
  128. return {
  129. date: convertToHKTime(item.createdAt).hkDate,
  130. description: description,
  131. amount:
  132. item.type === 'qfpay'
  133. ? item.amount
  134. : item.goods_name === 'Penalty'
  135. ? item.amount
  136. : item.current_price && item.actual_total_power
  137. ? (item.current_price * item.actual_total_power).toFixed(1)
  138. : '-',
  139. actual_total_power:
  140. item.actual_total_power !== undefined &&
  141. item.actual_total_power !== null &&
  142. item.actual_total_power !== '' &&
  143. !isNaN(Number(item.actual_total_power))
  144. ? item.actual_total_power
  145. : '-',
  146. current_price:
  147. item.type === 'qfpay' || item.goods_name === 'Penalty' ? '-' : item.current_price
  148. };
  149. });
  150. setTransactionRecord(formattedData.slice(0, 10));
  151. } catch (error) {
  152. console.log(error);
  153. }
  154. };
  155. fetchTransactionRecord();
  156. }, []);
  157. return (
  158. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  159. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  160. <View style={{ marginTop: 25 }}>
  161. <Pressable
  162. onPress={() => {
  163. if (router.canGoBack()) {
  164. router.back();
  165. } else {
  166. router.replace('/accountMainPage');
  167. }
  168. }}
  169. >
  170. <PreviousPageBlackSvg />
  171. </Pressable>
  172. <Text style={{ fontSize: 45, marginVertical: 25 }}>錢包記錄</Text>
  173. </View>
  174. <View className="">
  175. <ImageBackground
  176. className="flex-col-reverse shadow-lg"
  177. style={{ height: 200 }}
  178. source={require('../../assets/walletCard1.png')}
  179. resizeMode="contain"
  180. >
  181. <View className="mx-[5%] pb-6">
  182. <Text className="text-white text-xl">餘額 (HKD)</Text>
  183. <View className="flex-row items-center justify-between ">
  184. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  185. {loading ? (
  186. <View className="items-center justify-center">
  187. <ActivityIndicator />
  188. </View>
  189. ) : (
  190. <>
  191. <Text>$</Text> {walletBalance}
  192. </>
  193. )}
  194. </Text>
  195. </View>
  196. </View>
  197. </ImageBackground>
  198. </View>
  199. <View className="flex flex-row w-full py-2 border-b border-[#CCCCCC]">
  200. <Text className="flex-[0.2] text-sm text-[#888888]">日期</Text>
  201. <Text className="flex-[0.2] text-sm text-center text-[#888888]">內容</Text>
  202. <Text className="flex-[0.2] text-sm text-right text-[#888888]">實際充電量</Text>
  203. <Text className="flex-[0.2] text-sm text-right text-[#888888]">電價</Text>
  204. <Text className="flex-[0.2] text-sm text-right text-[#888888]">金額</Text>
  205. </View>
  206. <View className="border-t border-[#CCCCCC]" />
  207. <FlashList
  208. data={transactionRecord}
  209. renderItem={({ item }) => (
  210. <TransactionRow
  211. date={item.date}
  212. description={item.description}
  213. amount={item.amount}
  214. actual_total_power={item.actual_total_power}
  215. current_price={item.current_price}
  216. />
  217. )}
  218. estimatedItemSize={10}
  219. />
  220. </ScrollView>
  221. </SafeAreaView>
  222. );
  223. };
  224. export default PaymentRecordPageComponent;