paymentRecordPageComponent.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 NormalButton from '../global/normal_button';
  6. import { useEffect, useState } from 'react';
  7. import * as ImagePicker from 'expo-image-picker';
  8. import * as SecureStore from 'expo-secure-store';
  9. import * as FileSystem from 'expo-file-system';
  10. import { authenticationService } from '../../service/authService';
  11. import { FlashList } from '@shopify/flash-list';
  12. import { walletService } from '../../service/walletService';
  13. import { convertToHKTime } from '../../util/lib';
  14. import { set } from 'date-fns';
  15. interface TransactionRecordItem {
  16. date: string;
  17. description: string;
  18. amount: number;
  19. }
  20. const TransactionRow: React.FC<TransactionRecordItem> = ({ date, description, amount }) => (
  21. <View className="flex flex-row w-full my-4">
  22. <Text className="flex-[0.3]">{date}</Text>
  23. <Text className="flex-[0.3] text-center ">{description}</Text>
  24. <Text className="flex-[0.4] text-right">{amount}</Text>
  25. <View className="border-t border-[#CCCCCC]"></View>
  26. </View>
  27. );
  28. const PaymentRecordPageComponent = () => {
  29. const params = useLocalSearchParams();
  30. const walletBalance = params.walletBalance;
  31. const [transactionRecord, setTransactionRecord] = useState<TransactionRecordItem[]>([]);
  32. const [loading, setLoading] = useState(false);
  33. const [loading1, setLoading1] = useState(false);
  34. useEffect(() => {
  35. const fetchTransactionRecord = async () => {
  36. try {
  37. const response = await walletService.getTransactionRecord();
  38. const formattedData: TransactionRecordItem[] = response
  39. .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
  40. .map((item: any) => {
  41. let description;
  42. if (item.type === 'wallet') {
  43. description = item.good_name === 'withdrawl' ? '充電完成-退回餘額' : '充電';
  44. } else {
  45. description = '充值';
  46. }
  47. return {
  48. date: convertToHKTime(item.createdAt).hkDate,
  49. description: description,
  50. amount: item.amount
  51. };
  52. });
  53. setTransactionRecord(formattedData.slice(0, 10));
  54. } catch (error) {
  55. console.log(error);
  56. }
  57. };
  58. fetchTransactionRecord();
  59. }, []);
  60. //fetch transaction record
  61. return (
  62. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  63. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  64. <View style={{ marginTop: 25 }}>
  65. <Pressable
  66. onPress={() => {
  67. if (router.canGoBack()) {
  68. router.back();
  69. } else {
  70. router.replace('/accountMainPage');
  71. }
  72. }}
  73. >
  74. <PreviousPageBlackSvg />
  75. </Pressable>
  76. <Text style={{ fontSize: 45, marginVertical: 25 }}>錢包記錄</Text>
  77. </View>
  78. <View className="">
  79. <ImageBackground
  80. className="flex-col-reverse shadow-lg"
  81. style={{ height: 200 }}
  82. source={require('../../assets/walletCard1.png')}
  83. resizeMode="contain"
  84. >
  85. <View className="mx-[5%] pb-6">
  86. <Text className="text-white text-xl">餘額 (HKD)</Text>
  87. <View className="flex-row items-center justify-between ">
  88. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  89. {loading ? (
  90. <View className="items-center justify-center">
  91. <ActivityIndicator />
  92. </View>
  93. ) : (
  94. <>
  95. <Text>$</Text> {walletBalance}
  96. </>
  97. )}
  98. </Text>
  99. </View>
  100. </View>
  101. </ImageBackground>
  102. </View>
  103. <View className="flex flex-row w-full my-2">
  104. <Text className="flex-[0.3] text-[#888888] ">日期</Text>
  105. <Text className="flex-[0.3] text-center text-[#888888]">內容</Text>
  106. <Text className="flex-[0.4] text-right text-[#888888]">金額</Text>
  107. </View>
  108. <View className="border-t border-[#CCCCCC]" />
  109. <FlashList
  110. data={transactionRecord}
  111. renderItem={({ item }) => (
  112. <TransactionRow date={item.date} description={item.description} amount={item.amount} />
  113. )}
  114. estimatedItemSize={10}
  115. />
  116. </ScrollView>
  117. </SafeAreaView>
  118. );
  119. };
  120. export default PaymentRecordPageComponent;