瀏覽代碼

perf: 邮箱可以修改

kuns 2 月之前
父節點
當前提交
4e4e1b58cc

+ 12 - 0
app/(auth)/(tabs)/(account)/changeEmailPage.tsx

@@ -0,0 +1,12 @@
+import { View, Text } from 'react-native';
+import ChangeEmailPageComponent from '../../../../component/accountPages/changeEmailPageComponent';
+
+const ChangeEmailPage = () => {
+    return (
+        <View className='flex-1'>
+           <ChangeEmailPageComponent />  
+        </View>
+    );
+};
+
+export default ChangeEmailPage;

+ 2 - 2
component/accountPages/accountSettingPageComponent.tsx

@@ -49,13 +49,13 @@ const AccountSettingPageComponent = () => {
                 </View>
                 <View>
                     <View className="flex-col">
-                        <Pressable onPress={() => console.log('abc')}>
+                        <Pressable onPress={() => router.push('changeEmailPage')}>
                             <View className="flex-row items-center justify-between">
                                 <View className="flex-col py-4">
                                     <Text className="text-lg pb-1">電郵地址</Text>
                                     <Text style={{ color: '#555555' }}>{user?.email}</Text>
                                 </View>
-                                {/* <RightArrowIconSvg /> */}
+                                <RightArrowIconSvg />
                             </View>
                         </Pressable>
                         <View className="h-0.5  bg-[#f4f4f4] " />

+ 101 - 0
component/accountPages/changeEmailPageComponent.tsx

@@ -0,0 +1,101 @@
+import { View, Text, ScrollView, Pressable } from 'react-native';
+import React, { useContext, useEffect, useState } from 'react';
+import { SafeAreaView } from 'react-native-safe-area-context';
+import { router } from 'expo-router';
+import { CrossLogoSvg } from '../global/SVG';
+import { AuthContext } from '../../context/AuthProvider';
+import NormalInput from '../global/normal_input';
+import NormalButton from '../global/normal_button';
+import { authenticationService } from '../../service/authService';
+import * as SecureStore from 'expo-secure-store';
+const ChangeEmailPageComponent = () => {
+    const { user, setUser } = useContext(AuthContext);
+    const [token, setToken] = useState<string | null>(null);
+    const [email, setName] = useState<string | null>(null);
+    const [isLoading, setIsLoading] = useState(false);
+    const [error, setError] = useState<string | null>(null);
+    useEffect(() => {
+        const getToken = async () => {
+            const storedToken = await SecureStore.getItemAsync('accessToken');
+            setToken(storedToken);
+        };
+        getToken();
+    }, []);
+
+    const handleChangeEmail= async () => {
+        if (!email) {
+            setError('請輸入新的郵箱');
+            return;
+        }
+        if (!token) {
+            setError('未找到有效的登錄令牌,請重新登錄');
+            return;
+        }
+        setError(null);
+        setIsLoading(true);
+        try {
+            const success = await authenticationService.changeEmail(email, token);
+            if (success) {
+                if (user) {
+                    setUser({
+                        ...user,
+                        email: email
+                    });
+                }
+                router.replace('accountMainPage');
+            } else {
+                setError('更新郵箱失敗,請稍後再試');
+            }
+        } catch (error) {
+            console.error('Error changing name:', error);
+            setError('發生錯誤,請稍後再試');
+        } finally {
+            setIsLoading(false);
+        }
+    };
+
+    return (
+        <SafeAreaView
+            className="flex-1 bg-white"
+            edges={['top', 'right', 'left']}
+        >
+            <ScrollView className="flex-1 mx-[5%]">
+                <View style={{ marginTop: 25 }}>
+                    <Pressable
+                        onPress={() => {
+                            if (router.canGoBack()) {
+                                router.back();
+                            } else {
+                                router.replace('/accountMainPage');
+                            }
+                        }}
+                    >
+                        <CrossLogoSvg />
+                    </Pressable>
+                    <Text style={{ fontSize: 45, marginVertical: 25 }}>
+                        更改郵箱
+                    </Text>
+                    <Text className="text-xl ">請輸入新郵箱</Text>
+                    <View className="py-2">
+                        <NormalInput
+                            placeholder={user?.email}
+                            onChangeText={(t) => setName(t)}
+                            editable={!isLoading}
+                        />
+                    </View>
+                    <NormalButton
+                        title={
+                            <Text className="text-white">
+                                {isLoading ? '更改中...' : '確認'}
+                            </Text>
+                        }
+                        disabled={isLoading}
+                        onPress={handleChangeEmail}
+                    />
+                </View>
+            </ScrollView>
+        </SafeAreaView>
+    );
+};
+
+export default ChangeEmailPageComponent;

+ 1 - 2
component/homePage/homePage.tsx

@@ -171,7 +171,6 @@ const HomePage: React.FC<HomePageProps> = () => {
             };
 
             fetchData();
-            console.log('iiiiiii');
             return () => {
                 isActive = false;
             };
@@ -203,7 +202,7 @@ const HomePage: React.FC<HomePageProps> = () => {
                     animationType="fade"
                     transparent={true}
                     visible={showOnboarding}
-                    onRequestClose={() => setShowOnboarding(false)}
+                    onRequestClose={() => { setShowOnboarding(false)}}
                 >
                     <Pressable
                         className="flex-1 bg-black/50 items-center justify-center"

+ 22 - 1
service/authService.tsx

@@ -318,7 +318,28 @@ class AuthenticationService {
         }
     }
 
-    //BELOW CODES RELATE TO "changing account info (such as gender, name)"
+    async changeEmail(email: string | null, token: string | null): Promise<boolean> {
+        try {
+            const res = await axios.put(
+                `${this.apiUrl}/clients/customer`,
+                { email: email },
+                {
+                    headers: {
+                        Authorization: `Bearer ${token}`
+                    }
+                }
+            );
+            console.log('Change Name Successfully!');
+            return true;
+        } catch (error) {
+            if (axios.isAxiosError(error)) {
+                console.error('Error changing name:', error.response?.data?.message);
+            } else {
+                console.error('An unexpected error occurred:', error);
+            }
+            return false;
+        }
+    }
     //BELOW CODES RELATE TO "changing account info (such as gender, name)"
     async changeName(name: string | null, token: string | null): Promise<boolean> {
         try {

+ 0 - 2
service/chargeStationService.tsx

@@ -479,7 +479,6 @@ class ChargeStationService {
 
     async fetchReservationHistories() {
         const aa = await SecureStore.getItemAsync('accessToken')
-        console.log('iiiiiiiqqqqq', aa)
         try {
             const response = await axios.get(`${this.apiUrl}/clients/reservation/all`, {
                 headers: {
@@ -492,7 +491,6 @@ class ChargeStationService {
                 console.log('invalid response');
             }
         } catch (error) {
-            console.log('ddqqqqqwwww', error);
         }
     }