eletrotupi / tcc/ frontend/app/auth/activate.tsx6d8c0a74220b36e86fd53538617fd70ce2618f59
3.9 KB | 159 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, useLocalSearchParams, 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 { Colors } from '@/constants/theme'
16
import { useAuth } from '@/context/AuthContext';
17

18
export default function ActivateScreen() {
19
  const { userId } = useLocalSearchParams();
20
  const [code, setCode] = useState('');
21
  const [errors, setErrors] = useState<{
22
    code?: string;
23
  }>({});
24

25
  const { activate } = useAuth();
26
  const [loading, setLoading] = useState(false);
27
  const textColor = useThemeColor({}, 'text');
28
  const backgroundColor = useThemeColor({}, 'background');
29
  const tintColor = useThemeColor({}, 'tint');
30

31
  const validateForm = () => {
32
    const newErrors: typeof errors = {};
33

34
    if (!code) {
35
      newErrors.code = 'O código é obrigatório';
36
    } else if (code.length < 6) {
37
      newErrors.code = 'O código deve conter pelo menos 6 caracteres';
38
    }
39

40
    setErrors(newErrors);
41

42
    return Object.keys(newErrors).length === 0;
43
  };
44

45
  const handleActivate = async () => {
46
    if (!validateForm()) return;
47

48
    setLoading(true);
49
    try {
50
      await activate(Number(code.replaceAll(' ', '')));
51

52
      Alert.alert('Sucesso!', 'Conta ativada com sucesso', [
53
        {
54
          text: 'OK',
55
          onPress: () => router.replace('/'),
56
        },
57
      ]);
58
    } catch (error: any) {
59
      Alert.alert('Erro: ', error.message || 'Falha em ativar a conta');
60
    } finally {
61
      setLoading(false);
62
    }
63
  };
64

65
  const resendCode = async () => {
66
  }
67

68
  return (
69
    <SafeAreaView style={[styles.container, { backgroundColor }]}>
70
      <KeyboardAvoidingView
71
        behavior='height'
72
        style={styles.keyboardView}
73
      >
74
        <ScrollView
75
          contentContainerStyle={styles.scrollContainer}
76
          showsVerticalScrollIndicator={false}
77
        >
78
          <View style={styles.header}>
79
            <Text style={[styles.title, { color: textColor }]}>Ativar a conta</Text>
80
            <Text style={[styles.subtitle, { color: textColor, opacity: 0.7 }]}>
81
              Digite o código de ativação enviado para seu email
82
            </Text>
83
          </View>
84

85
          <View style={styles.form}>
86
            <Input
87
              label="Código"
88
              type="text"
89
              value={code}
90
              onChangeText={setCode}
91
              keyboardType="number-pad"
92
              placeholder="Digite o código enviado para sua conta"
93
              error={errors.code}
94
            />
95

96
            <Button
97
              title="Ativar conta"
98
              onPress={handleActivate}
99
              loading={loading}
100
              style={styles.resetButton}
101
            />
102
          </View>
103

104
          <View style={styles.footer}>
105
            <Text style={[styles.footerText, { color: textColor, opacity: 0.7 }]}>
106
              Não recebeu?{' '}
107
            </Text>
108

109
            <Button variant="outline" title="Reenviar um novo código" onPressed={resendCode} />
110
          </View>
111
        </ScrollView>
112
      </KeyboardAvoidingView>
113
    </SafeAreaView>
114
  );
115
}
116

117
const styles = StyleSheet.create({
118
  container: {
119
    flex: 1,
120
  },
121
  keyboardView: {
122
    flex: 1,
123
  },
124
  scrollContainer: {
125
    flexGrow: 1,
126
    paddingHorizontal: 24,
127
    justifyContent: 'center',
128
    minHeight: '100%',
129
  },
130
  header: {
131
    alignItems: 'center',
132
    marginBottom: 40,
133
  },
134
  title: {
135
    fontSize: 28,
136
    fontWeight: 'bold',
137
    marginBottom: 8,
138
  },
139
  subtitle: {
140
    fontSize: 16,
141
    textAlign: 'center',
142
  },
143
  form: {
144
    marginBottom: 32,
145
  },
146
  resetButton: {
147
    marginTop: 8,
148
  },
149
  footer: {
150
    marginTop: 20,
151
    borderTopColor: Colors.light.accentBlue,
152
    borderTopWidth: 1,
153
    paddingTop: 20
154
  },
155
  footerText: {
156
    textAlign: 'center',
157
    marginBottom: 10
158
  }
159
});