paymentRecordPageComponent.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. const formattedData: TransactionRecordItem[] = response
  45. .sort((a: any, b: any) => new Date(b.createdAt) - new Date(a.createdAt))
  46. .filter(
  47. (item: any) =>
  48. item.type !== 'wallet' ||
  49. item.goods_name === 'Penalty' ||
  50. item.goods_name === 'manual_refund' ||
  51. item.goods_name === 'manual_deduction' ||
  52. (item.goods_name === 'Walk In' && Number(item.actual_total_power) >= 1)
  53. )
  54. .map((item: any) => {
  55. let description;
  56. if (item.type === 'wallet') {
  57. switch (item.goods_name) {
  58. case 'withdrawl':
  59. description = '充電完成-退回餘額';
  60. break;
  61. case 'Penalty':
  62. description = '繳付罰款';
  63. break;
  64. case 'Book a connector':
  65. description = '預約充電概要';
  66. break;
  67. case 'Walk In':
  68. description = '充電概要';
  69. break;
  70. case 'manual_refund':
  71. description = '系統退款';
  72. break;
  73. case 'manual_deduction':
  74. description = '系統扣款';
  75. break;
  76. default:
  77. description = '充電';
  78. }
  79. } else if (item.type === 'qfpay') {
  80. description = '錢包增值';
  81. }
  82. return {
  83. date: convertToHKTime(item.createdAt).hkDate,
  84. description: description,
  85. amount: (() => {
  86. if (item.goods_name === 'Walk In') {
  87. return item.amountNew ? (item.amountNew).toFixed(1) : '0';
  88. } else {
  89. if (item.type === 'qfpay' ||
  90. item.goods_name === 'Penalty' ||
  91. item.goods_name === 'manual_refund' ||
  92. item.goods_name === 'manual_deduction') {
  93. return item.amount;
  94. } else {
  95. return item.current_price && item.actual_total_power
  96. ? (item.current_price * item.actual_total_power).toFixed(1)
  97. : '-';
  98. }
  99. }
  100. })(),
  101. actual_total_power:
  102. item.goods_name === 'manual_refund' || item.goods_name === 'manual_deduction'
  103. ? '-'
  104. : item.actual_total_power !== undefined &&
  105. item.actual_total_power !== null &&
  106. item.actual_total_power !== '' &&
  107. !isNaN(Number(item.actual_total_power))
  108. ? item.actual_total_power
  109. : '-',
  110. current_price:
  111. item.type === 'qfpay' ||
  112. item.goods_name === 'Penalty' ||
  113. item.goods_name === 'manual_refund' ||
  114. item.goods_name === 'manual_deduction'
  115. ? '-'
  116. : item.current_price
  117. };
  118. });
  119. setTransactionRecord(formattedData.slice(0, 10));
  120. console.log('ddd', formattedData);
  121. } catch (error) {}
  122. };
  123. fetchTransactionRecord();
  124. }, []);
  125. return (
  126. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  127. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  128. <View style={{ marginTop: 25 }}>
  129. <Pressable
  130. onPress={() => {
  131. if (router.canGoBack()) {
  132. router.back();
  133. } else {
  134. router.replace('/accountMainPage');
  135. }
  136. }}
  137. >
  138. <PreviousPageBlackSvg />
  139. </Pressable>
  140. <Text style={{ fontSize: 45, marginVertical: 25 }}>訂單記錄</Text>
  141. </View>
  142. <View className="">
  143. <ImageBackground
  144. className="flex-col-reverse shadow-lg"
  145. style={{ height: 200 }}
  146. source={require('../../assets/walletCard1.png')}
  147. resizeMode="contain"
  148. >
  149. <View className="mx-[5%] pb-6">
  150. <Text className="text-white text-xl">餘額 (HKD)</Text>
  151. <View className="flex-row items-center justify-between ">
  152. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  153. {loading ? (
  154. <View className="items-center justify-center">
  155. <ActivityIndicator />
  156. </View>
  157. ) : (
  158. <>
  159. <Text>$</Text> {walletBalance}
  160. </>
  161. )}
  162. </Text>
  163. </View>
  164. </View>
  165. </ImageBackground>
  166. </View>
  167. <View className="flex flex-row w-full py-2 border-b border-[#CCCCCC]">
  168. <Text className="flex-[0.2] text-sm text-[#888888]">日期</Text>
  169. <Text className="flex-[0.2] text-sm text-center text-[#888888]">內容</Text>
  170. <Text className="flex-[0.2] text-sm text-right text-[#888888]">實際充電量</Text>
  171. <Text className="flex-[0.2] text-sm text-right text-[#888888]">電價</Text>
  172. <Text className="flex-[0.2] text-sm text-right text-[#888888]">金額</Text>
  173. </View>
  174. <View className="border-t border-[#CCCCCC]" />
  175. <FlashList
  176. data={transactionRecord}
  177. renderItem={({ item }) => (
  178. <TransactionRow
  179. date={item.date}
  180. description={item.description}
  181. amount={item.amount}
  182. actual_total_power={item.actual_total_power}
  183. current_price={item.current_price}
  184. />
  185. )}
  186. />
  187. </ScrollView>
  188. </SafeAreaView>
  189. );
  190. };
  191. export default PaymentRecordPageComponent;