changeNamePageComponent.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { View, Text, ScrollView, Pressable } 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 { authenticationService } from '../../service/authService';
  10. import * as SecureStore from 'expo-secure-store';
  11. const ChangeNamePageComponent = () => {
  12. const { user, setUser } = useContext(AuthContext);
  13. const [token, setToken] = useState<string | null>(null);
  14. const [name, setName] = 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 handleChangeName = async () => {
  25. if (!name) {
  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.changeName(name, token);
  37. if (success) {
  38. if (user) {
  39. setUser({
  40. ...user,
  41. nickname: name
  42. });
  43. }
  44. router.replace('accountMainPage');
  45. } else {
  46. setError('更新暱稱失敗,請稍後再試');
  47. }
  48. } catch (error) {
  49. console.error('Error changing name:', error);
  50. setError('發生錯誤,請稍後再試');
  51. } finally {
  52. setIsLoading(false);
  53. }
  54. };
  55. return (
  56. <SafeAreaView
  57. className="flex-1 bg-white"
  58. edges={['top', 'right', 'left']}
  59. >
  60. <ScrollView className="flex-1 mx-[5%]">
  61. <View style={{ marginTop: 25 }}>
  62. <Pressable
  63. onPress={() => {
  64. if (router.canGoBack()) {
  65. router.back();
  66. } else {
  67. router.replace('/accountMainPage');
  68. }
  69. }}
  70. >
  71. <CrossLogoSvg />
  72. </Pressable>
  73. <Text style={{ fontSize: 45, marginVertical: 25 }}>
  74. 更改暱稱
  75. </Text>
  76. <Text className="text-xl ">請輸入新名稱</Text>
  77. <View className="py-2">
  78. <NormalInput
  79. placeholder={user?.nickname}
  80. onChangeText={(t) => setName(t)}
  81. editable={!isLoading}
  82. />
  83. </View>
  84. <NormalButton
  85. title={
  86. <Text className="text-white">
  87. {isLoading ? '更改中...' : '確認'}
  88. </Text>
  89. }
  90. disabled={isLoading}
  91. onPress={handleChangeName}
  92. />
  93. </View>
  94. </ScrollView>
  95. </SafeAreaView>
  96. );
  97. };
  98. export default ChangeNamePageComponent;