frontend: update user profile

Pedro Lucas Porcellis porcellis@eletrotupi.com 1 month ago e767eac862370de7f00267c757d71ab7aa9f6982
Parents: 02a7abe
1 file(s) changed
  • frontend/app/profile.tsx +22 -12
frontend/app/profile.tsx
@@ -17,9 +17,10 @@ import { SafeAreaView } from 'react-native-safe-area-context';
17 17 import { useThemeColor } from '@/hooks/use-theme-color';
18 18 import { Button, Input } from '@/components/ui';
19 19 import { useAuth } from '@/context/AuthContext';
20 + import { apiClient } from '@/lib/api'
20 21
21 22 export default function Profile() {
22 - const { user } = useAuth();
23 + const { user, updateAuthUser } = useAuth();
23 24 const [firstName, setFirstName] = useState(user.firstName);
24 25 const [lastName, setLastName] = useState(user.lastName);
25 26 const [email, setEmail] = useState(user.email);
@@ -36,8 +37,6 @@ useEffect(() => {
36 37 setFirstName(user.firstName)
37 38 setLastName(user.lastName)
38 39 setEmail(user.email)
39 -
40 - console.log(user);
41 40 }, [user])
42 41
43 42 const backgroundColor = useThemeColor({}, 'background');
@@ -55,11 +54,13 @@ if (password) {
55 54 if (!confirmPassword) {
56 55 newErrors.confirmPassword = 'Precisa confirmar a senha';
57 56 } else {
58 - if (password == confirmPassword) {
57 + if (password !== confirmPassword) {
59 58 newErrors.confirmPassword = "Senhas não conferem";
60 59 }
61 60 }
62 - } else if (!/\S+@\S+\.\S+/.test(email)) {
61 + }
62 +
63 + if (!/\S+@\S+\.\S+/.test(email)) {
63 64 newErrors.email = 'Por favor, use um email válido';
64 65 }
65 66
@@ -75,8 +76,7 @@
75 76 const handleUpdate = async () => {
76 77 if (!validateForm()) return;
77 78
78 - const data = {
79 - };
79 + const data = {};
80 80
81 81 data.firstName = firstName
82 82 data.lastName = lastName
@@ -86,13 +86,23 @@ if (password) {
86 86 data.password = password
87 87 }
88 88
89 - console.log("sending the update")
90 -
91 89 try {
92 - await updateUser({ firstName, lastName, email, password });
90 + const id = user.id;
91 +
92 + const response = await apiClient.updateUser({
93 + id,
94 + firstName,
95 + lastName,
96 + email,
97 + password
98 + });
99 +
100 + updateAuthUser(response.user);
101 +
93 102 router.replace('/(tabs)');
94 103 } catch (error: any) {
95 - Alert.alert('Erro: ', 'Falha ao fazer login');
104 + console.error("[PROFILE]", error)
105 + Alert.alert('Erro: ', 'Falha ao tentar atualizar o usuário');
96 106 }
97 107 }
98 108
@@ -163,7 +173,7 @@ showPasswordToggle
163 173 />
164 174
165 175 <Button
166 - title="Trocar senha"
176 + title="Atualizar perfil"
167 177 onPress={handleUpdate}
168 178 loading={loading}
169 179 style={styles.resetButton}