eletrotupi / tcc/ frontend/app/profile.tsx17207313f44cdecf0de608083d71bbfaabc87bf5
5.5 KB | 226 lines Raw
1
import { useState, useEffect } from 'react';
2
import {
3
  View,
4
  Text,
5
  StyleSheet,
6
  ScrollView,
7
  KeyboardAvoidingView,
8
  Platform,
9
  Alert,
10
} from 'react-native';
11

12
import { ThemedView } from '@/components/ui/themed-view'
13
import { ThemedText } from '@/components/ui/themed-text'
14
import { Link, useLocalSearchParams, router } from 'expo-router';
15
import { StatusBar } from 'expo-status-bar';
16
import { SafeAreaView } from 'react-native-safe-area-context';
17
import { useThemeColor } from '@/hooks/use-theme-color';
18
import { Button, Input } from '@/components/ui';
19
import { useAuth } from '@/context/AuthContext';
20

21
export default function Profile() {
22
  const { user } = useAuth();
23
  const [firstName, setFirstName] = useState(user.firstName);
24
  const [lastName, setLastName] = useState(user.lastName);
25
  const [email, setEmail] = useState(user.email);
26
  const [password, setPassword] = useState();
27
  const [confirmPassword, setConfirmPassword] = useState();
28
  const [loading, setLoading] = useState(false);
29
  const [errors, setErrors] = useState<{
30
    firstName?: string;
31
    password?: string;
32
    confirmPassword?: string;
33
  }>({});
34

35
  useEffect(() => {
36
    setFirstName(user.firstName)
37
    setLastName(user.lastName)
38
    setEmail(user.email)
39

40
    console.log(user);
41
  }, [user])
42

43
  const backgroundColor = useThemeColor({}, 'background');
44
  const textColor = useThemeColor({}, 'text');
45

46
  const validateForm = () => {
47
    const newErrors: {
48
      firstName?: string;
49
      email?: string;
50
      password?: string;
51
      confirmPassword?: string
52
    } = {};
53

54
    if (password) {
55
      if (!confirmPassword) {
56
        newErrors.confirmPassword = 'Precisa confirmar a senha';
57
      } else {
58
        if (password == confirmPassword) {
59
          newErrors.confirmPassword = "Senhas não conferem";
60
        }
61
      }
62
    } else if (!/\S+@\S+\.\S+/.test(email)) {
63
      newErrors.email = 'Por favor, use um email válido';
64
    }
65

66
    if (!firstName) {
67
      newErrors.firstName = 'É necessário ter um nome';
68
    }
69

70
    setErrors(newErrors);
71

72
    return Object.keys(newErrors).length === 0;
73
  }
74

75
  const handleUpdate = async () => {
76
    if (!validateForm()) return;
77

78
    const data = {
79
    };
80

81
    data.firstName = firstName
82
    data.lastName = lastName
83
    data.email = email
84

85
    if (password) {
86
      data.password = password
87
    }
88

89
    console.log("sending the update")
90

91
    try {
92
      await updateUser({ firstName, lastName, email, password });
93
      router.replace('/(tabs)');
94
    } catch (error: any) {
95
      Alert.alert('Erro: ', 'Falha ao fazer login');
96
    }
97
  }
98

99
  return (
100
    <SafeAreaView style={[styles.container, { backgroundColor }]}>
101
      <StatusBar style={'dark'} />
102
      <KeyboardAvoidingView
103
        behavior='height'
104
        style={styles.keyboardView}
105
      >
106
        <ScrollView
107
          contentContainerStyle={styles.scrollContainer}
108
          showsVerticalScrollIndicator={false}
109
        >
110
          <View style={styles.header}>
111
            <Text style={[styles.title, { color: textColor }]}>Edite o seu perfil</Text>
112
            <Text style={[styles.subtitle, { color: textColor, opacity: 0.7 }]}>
113
              Altere suas configurações
114
            </Text>
115
          </View>
116

117
          <View style={styles.form}>
118
            <Input
119
              label="Primeiro Nome"
120
              type="text"
121
              value={firstName}
122
              onChangeText={setFirstName}
123
              placeholder="Qual seu nome?"
124
              error={errors.firstName}
125
            />
126

127
            <Input
128
              label="Sobrenome"
129
              type="text"
130
              value={lastName}
131
              onChangeText={setLastName}
132
              placeholder="Qual seu sobrenome?"
133
              error={errors.lastName}
134
            />
135

136
            <Input
137
              label="Email"
138
              type="text"
139
              value={email}
140
              onChangeText={setEmail}
141
              placeholder="Qual seu email?"
142
              error={errors.email}
143
            />
144

145
            <Input
146
              label="Nova senha"
147
              type="password"
148
              value={password}
149
              onChangeText={setPassword}
150
              placeholder="Digite uma nova senha"
151
              error={errors.password}
152
              showPasswordToggle
153
            />
154

155
            <Input
156
              label="Confirmar nova senha"
157
              type="password"
158
              value={confirmPassword}
159
              onChangeText={setConfirmPassword}
160
              placeholder="Confirme a nova senha"
161
              error={errors.confirmPassword}
162
              showPasswordToggle
163
            />
164

165
            <Button
166
              title="Trocar senha"
167
              onPress={handleUpdate}
168
              loading={loading}
169
              style={styles.resetButton}
170
            />
171
          </View>
172

173
        </ScrollView>
174
      </KeyboardAvoidingView>
175
    </SafeAreaView>
176
  )
177
}
178

179
const styles = StyleSheet.create({
180
  container: {
181
    flex: 1,
182
  },
183
  keyboardView: {
184
    flex: 1,
185
  },
186
  scrollContainer: {
187
    flexGrow: 1,
188
    paddingHorizontal: 24,
189
    justifyContent: 'start',
190
    minHeight: '100%',
191
  },
192
  header: {
193
    alignItems: 'left',
194
    marginBottom: 40,
195
  },
196
  title: {
197
    fontSize: 28,
198
    fontWeight: 'bold',
199
    marginBottom: 8,
200
  },
201
  subtitle: {
202
    fontSize: 16,
203
    textAlign: 'left',
204
  },
205
  form: {
206
    marginBottom: 32,
207
  },
208
  resetButton: {
209
    marginTop: 8,
210
  },
211
  link: {
212
    textAlign: 'center',
213
    fontSize: 14,
214
    fontWeight: '500',
215
  },
216
  footer: {
217
    flexDirection: 'row',
218
    justifyContent: 'center',
219
    alignItems: 'center',
220
    marginTop: 'auto',
221
    paddingBottom: 20,
222
  },
223
  footerText: {
224
    fontSize: 14,
225
  },
226
});