|
|
@@ -10,28 +10,9 @@ import { SafeAreaView } from 'react-native-safe-area-context';
|
|
|
import { router } from 'expo-router';
|
|
|
import { CrossLogoSvg } from '../global/SVG';
|
|
|
import { useEffect, useState } from 'react';
|
|
|
-import * as SecureStore from 'expo-secure-store';
|
|
|
import { walletService } from '../../service/walletService';
|
|
|
-const coupons = [
|
|
|
- {
|
|
|
- title: '迎新優惠券',
|
|
|
- price: '999',
|
|
|
- detail: '這是一段有關迎新優惠券的說明',
|
|
|
- date: '至14/3/2025'
|
|
|
- },
|
|
|
- {
|
|
|
- title: '折扣優惠券',
|
|
|
- price: '888',
|
|
|
- detail: '這是另一段有關迎新優惠券的說明',
|
|
|
- date: '至15/4/2025'
|
|
|
- },
|
|
|
- {
|
|
|
- title: '三張優惠券',
|
|
|
- price: '777',
|
|
|
- detail: '這是第三段有關迎新優惠券的說明',
|
|
|
- date: '至16/9/2026'
|
|
|
- }
|
|
|
-];
|
|
|
+
|
|
|
+import { formatCouponDate, formatDate } from '../../util/lib';
|
|
|
|
|
|
export const IndividualCouponComponent = ({
|
|
|
title,
|
|
|
@@ -43,75 +24,81 @@ export const IndividualCouponComponent = ({
|
|
|
price: string;
|
|
|
detail: string;
|
|
|
date: string;
|
|
|
-}) => (
|
|
|
- <View className="bg-[#e7f2f8] h-[124px] rounded-xl flex-row mb-3">
|
|
|
- <View className="bg-white mx-3 my-3 w-[28%] rounded-xl">
|
|
|
- <View className="flex-row justify-center items-center pr-4 pt-4 ">
|
|
|
- <Text className="color-[#02677d] text-2xl pl-2 pr-1">$</Text>
|
|
|
- <Text
|
|
|
- className="color-[#02677d] text-3xl font-bold"
|
|
|
- adjustsFontSizeToFit={true}
|
|
|
- >
|
|
|
- {price}
|
|
|
- </Text>
|
|
|
- </View>
|
|
|
- <View className="items-center justify-center">
|
|
|
- <Text className="text-base mt-1">{title}</Text>
|
|
|
+}) => {
|
|
|
+ return (
|
|
|
+ <View className="bg-[#e7f2f8] h-[124px] rounded-xl flex-row mb-3">
|
|
|
+ <View className="bg-white mx-3 my-3 w-[28%] rounded-xl">
|
|
|
+ <View className="flex-row justify-center items-center pr-4 pt-4 ">
|
|
|
+ <Text className="color-[#02677d] text-2xl pl-2 pr-1">
|
|
|
+ $
|
|
|
+ </Text>
|
|
|
+ <Text
|
|
|
+ className="color-[#02677d] text-3xl font-bold"
|
|
|
+ adjustsFontSizeToFit={true}
|
|
|
+ >
|
|
|
+ {price}
|
|
|
+ </Text>
|
|
|
+ </View>
|
|
|
+ <View className="items-center justify-center">
|
|
|
+ <Text className="text-base mt-1">{title}</Text>
|
|
|
+ </View>
|
|
|
</View>
|
|
|
- </View>
|
|
|
|
|
|
- {/* //dash line */}
|
|
|
- <View style={{ overflow: 'hidden' }}>
|
|
|
- <View
|
|
|
- style={{
|
|
|
- borderStyle: 'dashed',
|
|
|
- borderWidth: 1,
|
|
|
- borderColor: '#CCCCCC',
|
|
|
- margin: -1,
|
|
|
- width: 0,
|
|
|
- marginRight: 0,
|
|
|
- height: '100%'
|
|
|
- }}
|
|
|
- >
|
|
|
- <View style={{ height: 60 }}></View>
|
|
|
+ {/* //dash line */}
|
|
|
+ <View style={{ overflow: 'hidden' }}>
|
|
|
+ <View
|
|
|
+ style={{
|
|
|
+ borderStyle: 'dashed',
|
|
|
+ borderWidth: 1,
|
|
|
+ borderColor: '#CCCCCC',
|
|
|
+ margin: -1,
|
|
|
+ width: 0,
|
|
|
+ marginRight: 0,
|
|
|
+ height: '100%'
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ <View style={{ height: 60 }}></View>
|
|
|
+ </View>
|
|
|
</View>
|
|
|
- </View>
|
|
|
|
|
|
- <View className="flex-col flex-1 m-[5%] justify-center ">
|
|
|
- <Text className="text-lg">{title}</Text>
|
|
|
- <Text className="color-[#888888] text-sm">{detail}</Text>
|
|
|
- <View className="flex-row items-center ">
|
|
|
- <Text className="text-base">有效期 </Text>
|
|
|
- <Text className="text-base font-bold text-[#02677d]">
|
|
|
- {' '}
|
|
|
- {date}
|
|
|
- </Text>
|
|
|
+ <View className="flex-col flex-1 m-[5%] justify-center ">
|
|
|
+ <Text className="text-lg">{title}</Text>
|
|
|
+ <Text className="color-[#888888] text-sm">{detail}</Text>
|
|
|
+ <View className="flex-row items-center ">
|
|
|
+ <Text className="text-base">有效期 </Text>
|
|
|
+ <Text className="text-base font-bold text-[#02677d]">
|
|
|
+ {' '}
|
|
|
+ {date}
|
|
|
+ </Text>
|
|
|
+ </View>
|
|
|
</View>
|
|
|
</View>
|
|
|
- </View>
|
|
|
-);
|
|
|
+ );
|
|
|
+};
|
|
|
|
|
|
const WalletPageComponent = () => {
|
|
|
- const [token, setToken] = useState<string | null>(null);
|
|
|
const [walletBalance, setWalletBalance] = useState<string | null>(null);
|
|
|
- useEffect(() => {
|
|
|
- const getToken = async () => {
|
|
|
- const storedToken = await SecureStore.getItemAsync('accessToken');
|
|
|
- setToken(storedToken);
|
|
|
- };
|
|
|
- getToken();
|
|
|
- }, []);
|
|
|
+ const [loading, setLoading] = useState<boolean>(false);
|
|
|
+ const [coupons, setCoupons] = useState([]);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- const fetchWalletBalance = async () => {
|
|
|
- if (token) {
|
|
|
- await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
|
- const balance = await walletService.getWalletBalance(token);
|
|
|
+ try {
|
|
|
+ setLoading(true);
|
|
|
+
|
|
|
+ const fetchWalletBalanceAndCoupon = async () => {
|
|
|
+ await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
|
+ const balance = await walletService.getWalletBalance();
|
|
|
+ const coupon = await walletService.getCoupon();
|
|
|
+ console.log(JSON.stringify(coupon));
|
|
|
+ setCoupons(coupon);
|
|
|
setWalletBalance(balance);
|
|
|
- }
|
|
|
- };
|
|
|
- fetchWalletBalance();
|
|
|
- }, [token]);
|
|
|
+ setLoading(false);
|
|
|
+ };
|
|
|
+ fetchWalletBalanceAndCoupon();
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
+ }
|
|
|
+ }, []);
|
|
|
|
|
|
const formatMoney = (amount: any) => {
|
|
|
if (typeof amount !== 'number') {
|
|
|
@@ -159,14 +146,13 @@ const WalletPageComponent = () => {
|
|
|
style={{ fontSize: 52 }}
|
|
|
className=" text-white font-bold"
|
|
|
>
|
|
|
- {walletBalance ? (
|
|
|
- formatMoney(walletBalance)
|
|
|
- ) : (
|
|
|
+ {loading ? (
|
|
|
<View className="items-center">
|
|
|
<ActivityIndicator />
|
|
|
</View>
|
|
|
+ ) : (
|
|
|
+ formatMoney(walletBalance)
|
|
|
)}
|
|
|
- {/* Format the wallet balance */}
|
|
|
</Text>
|
|
|
<Pressable
|
|
|
className="rounded-2xl items-center justify-center p-3 px-5 pr-6 "
|
|
|
@@ -199,17 +185,23 @@ const WalletPageComponent = () => {
|
|
|
<Text className="text-xl text-[#888888]">顯示所有</Text>
|
|
|
</Pressable>
|
|
|
</View>
|
|
|
-
|
|
|
<View className="flex-1 flex-col mx-[5%]">
|
|
|
- {coupons.slice(0, 2).map((coupon, index) => (
|
|
|
- <IndividualCouponComponent
|
|
|
- key={index}
|
|
|
- title={coupon.title}
|
|
|
- price={coupon.price}
|
|
|
- detail={coupon.detail}
|
|
|
- date={coupon.date}
|
|
|
- />
|
|
|
- ))}
|
|
|
+ {coupons
|
|
|
+ .filter(
|
|
|
+ (coupon) =>
|
|
|
+ coupon.is_consumned === false &&
|
|
|
+ new Date(coupon.expire_date) > new Date()
|
|
|
+ )
|
|
|
+ .slice(0, 2)
|
|
|
+ .map((coupon, index) => (
|
|
|
+ <IndividualCouponComponent
|
|
|
+ key={index}
|
|
|
+ title={coupon.name}
|
|
|
+ price={coupon.amount}
|
|
|
+ detail={coupon.description}
|
|
|
+ date={formatCouponDate(coupon.expire_date)}
|
|
|
+ />
|
|
|
+ ))}
|
|
|
</View>
|
|
|
</ScrollView>
|
|
|
</SafeAreaView>
|