1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
import { useEffect } from 'react';
import {
ActivityIndicator,
Pressable,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import { useAuthSession } from '@/providers/AuthProvider';
import { useProfile } from '@/features/profile/hooks/useProfile';
import { useUpdateProfile } from '@/features/profile/hooks/useUpdateProfile';
import { router } from 'expo-router';
export default function ProfileScreen() {
const { logout } = useAuthSession();
const { profile, loading, error, reload } = useProfile();
const {
displayName,
setDisplayName,
isSubmitting,
error: updateError,
successMessage,
submit,
} = useUpdateProfile(profile?.displayName ?? '');
useEffect(() => {
if (profile?.displayName) {
setDisplayName(profile.displayName);
}
}, [profile?.displayName]);
const handleLogout = async () => {
await logout();
router.replace('/(auth)/welcome');
};
if (loading) {
return (
<View style={styles.center}>
<ActivityIndicator size="large" />
</View>
);
}
if (error || !profile) {
return (
<View style={styles.center}>
<Text style={styles.error}>{error ?? 'No se pudo cargar el perfil'}</Text>
<Pressable style={styles.button} onPress={reload}>
<Text style={styles.buttonText}>Reintentar</Text>
</Pressable>
</View>
);
}
return (
<View style={styles.container}>
<Text style={styles.title}>Perfil</Text>
<View style={styles.card}>
<Text style={styles.label}>Correo</Text>
<Text style={styles.value}>{profile.email}</Text>
<Text style={styles.label}>Nombre</Text>
<TextInput
style={styles.input}
value={displayName}
onChangeText={setDisplayName}
placeholder="Tu nombre"
/>
{updateError ? <Text style={styles.error}>{updateError}</Text> : null}
{successMessage ? <Text style={styles.success}>{successMessage}</Text> : null}
<Pressable style={styles.button} onPress={submit} disabled={isSubmitting}>
<Text style={styles.buttonText}>
{isSubmitting ? 'Guardando...' : 'Guardar cambios'}
</Text>
</Pressable>
</View>
<Pressable style={styles.logoutButton} onPress={handleLogout}>
<Text style={styles.buttonText}>Cerrar sesión</Text>
</Pressable>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
gap: 16,
},
center: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 24,
gap: 12,
},
title: {
fontSize: 28,
fontWeight: '700',
},
card: {
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 12,
padding: 16,
gap: 10,
},
label: {
fontSize: 14,
fontWeight: '700',
color: '#444',
},
value: {
fontSize: 16,
color: '#111',
},
input: {
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 10,
paddingHorizontal: 14,
paddingVertical: 12,
},
button: {
backgroundColor: '#1f6feb',
paddingVertical: 14,
borderRadius: 10,
alignItems: 'center',
},
logoutButton: {
backgroundColor: '#c62828',
paddingVertical: 14,
borderRadius: 10,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontWeight: '600',
},
error: {
color: 'red',
},
success: {
color: 'green',
},
});
|