paymentRecordPageComponent.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. import { useTranslation } from '../../util/hooks/useTranslation';
  11. interface TransactionRecordItem {
  12. date: string;
  13. description: string;
  14. amount: number;
  15. actual_total_power: string | number;
  16. current_price?: number;
  17. }
  18. const TransactionRow: React.FC<TransactionRecordItem> = ({
  19. date,
  20. description,
  21. amount,
  22. actual_total_power,
  23. current_price
  24. }) => {
  25. const { t } = useTranslation(); // 使用翻译钩子
  26. return (
  27. <View className="flex flex-row w-full py-4 border-b border-[#CCCCCC]">
  28. <Text className="flex-[0.2] text-sm">{date}</Text>
  29. <Text className="flex-[0.2] text-center text-sm">{t(`payment_record.descriptions.${description}`) || description}</Text>
  30. <Text className="flex-[0.2] text-sm text-right ">
  31. {actual_total_power !== '-' ? Number(actual_total_power).toFixed(1) : actual_total_power}
  32. </Text>
  33. <Text className="flex-[0.2] text-sm text-right">{current_price ? `${current_price}` : '-'}</Text>
  34. <Text className="flex-[0.2] text-sm text-right">${amount}</Text>
  35. </View>
  36. );
  37. };
  38. const PaymentRecordPageComponent = () => {
  39. const { t } = useTranslation(); // 使用翻译钩子
  40. const params = useLocalSearchParams();
  41. const walletBalance = params.walletBalance;
  42. const [transactionRecord, setTransactionRecord] = useState<TransactionRecordItem[]>([]);
  43. const [loading, setLoading] = useState(false);
  44. useEffect(() => {
  45. const fetchTransactionRecord = async () => {
  46. try {
  47. const response = await walletService.getTransactionRecord();
  48. const formattedData: TransactionRecordItem[] = response
  49. .sort((a: any, b: any) => new Date(b.createdAt) - new Date(a.createdAt))
  50. .filter(
  51. (item: any) =>
  52. item.type !== 'wallet' ||
  53. item.goods_name === 'Penalty' ||
  54. item.goods_name === 'manual_refund' ||
  55. item.goods_name === 'manual_deduction' ||
  56. (item.goods_name === 'Walk In' && Number(item.actual_total_power) >= 1)
  57. )
  58. .map((item: any) => {
  59. let description;
  60. if (item.type === 'wallet') {
  61. switch (item.goods_name) {
  62. case 'withdrawl':
  63. description = 'withdraw_balance';
  64. break;
  65. case 'Penalty':
  66. description = 'pay_penalty';
  67. break;
  68. case 'Book a connector':
  69. description = 'booking_summary';
  70. break;
  71. case 'Walk In':
  72. description = 'charging_summary';
  73. break;
  74. case 'manual_refund':
  75. description = 'system_refund';
  76. break;
  77. case 'manual_deduction':
  78. description = 'system_deduction';
  79. break;
  80. default:
  81. description = 'charging';
  82. }
  83. } else if (item.type === 'qfpay') {
  84. description = 'wallet_top_up';
  85. }
  86. return {
  87. date: convertToHKTime(item.createdAt).hkDate,
  88. description: description,
  89. amount: (() => {
  90. if (item.goods_name === 'Walk In') {
  91. return item.amountNew ? (item.amountNew).toFixed(1) : '0';
  92. } else {
  93. if (item.type === 'qfpay' ||
  94. item.goods_name === 'Penalty' ||
  95. item.goods_name === 'manual_refund' ||
  96. item.goods_name === 'manual_deduction') {
  97. return item.amount;
  98. } else {
  99. return item.current_price && item.actual_total_power
  100. ? (item.current_price * item.actual_total_power).toFixed(1)
  101. : '-';
  102. }
  103. }
  104. })(),
  105. actual_total_power:
  106. item.goods_name === 'manual_refund' || item.goods_name === 'manual_deduction'
  107. ? '-'
  108. : item.actual_total_power !== undefined &&
  109. item.actual_total_power !== null &&
  110. item.actual_total_power !== '' &&
  111. !isNaN(Number(item.actual_total_power))
  112. ? item.actual_total_power
  113. : '-',
  114. current_price:
  115. item.type === 'qfpay' ||
  116. item.goods_name === 'Penalty' ||
  117. item.goods_name === 'manual_refund' ||
  118. item.goods_name === 'manual_deduction'
  119. ? '-'
  120. : item.current_price
  121. };
  122. });
  123. setTransactionRecord(formattedData.slice(0, 10));
  124. console.log('ddd', formattedData);
  125. } catch (error) {
  126. console.error(t('payment_record.error_fetching_records'), error);
  127. }
  128. };
  129. fetchTransactionRecord();
  130. }, []);
  131. return (
  132. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  133. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  134. <View style={{ marginTop: 25 }}>
  135. <Pressable
  136. onPress={() => {
  137. if (router.canGoBack()) {
  138. router.back();
  139. } else {
  140. router.replace('/accountMainPage');
  141. }
  142. }}
  143. >
  144. <PreviousPageBlackSvg />
  145. </Pressable>
  146. <Text style={{ fontSize: 45, marginVertical: 25 }}>{t('payment_record.title')}</Text>
  147. </View>
  148. <View className="">
  149. <ImageBackground
  150. className="flex-col-reverse shadow-lg"
  151. style={{ height: 200 }}
  152. source={require('../../assets/walletCard1.png')}
  153. resizeMode="contain"
  154. >
  155. <View className="mx-[5%] pb-6">
  156. <Text className="text-white text-xl">{t('payment_record.balance_label')}</Text>
  157. <View className="flex-row items-center justify-between ">
  158. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  159. {loading ? (
  160. <View className="items-center justify-center">
  161. <ActivityIndicator />
  162. </View>
  163. ) : (
  164. <>
  165. <Text>$</Text> {walletBalance}
  166. </>
  167. )}
  168. </Text>
  169. </View>
  170. </View>
  171. </ImageBackground>
  172. </View>
  173. <View className="flex flex-row w-full py-2 border-b border-[#CCCCCC]">
  174. <Text className="flex-[0.2] text-sm text-[#888888]">{t('payment_record.headers.date')}</Text>
  175. <Text className="flex-[0.2] text-sm text-center text-[#888888]">{t('payment_record.headers.content')}</Text>
  176. <Text className="flex-[0.2] text-sm text-right text-[#888888]">{t('payment_record.headers.actual_power')}</Text>
  177. <Text className="flex-[0.2] text-sm text-right text-[#888888]">{t('payment_record.headers.electricity_price')}</Text>
  178. <Text className="flex-[0.2] text-sm text-right text-[#888888]">{t('payment_record.headers.amount')}</Text>
  179. </View>
  180. <View className="border-t border-[#CCCCCC]" />
  181. <FlashList
  182. data={transactionRecord}
  183. renderItem={({ item }) => (
  184. <TransactionRow
  185. date={item.date}
  186. description={item.description}
  187. amount={item.amount}
  188. actual_total_power={item.actual_total_power}
  189. current_price={item.current_price}
  190. />
  191. )}
  192. />
  193. </ScrollView>
  194. </SafeAreaView>
  195. );
  196. };
  197. export default PaymentRecordPageComponent;