paymentRecordPageComponent.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. return {
  42. date: convertToHKTime(item.createdAt).hkDate,
  43. description: item.type === 'wallet' ? '充電' : '充值',
  44. amount: item.amount
  45. };
  46. });
  47. setTransactionRecord(formattedData.slice(0, 10));
  48. } catch (error) {
  49. console.log(error);
  50. }
  51. };
  52. fetchTransactionRecord();
  53. }, []);
  54. //fetch transaction record
  55. return (
  56. <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
  57. <ScrollView style={{ flex: 1 }} className="mx-[5%]" showsVerticalScrollIndicator={false}>
  58. <View style={{ marginTop: 25 }}>
  59. <Pressable
  60. onPress={() => {
  61. if (router.canGoBack()) {
  62. router.back();
  63. } else {
  64. router.replace('/accountMainPage');
  65. }
  66. }}
  67. >
  68. <PreviousPageBlackSvg />
  69. </Pressable>
  70. <Text style={{ fontSize: 45, marginVertical: 25 }}>錢包記錄</Text>
  71. </View>
  72. <View className="">
  73. <ImageBackground
  74. className="flex-col-reverse shadow-lg"
  75. style={{ height: 200 }}
  76. source={require('../../assets/walletCard1.png')}
  77. resizeMode="contain"
  78. >
  79. <View className="mx-[5%] pb-6">
  80. <Text className="text-white text-xl">餘額 (HKD)</Text>
  81. <View className="flex-row items-center justify-between ">
  82. <Text style={{ fontSize: 52 }} className=" text-white font-bold">
  83. {loading ? (
  84. <View className="items-center justify-center">
  85. <ActivityIndicator />
  86. </View>
  87. ) : (
  88. <>
  89. <Text>$</Text> {walletBalance}
  90. </>
  91. )}
  92. </Text>
  93. </View>
  94. </View>
  95. </ImageBackground>
  96. </View>
  97. <View className="flex flex-row w-full my-2">
  98. <Text className="flex-[0.3] text-[#888888] ">日期</Text>
  99. <Text className="flex-[0.3] text-center text-[#888888]">內容</Text>
  100. <Text className="flex-[0.4] text-right text-[#888888]">金額</Text>
  101. </View>
  102. <View className="border-t border-[#CCCCCC]" />
  103. <FlashList
  104. data={transactionRecord}
  105. renderItem={({ item }) => (
  106. <TransactionRow date={item.date} description={item.description} amount={item.amount} />
  107. )}
  108. estimatedItemSize={10}
  109. />
  110. </ScrollView>
  111. </SafeAreaView>
  112. );
  113. };
  114. export default PaymentRecordPageComponent;