| 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 | router.replace('/(tabs)');
|
| 75 | } catch (error: any) {
|
| 76 | Alert.alert('Erro:', error.message || 'Falha no cadastro. Tente novamente.');
|
| 77 | }
|
| 78 | };
|
| 79 |
|
| 80 | return (
|
| 81 | <SafeAreaView style={[styles.container, { backgroundColor }]}>
|
| 82 | <KeyboardAvoidingView
|
| 83 | behavior='height'
|
| 84 | style={styles.keyboardView}
|
| 85 | >
|
| 86 | <ScrollView
|
| 87 | contentContainerStyle={styles.scrollContainer}
|
| 88 | showsVerticalScrollIndicator={false}
|
| 89 | >
|
| 90 | <View style={styles.header}>
|
| 91 | <Text style={[styles.title, { color: textColor }]}>Criar conta</Text>
|
| 92 | <Text style={[styles.subtitle, { color: textColor, opacity: 0.7 }]}>
|
| 93 | Cadastre-se para começar a monitorar o seu humor.
|
| 94 | </Text>
|
| 95 | </View>
|
| 96 |
|
| 97 | <View style={styles.form}>
|
| 98 | <Input
|
| 99 | label="Nome"
|
| 100 | value={firstName}
|
| 101 | onChangeText={setFirstName}
|
| 102 | placeholder="Digite seu nome"
|
| 103 | error={errors.firstName}
|
| 104 | />
|
| 105 |
|
| 106 | <Input
|
| 107 | label="Sobrenome (opcional)"
|
| 108 | value={lastName}
|
| 109 | onChangeText={setLastName}
|
| 110 | placeholder="Digite seu sobrenome"
|
| 111 | />
|
| 112 |
|
| 113 | <Input
|
| 114 | label="Email"
|
| 115 | type="email"
|
| 116 | value={email}
|
| 117 | onChangeText={setEmail}
|
| 118 | placeholder="Qual seu email?"
|
| 119 | error={errors.email}
|
| 120 | autoCapitalize="none"
|
| 121 | />
|
| 122 |
|
| 123 | <Input
|
| 124 | label="Password"
|
| 125 | type="password"
|
| 126 | value={password}
|
| 127 | onChangeText={setPassword}
|
| 128 | placeholder="Escolha uma senha (min. 6 caracteres)"
|
| 129 | error={errors.password}
|
| 130 | showPasswordToggle
|
| 131 | />
|
| 132 |
|
| 133 | <Input
|
| 134 | label="Confirme a senha"
|
| 135 | type="password"
|
| 136 | value={confirmPassword}
|
| 137 | onChangeText={setConfirmPassword}
|
| 138 | placeholder="Confirme sua senha"
|
| 139 | error={errors.confirmPassword}
|
| 140 | showPasswordToggle
|
| 141 | />
|
| 142 |
|
| 143 | <Button
|
| 144 | title="Criar conta"
|
| 145 | onPress={handleSignup}
|
| 146 | loading={isLoading}
|
| 147 | style={styles.signupButton}
|
| 148 | />
|
| 149 | </View>
|
| 150 |
|
| 151 | <View style={styles.footer}>
|
| 152 | <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
|
| 153 | Já tem uma conta?{' '}
|
| 154 | </Text>
|
| 155 | <Link href="/auth/login" asChild style={[styles.link, { color: tintColor }]}>
|
| 156 | <Text style={[styles.link, { color: tintColor }]}>
|
| 157 | Faça login
|
| 158 | </Text>
|
| 159 | </Link>
|
| 160 | </View>
|
| 161 | </ScrollView>
|
| 162 | </KeyboardAvoidingView>
|
| 163 | </SafeAreaView>
|
| 164 | );
|
| 165 | }
|
| 166 |
|
| 167 | const styles = StyleSheet.create({
|
| 168 | container: {
|
| 169 | flex: 1,
|
| 170 | },
|
| 171 | keyboardView: {
|
| 172 | flex: 1,
|
| 173 | },
|
| 174 | scrollContainer: {
|
| 175 | flexGrow: 1,
|
| 176 | paddingHorizontal: 24,
|
| 177 | justifyContent: 'center',
|
| 178 | minHeight: '100%',
|
| 179 | },
|
| 180 | header: {
|
| 181 | alignItems: 'center',
|
| 182 | marginBottom: 40,
|
| 183 | },
|
| 184 | title: {
|
| 185 | fontSize: 28,
|
| 186 | fontWeight: 'bold',
|
| 187 | marginBottom: 8,
|
| 188 | },
|
| 189 | subtitle: {
|
| 190 | fontSize: 16,
|
| 191 | textAlign: 'center',
|
| 192 | },
|
| 193 | form: {
|
| 194 | marginBottom: 32,
|
| 195 | },
|
| 196 | signupButton: {
|
| 197 | marginTop: 8,
|
| 198 | },
|
| 199 | link: {
|
| 200 | textAlign: 'center',
|
| 201 | fontSize: 14,
|
| 202 | fontWeight: '500',
|
| 203 | },
|
| 204 | footer: {
|
| 205 | flexDirection: 'row',
|
| 206 | justifyContent: 'center',
|
| 207 | alignItems: 'center',
|
| 208 | marginTop: 'auto',
|
| 209 | paddingBottom: 20,
|
| 210 | },
|
| 211 | footerText: {
|
| 212 | fontSize: 14,
|
| 213 | },
|
| 214 | });
|