paymentRecordPageComponent.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 === 'manual_refund' ||
  102. item.goods_name === 'manual_deduction' ||
  103. (item.goods_name === 'Walk In' && Number(item.actual_total_power) >= 1)
  104. )
  105. .map((item: any) => {
  106. let description;
  107. if (item.type === 'wallet') {
  108. switch (item.goods_name) {
  109. case 'withdrawl':
  110. description = '充電完成-退回餘額';
  111. break;
  112. case 'Penalty':
  113. description = '繳付罰款';
  114. break;
  115. case 'Book a connector':
  116. description = '預約充電概要';
  117. break;
  118. case 'Walk In':
  119. description = '充電概要';
  120. break;
  121. case 'manual_refund':
  122. description = '系統退款';
  123. break;
  124. case 'manual_deduction':
  125. description = '系統扣款';
  126. break;
  127. default:
  128. description = '充電';
  129. }
  130. } else if (item.type === 'qfpay') {
  131. description = '錢包增值';
  132. }
  133. return {
  134. date: convertToHKTime(item.createdAt).hkDate,
  135. description: description,
  136. amount:
  137. item.type === 'qfpay' ||
  138. item.goods_name === 'Penalty' ||
  139. item.goods_name === 'manual_refund' ||
  140. item.goods_name === 'manual_deduction'
  141. ? item.amount
  142. : item.current_price && item.actual_total_power
  143. ? (item.current_price * item.actual_total_power).toFixed(1)
  144. : '-',
  145. actual_total_power:
  146. item.goods_name === 'manual_refund' || item.goods_name === 'manual_deduction'
  147. ? '-'
  148. : item.actual_total_power !== undefined &&
  149. item.actual_total_power !== null &&
  150. item.actual_total_power !== '' &&
  151. !isNaN(Number(item.actual_total_power))
  152. ? item.actual_total_power
  153. : '-',
  154. current_price:
  155. item.type === 'qfpay' ||
  156. item.goods_name === 'Penalty' ||
  157. item.goods_name === 'manual_refund' ||
  158. item.goods_name === 'manual_deduction'
  159. ? '-'
  160. : item.current_price
  161. };
  162. });
  163. setTransactionRecord(formattedData.slice(0, 10));
  164. } catch (error) {
  165. console.log(error);
  166. }
  167. };
  168. fetchTransactionRecord();
  169. }, []);
  170. return (
  171. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  172. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  173. <View style={{ marginTop: 25 }}>
  174. <Pressable
  175. onPress={() => {
  176. if (router.canGoBack()) {
  177. router.back();
  178. } else {
  179. router.replace('/accountMainPage');
  180. }
  181. }}
  182. >
  183. <PreviousPageBlackSvg />
  184. </Pressable>
  185. <Text style={{ fontSize: 45, marginVertical: 25 }}>订单記錄</Text>
  186. </View>
  187. <View className="">
  188. <ImageBackground
  189. className="flex-col-reverse shadow-lg"
  190. style={{ height: 200 }}
  191. source={require('../../assets/walletCard1.png')}
  192. resizeMode="contain"
  193. >
  194. <View className="mx-[5%] pb-6">
  195. <Text className="text-white text-xl">餘額 (HKD)</Text>
  196. <View className="flex-row items-center justify-between ">
  197. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  198. {loading ? (
  199. <View className="items-center justify-center">
  200. <ActivityIndicator />
  201. </View>
  202. ) : (
  203. <>
  204. <Text>$</Text> {walletBalance}
  205. </>
  206. )}
  207. </Text>
  208. </View>
  209. </View>
  210. </ImageBackground>
  211. </View>
  212. <View className="flex flex-row w-full py-2 border-b border-[#CCCCCC]">
  213. <Text className="flex-[0.2] text-sm text-[#888888]">日期</Text>
  214. <Text className="flex-[0.2] text-sm text-center text-[#888888]">內容</Text>
  215. <Text className="flex-[0.2] text-sm text-right text-[#888888]">實際充電量</Text>
  216. <Text className="flex-[0.2] text-sm text-right text-[#888888]">電價</Text>
  217. <Text className="flex-[0.2] text-sm text-right text-[#888888]">金額</Text>
  218. </View>
  219. <View className="border-t border-[#CCCCCC]" />
  220. <FlashList
  221. data={transactionRecord}
  222. renderItem={({ item }) => (
  223. <TransactionRow
  224. date={item.date}
  225. description={item.description}
  226. amount={item.amount}
  227. actual_total_power={item.actual_total_power}
  228. current_price={item.current_price}
  229. />
  230. )}
  231. estimatedItemSize={10}
  232. />
  233. </ScrollView>
  234. </SafeAreaView>
  235. );
  236. };
  237. export default PaymentRecordPageComponent;