frontend: update user profile
Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago
e767eac862370de7f00267c757d71ab7aa9f6982 Browse filesParents
02a7abeChanged files
1
Additions
+22
Deletions
-12
frontend/app/profile.tsx
1 1
import { useState, useEffect } from 'react';
2 2
import {
3 3
View,
4 4
Text,
5 5
StyleSheet,
15 15
import { StatusBar } from 'expo-status-bar';
16 16
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);
26 27
const [password, setPassword] = useState();
27 28
const [confirmPassword, setConfirmPassword] = useState();
34 35
35 36
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');
44 43
const textColor = useThemeColor({}, 'text');
45 44
53 52
54 53
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
66 67
if (!firstName) {
67 68
newErrors.firstName = 'É necessário ter um nome';
73 74
}
74 75
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
83 83
data.email = email
84 84
85 85
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
99 109
return (
100 110
<SafeAreaView style={[styles.container, { backgroundColor }]}>
161 171
error={errors.confirmPassword}
162 172
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}
170 180
/>
171 181
</View>
222 232
},
223 233
footerText: {
224 234
fontSize: 14,
225 235
},
226 236
});