changePhonePageComponent.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { View, Text, ScrollView, Pressable, StyleSheet } from 'react-native';
  2. import React, { useContext, useEffect, useState } from 'react';
  3. import { SafeAreaView } from 'react-native-safe-area-context';
  4. import { router } from 'expo-router';
  5. import { CrossLogoSvg } from '../global/SVG';
  6. import { AuthContext } from '../../context/AuthProvider';
  7. import NormalInput from '../global/normal_input';
  8. import NormalButton from '../global/normal_button';
  9. import * as SecureStore from 'expo-secure-store';
  10. import { authenticationService } from '../../service/authService';
  11. const ChangePhonePageComponent = () => {
  12. const { user, setUser } = useContext(AuthContext);
  13. const [token, setToken] = useState<string | null>(null);
  14. const [phone, setPhone] = useState<string | null>(null);
  15. const [isLoading, setIsLoading] = useState(false);
  16. const [error, setError] = useState<string | null>(null);
  17. useEffect(() => {
  18. const getToken = async () => {
  19. const storedToken = await SecureStore.getItemAsync('accessToken');
  20. setToken(storedToken);
  21. };
  22. getToken();
  23. }, []);
  24. const handleChangePhone = async () => {
  25. if (!phone) {
  26. setError('請輸入新的電話號碼');
  27. return;
  28. }
  29. if (!token) {
  30. setError('未找到有效的登錄令牌,請重新登錄');
  31. return;
  32. }
  33. setError(null);
  34. setIsLoading(true);
  35. try {
  36. const success = await authenticationService.changePhone(
  37. phone,
  38. token
  39. );
  40. if (success) {
  41. if (user) {
  42. setUser({
  43. ...user,
  44. phone: phone
  45. });
  46. }
  47. router.replace('accountMainPage');
  48. } else {
  49. setError('更新電話號碼失敗,請稍後再試');
  50. }
  51. } catch (error) {
  52. console.error('Error changing phone:', error);
  53. setError('發生錯誤,請稍後再試');
  54. } finally {
  55. setIsLoading(false);
  56. }
  57. };
  58. return (
  59. <SafeAreaView
  60. className="flex-1 bg-white"
  61. edges={['top', 'right', 'left']}
  62. >
  63. <ScrollView className="flex-1 mx-[5%]">
  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. <CrossLogoSvg />
  75. </Pressable>
  76. <Text style={{ fontSize: 45, marginVertical: 25 }}>
  77. 更改電話號碼
  78. </Text>
  79. <Text className="text-xl ">請輸入新的電話號碼</Text>
  80. <View className="py-2">
  81. <NormalInput
  82. placeholder="請輸入新的電話號碼"
  83. onChangeText={(t) => {
  84. setPhone(t);
  85. }}
  86. editable={!isLoading}
  87. />
  88. </View>
  89. <NormalButton
  90. title={
  91. <Text className="text-white">
  92. {isLoading ? '更改中...' : '確認'}
  93. </Text>
  94. }
  95. onPress={handleChangePhone}
  96. disabled={isLoading}
  97. />
  98. {error && (
  99. <Text style={{ color: 'red', marginTop: 10 }}>
  100. {error}
  101. </Text>
  102. )}
  103. </View>
  104. </ScrollView>
  105. </SafeAreaView>
  106. );
  107. };
  108. export default ChangePhonePageComponent;
  109. const styles = StyleSheet.create({
  110. container: {
  111. flex: 1,
  112. marginHorizontal: 20
  113. },
  114. titleText: {
  115. fontSize: 24,
  116. fontWeight: '300'
  117. },
  118. bottomContainer: {
  119. flex: 3,
  120. paddingBottom: 100
  121. },
  122. breakline: {
  123. width: 24,
  124. height: 1,
  125. backgroundColor: '#000000',
  126. marginVertical: 17
  127. },
  128. text: {
  129. fontSize: 18,
  130. paddingBottom: 10
  131. },
  132. hiddenPasswordFields: {
  133. gap: 10,
  134. paddingTop: 10
  135. },
  136. opacityZero: {
  137. opacity: 0
  138. },
  139. opacityFull: {
  140. opacity: 1
  141. },
  142. errorMessage: {
  143. fontSize: 14,
  144. color: '#ff0033',
  145. fontWeight: '400',
  146. marginLeft: 10,
  147. marginTop: 10
  148. },
  149. footer: { color: '#02677D', fontSize: 16, paddingVertical: 10 }
  150. });