eletrotupi / tcc/ frontend/app/auth/signup.tsxa41d791f910607f9855186176b4ac73901ced70f
5.5 KB | 215 lines Raw
1
import React, { useState } from 'react';
2
import {
3
  View,
4
  Text,
5
  StyleSheet,
6
  ScrollView,
7
  KeyboardAvoidingView,
8
  Platform,
9
  Alert,
10
} from 'react-native';
11
import { Link, router } from 'expo-router';
12
import { SafeAreaView } from 'react-native-safe-area-context';
13
import { useThemeColor } from '@/hooks/use-theme-color';
14
import { Button, Input } from '@/components/ui';
15
import { useAuth } from '@/context/AuthContext';
16

17
export default function SignupScreen() {
18
  const [firstName, setFirstName] = useState('');
19
  const [lastName, setLastName] = useState('');
20
  const [email, setEmail] = useState('');
21
  const [password, setPassword] = useState('');
22
  const [confirmPassword, setConfirmPassword] = useState('');
23
  const [errors, setErrors] = useState<{
24
    firstName?: string;
25
    email?: string;
26
    password?: string;
27
    confirmPassword?: string;
28
  }>({});
29

30
  const { signup, isLoading } = useAuth();
31
  const textColor = useThemeColor({}, 'text');
32
  const backgroundColor = useThemeColor({}, 'background');
33
  const tintColor = useThemeColor({}, 'tint');
34

35
  const validateForm = () => {
36
    const newErrors: typeof errors = {};
37

38
    if (!firstName.trim()) {
39
      newErrors.firstName = 'O nome é obrigatório';
40
    }
41

42
    if (!email) {
43
      newErrors.email = 'Email é obrigatório';
44
    } else if (!/\S+@\S+\.\S+/.test(email)) {
45
      newErrors.email = 'Por favor entre um email válido';
46
    }
47

48
    if (!password) {
49
      newErrors.password = 'Senha é obrigatório';
50
    } else if (password.length < 6) {
51
      newErrors.password = 'A senha deve conter pelo menos 6 caracteres';
52
    }
53

54
    if (!confirmPassword) {
55
      newErrors.confirmPassword = 'Por favor confirme sua senha';
56
    } else if (password !== confirmPassword) {
57
      newErrors.confirmPassword = 'As senhas não coincidem';
58
    }
59

60
    setErrors(newErrors);
61
    return Object.keys(newErrors).length === 0;
62
  };
63

64
  const handleSignup = async () => {
65
    if (!validateForm()) return;
66

67
    try {
68
      await signup({
69
        firstName,
70
        lastName,
71
        email,
72
        password
73
      });
74

75
      router.replace('/');
76
    } catch (error: any) {
77
      Alert.alert('Erro:', error.message || 'Falha no cadastro. Tente novamente.');
78
    }
79
  };
80

81
  return (
82
    <SafeAreaView style={[styles.container, { backgroundColor }]}>
83
      <KeyboardAvoidingView
84
        behavior='height'
85
        style={styles.keyboardView}
86
      >
87
        <ScrollView
88
          contentContainerStyle={styles.scrollContainer}
89
          showsVerticalScrollIndicator={false}
90
        >
91
          <View style={styles.header}>
92
            <Text style={[styles.title, { color: textColor }]}>Criar conta</Text>
93
            <Text style={[styles.subtitle, { color: textColor, opacity: 0.7 }]}>
94
              Cadastre-se para começar a monitorar o seu humor.
95
            </Text>
96
          </View>
97

98
          <View style={styles.form}>
99
            <Input
100
              label="Nome"
101
              value={firstName}
102
              onChangeText={setFirstName}
103
              placeholder="Digite seu nome"
104
              error={errors.firstName}
105
            />
106

107
            <Input
108
              label="Sobrenome (opcional)"
109
              value={lastName}
110
              onChangeText={setLastName}
111
              placeholder="Digite seu sobrenome"
112
            />
113

114
            <Input
115
              label="Email"
116
              type="email"
117
              value={email}
118
              onChangeText={setEmail}
119
              placeholder="Qual seu email?"
120
              error={errors.email}
121
              autoCapitalize="none"
122
            />
123

124
            <Input
125
              label="Password"
126
              type="password"
127
              value={password}
128
              onChangeText={setPassword}
129
              placeholder="Escolha uma senha (min. 6 caracteres)"
130
              error={errors.password}
131
              showPasswordToggle
132
            />
133

134
            <Input
135
              label="Confirme a senha"
136
              type="password"
137
              value={confirmPassword}
138
              onChangeText={setConfirmPassword}
139
              placeholder="Confirme sua senha"
140
              error={errors.confirmPassword}
141
              showPasswordToggle
142
            />
143

144
            <Button
145
              title="Criar conta"
146
              onPress={handleSignup}
147
              loading={isLoading}
148
              style={styles.signupButton}
149
            />
150
          </View>
151

152
          <View style={styles.footer}>
153
            <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
154
              Já tem uma conta?{' '}
155
            </Text>
156
            <Link href="/auth/login" asChild style={[styles.link, { color: tintColor }]}>
157
              <Text style={[styles.link, { color: tintColor }]}>
158
                Faça login
159
              </Text>
160
            </Link>
161
          </View>
162
        </ScrollView>
163
      </KeyboardAvoidingView>
164
    </SafeAreaView>
165
  );
166
}
167

168
const styles = StyleSheet.create({
169
  container: {
170
    flex: 1,
171
  },
172
  keyboardView: {
173
    flex: 1,
174
  },
175
  scrollContainer: {
176
    flexGrow: 1,
177
    paddingHorizontal: 24,
178
    justifyContent: 'center',
179
    minHeight: '100%',
180
  },
181
  header: {
182
    alignItems: 'center',
183
    marginBottom: 40,
184
  },
185
  title: {
186
    fontSize: 28,
187
    fontWeight: 'bold',
188
    marginBottom: 8,
189
  },
190
  subtitle: {
191
    fontSize: 16,
192
    textAlign: 'center',
193
  },
194
  form: {
195
    marginBottom: 32,
196
  },
197
  signupButton: {
198
    marginTop: 8,
199
  },
200
  link: {
201
    textAlign: 'center',
202
    fontSize: 14,
203
    fontWeight: '500',
204
  },
205
  footer: {
206
    flexDirection: 'row',
207
    justifyContent: 'center',
208
    alignItems: 'center',
209
    marginTop: 'auto',
210
    paddingBottom: 20,
211
  },
212
  footerText: {
213
    fontSize: 14,
214
  },
215
});