1
- import { View , Text , TextInput , TouchableOpacity } from "react-native" ;
2
- import { Button } from "react-native-paper" ;
1
+ import { View , Text , Linking } from "react-native" ;
2
+ import { TextInput , Button , ActivityIndicator , MD2Colors } from "react-native-paper" ;
3
3
import { useEffect , useState } from "react" ;
4
4
import * as SecureStore from 'expo-secure-store' ;
5
5
import axiosInstance from "../server/axios" ;
@@ -10,6 +10,9 @@ import { router } from "expo-router";
10
10
export default function ChangeRegisterData ( ) {
11
11
const { signOut } = useSession ( ) ;
12
12
13
+ const [ loading , setLoading ] = useState ( false ) ;
14
+ const [ loading2 , setLoading2 ] = useState ( false ) ;
15
+
13
16
const getName = async ( ) => {
14
17
const token = await SecureStore . getItemAsync ( 'session' ) ;
15
18
console . log ( token ) ;
@@ -41,75 +44,81 @@ export default function ChangeRegisterData(){
41
44
const [ confirmarNovaSenha , setConfirmarNovaSenha ] = useState ( "" ) ;
42
45
43
46
const changeRegisterData = async ( ) => {
44
- const token = await SecureStore . getItemAsync ( 'session' ) ;
45
- const config = {
46
- headers : {
47
- Authorization : "Bearer " + token
47
+ setLoading ( true ) ;
48
+ try {
49
+ const token = await SecureStore . getItemAsync ( 'session' ) ;
50
+ const config = {
51
+ headers : {
52
+ Authorization : "Bearer " + token
53
+ }
48
54
}
49
- }
50
- const requestData = {
51
- name : nomeNovo ,
52
- email : emailNovo ,
53
- senhaAntiga : senhaAntiga ,
54
- senhaNova : novaSenha ,
55
- }
56
- if ( emailNovo || emailNovo != "" ) {
57
- await axiosInstance . patch ( "/api/users/patch/" + id , requestData , config )
58
- router . push ( {
59
- pathname : "/EmailChangeVerification" ,
60
- params : {
61
- newEmail : emailNovo
55
+ const requestData = {
56
+ name : nomeNovo ,
57
+ email : emailNovo ,
58
+ senhaAntiga : senhaAntiga ,
59
+ senhaNova : novaSenha ,
60
+ }
61
+ if ( emailNovo || emailNovo != "" ) {
62
+ await axiosInstance . patch ( "/api/users/patch/" + id , requestData , config )
63
+ router . push ( {
64
+ pathname : "/EmailChangeVerification" ,
65
+ params : {
66
+ newEmail : emailNovo
67
+ }
68
+ } ) ;
69
+ } else {
70
+ if ( novaSenha != confirmarNovaSenha ) {
71
+ alert ( "As senhas não coincidem!" )
72
+ } if ( nomeNovo == "" || emailNovo == "" || senhaAntiga == "" || novaSenha == "" || confirmarNovaSenha == "" ) {
73
+ alert ( "Preencha algum campos!" )
62
74
}
63
- } ) ;
64
- } else {
65
- if ( novaSenha != confirmarNovaSenha ) {
66
- alert ( "As senhas não coincidem!" )
67
- } else {
68
- if ( nomeNovo ) {
69
- await axiosInstance . patch ( "/api/users/patch/" + id , requestData , config ) . then ( ( response ) => {
70
- alert ( "Dados alterados com sucesso!" )
71
- signOut ( )
72
- } ) . catch ( ( error ) => {
73
- console . log ( error )
74
- } )
75
- } if ( ! nomeNovo ) {
75
+ else {
76
+ if ( nomeNovo ) {
76
77
await axiosInstance . patch ( "/api/users/patch/" + id , requestData , config ) . then ( ( response ) => {
77
- if ( response . status == 500 ) {
78
- alert ( "Senha antiga incorreta!" )
79
- } else {
80
- alert ( "Dados alterados com sucesso!" )
81
- signOut ( )
82
- }
78
+ alert ( "Dados alterados com sucesso!" )
79
+ signOut ( )
83
80
} ) . catch ( ( error ) => {
84
- if ( error . response . status == 500 ) {
85
- alert ( "Senha antiga incorreta!" )
86
- }
81
+ console . log ( error )
87
82
} )
83
+ } if ( ! nomeNovo ) {
84
+ await axiosInstance . patch ( "/api/users/patch/" + id , requestData , config ) . then ( ( response ) => {
85
+ if ( response . status == 500 ) {
86
+ alert ( "Senha antiga incorreta!" )
87
+ } else {
88
+ alert ( "Dados alterados com sucesso!" )
89
+ signOut ( )
90
+ }
91
+ } ) . catch ( ( error ) => {
92
+ if ( error . response . status == 500 ) {
93
+ alert ( "Senha antiga incorreta!" )
94
+ }
95
+ } )
96
+ }
88
97
}
89
98
}
99
+ } catch ( error ) {
100
+ console . log ( error )
101
+ } finally {
102
+ setLoading ( false ) ;
90
103
}
91
104
}
92
105
93
106
return (
94
- < View className = "pt-12 h-full bg-white" >
95
- < Text > Nome: { nome } </ Text >
96
- < TextInput className = "border-2 border-gray-500 rounded-lg w-60 h-10" value = { nomeNovo } onChangeText = { setNomeNovo } > </ TextInput >
97
- < Text > Email: { email } </ Text >
98
- < TextInput className = "border-2 border-gray-500 rounded-lg w-60 h-10" value = { emailNovo } onChangeText = { setEmailNovo } > </ TextInput >
99
- < Text > Senha antiga</ Text >
100
- < TextInput value = { senhaAntiga } onChangeText = { setSenhaAntiga } className = "border-2 border-gray-500 rounded-lg w-60 h-10" > </ TextInput >
101
- < Text > Nova senha</ Text >
102
- < TextInput value = { novaSenha } onChangeText = { setNovaSenha } className = "border-2 border-gray-500 rounded-lg w-60 h-10" > </ TextInput >
103
- < Text > Confirmar nova senha</ Text >
104
- < TextInput value = { confirmarNovaSenha } onChangeText = { setConfirmarNovaSenha } className = "border-2 border-gray-500 rounded-lg w-60 h-10" > </ TextInput >
105
- < TouchableOpacity onPress = { changeRegisterData } className = "bg-blue-500 p-3 rounded-lg w-60" >
106
- < Text className = "text-center" > Alterar dados</ Text >
107
- </ TouchableOpacity >
108
- < TouchableOpacity className = "bg-red-500 p-3 rounded-lg w-60" >
109
- < Text className = "text-center" > Excluir conta</ Text >
110
- </ TouchableOpacity >
111
- < Text > Ao alterar o E-mail, use apenas E-mails que consigam ser acessados, para fazer uma nova verificação.</ Text >
112
- < Text > Em caso de problemas, contate o suporte: mercadobmxbr@gmail.com</ Text >
107
+ < View className = "pt-12 h-full bg-white flex justify-center items-center" >
108
+ < Text className = "mb-4 text-3xl" > Alteração de cadastro</ Text >
109
+ < TextInput label = { "Nome atual: " + nome } mode = "outlined" className = "text-black text-sm w-80 h-10 mb-2" value = { nomeNovo } onChangeText = { setNomeNovo } > </ TextInput >
110
+ < TextInput label = { "Email atual: " + email } mode = "outlined" className = "text-black text-sm w-80 h-10 mb-2" value = { emailNovo } onChangeText = { setEmailNovo } > </ TextInput >
111
+ < TextInput label = "Senha antiga" mode = "outlined" value = { senhaAntiga } onChangeText = { setSenhaAntiga } className = "text-black text-sm w-80 h-10 mb-2" > </ TextInput >
112
+ < TextInput label = "Nova senha" mode = "outlined" value = { novaSenha } onChangeText = { setNovaSenha } className = "text-black text-sm w-80 h-10 mb-2" > </ TextInput >
113
+ < TextInput label = "Confirmar nova senha" mode = "outlined" value = { confirmarNovaSenha } onChangeText = { setConfirmarNovaSenha } className = "text-black text-sm w-80 h-10" > </ TextInput >
114
+ < View className = "flex gap-2 my-4" >
115
+ { loading ? < ActivityIndicator animating = { true } color = { MD2Colors . green500 } size = { 40 } /> : < Button onPress = { changeRegisterData } mode = 'contained' className = "bg-green-500 w-40" > Alterar Dados</ Button > }
116
+ < Button onPress = { ( ) => { router . push ( {
117
+ pathname : "/DeleteAccount"
118
+ } ) } } mode = 'contained' className = "bg-red-500 w-40" > Excluir conta</ Button >
119
+ </ View >
120
+ < Text > Em caso de problemas, contate o suporte</ Text >
121
+ < Text className = "text-purple-700" onPress = { ( ) => { Linking . openURL ( "mailto:mercadobmxbr@gmail.com?subject=Suporte" ) } } > mercadobmxbr@gmail.com</ Text >
113
122
< BottomBar screen = "MenuScreen" > </ BottomBar >
114
123
</ View >
115
124
)
0 commit comments