All files / app/(auth) verify-email.tsx

0% Statements 0/50
0% Branches 0/32
0% Functions 0/13
0% Lines 0/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import React, { useState, useEffect, useRef } from 'react';
import {
  View,
  Text,
  StyleSheet,
  KeyboardAvoidingView,
  Platform,
  ScrollView,
  TouchableOpacity,
  Modal,
  TextInput,
  ActivityIndicator,
} from 'react-native';
import { router, useLocalSearchParams } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { useAuth } from '@/contexts/AuthContext';
import { Button } from '@/components/ui/Button';
import { ErrorMessage } from '@/components/ui/ErrorMessage';
import { AppColors, BorderRadius, FontSizes, Spacing } from '@/constants/theme';
 
export default function VerifyEmailScreen() {
  const { requestOtp, isLoading, error, clearError } = useAuth();
  const params = useLocalSearchParams<{ email: string }>();
 
  const email = params.email || '';
 
  // Invite code state
  const [inviteCode, setInviteCode] = useState('');
  const [inviteModalVisible, setInviteModalVisible] = useState(false);
  const [tempInviteCode, setTempInviteCode] = useState('');
 
  // OTP sending state
  const [codeSent, setCodeSent] = useState(false);
  const [sendingOtp, setSendingOtp] = useState(false);
  const hasSentOtp = useRef(false);
 
  // Clear errors on mount
  useEffect(() => {
    clearError();
  }, []);
 
  // Auto-send OTP on mount
  useEffect(() => {
    if (!email || hasSentOtp.current) return;
 
    const sendOtp = async () => {
      hasSentOtp.current = true;
      setSendingOtp(true);
 
      const result = await requestOtp(email);
 
      setSendingOtp(false);
      if (result.success) {
        setCodeSent(true);
      }
    };
 
    sendOtp();
  }, [email]);
 
  const handleContinue = async () => {
    clearError();
 
    if (!email) {
      router.replace('/(auth)/login');
      return;
    }
 
    // If code wasn't sent, try sending first
    if (!codeSent) {
      setSendingOtp(true);
      const result = await requestOtp(email);
      setSendingOtp(false);
 
      if (!result.success) return;
      setCodeSent(true);
    }
 
    // Navigate to OTP verification for new user
    router.push({
      pathname: '/(auth)/verify-otp',
      params: { email, isNewUser: '1', inviteCode }
    });
  };
 
  const handleBack = () => {
    router.back();
  };
 
  const handleInviteCodeSubmit = () => {
    setInviteCode(tempInviteCode);
    setInviteModalVisible(false);
  };
 
  const openInviteModal = () => {
    setTempInviteCode(inviteCode);
    setInviteModalVisible(true);
  };
 
  return (
    <KeyboardAvoidingView
      style={styles.container}
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
    >
      <ScrollView
        contentContainerStyle={styles.scrollContent}
        keyboardShouldPersistTaps="handled"
        showsVerticalScrollIndicator={false}
      >
        <TouchableOpacity style={styles.backButton} onPress={handleBack}>
          <Ionicons name="arrow-back" size={24} color={AppColors.textPrimary} />
        </TouchableOpacity>
 
        <View style={styles.iconContainer}>
          <View style={styles.iconCircle}>
            <Ionicons name="mail" size={48} color={AppColors.primary} />
          </View>
        </View>
 
        <View style={styles.header}>
          <Text style={styles.title}>Let's get started!</Text>
          {sendingOtp ? (
            <View style={styles.sendingContainer}>
              <ActivityIndicator size="small" color={AppColors.primary} />
              <Text style={styles.sendingText}>Sending verification code...</Text>
            </View>
          ) : codeSent ? (
            <Text style={styles.subtitle}>We've sent a verification code to</Text>
          ) : (
            <Text style={styles.subtitle}>We'll send a verification code to</Text>
          )}
          <Text style={styles.email}>{email}</Text>
        </View>
 
        {/* Invite Code */}
        {inviteCode ? (
          <TouchableOpacity style={styles.inviteBadge} onPress={openInviteModal}>
            <Ionicons name="gift" size={16} color={AppColors.success} />
            <Text style={styles.inviteBadgeText}>Invite code: {inviteCode}</Text>
            <Ionicons name="pencil" size={14} color={AppColors.success} />
          </TouchableOpacity>
        ) : (
          <TouchableOpacity style={styles.inviteLink} onPress={openInviteModal}>
            <Text style={styles.inviteLinkText}>Have an invite code?</Text>
          </TouchableOpacity>
        )}
 
        {error && (
          <ErrorMessage message={error.message} onDismiss={clearError} />
        )}
 
        <View style={styles.buttonContainer}>
          <Button
            title={codeSent ? 'Continue to Verify' : 'Send Code & Continue'}
            onPress={handleContinue}
            loading={isLoading || sendingOtp}
            fullWidth
            size="lg"
          />
        </View>
 
        <View style={styles.infoContainer}>
          <Text style={styles.infoText}>
            After verification, you'll be able to set up your profile and start monitoring your loved ones
          </Text>
        </View>
      </ScrollView>
 
      {/* Invite Code Modal */}
      <Modal
        visible={inviteModalVisible}
        transparent
        animationType="slide"
        onRequestClose={() => setInviteModalVisible(false)}
      >
        <View style={styles.modalOverlay}>
          <View style={styles.modalContent}>
            <Text style={styles.modalTitle}>Enter Invite Code</Text>
            <Text style={styles.modalSubtitle}>
              If you received a referral code, enter it below
            </Text>
 
            <View style={styles.codeContainer}>
              {[0, 1, 2, 3, 4, 5].map((index) => (
                <View
                  key={index}
                  style={[
                    styles.codeCell,
                    tempInviteCode[index] && styles.codeCellFilled,
                  ]}
                >
                  <Text style={styles.codeCellText}>
                    {tempInviteCode[index] || ''}
                  </Text>
                </View>
              ))}
            </View>
 
            <TextInput
              style={styles.hiddenInput}
              value={tempInviteCode}
              onChangeText={(text) => setTempInviteCode(text.replace(/[^0-9]/g, '').slice(0, 6))}
              keyboardType="number-pad"
              maxLength={6}
              autoFocus
            />
 
            <TouchableOpacity
              style={styles.testCodeHint}
              onPress={() => setTempInviteCode('123456')}
            >
              <Text style={styles.testCodeText}>For testing: tap to use 123456</Text>
            </TouchableOpacity>
 
            <View style={styles.modalButtons}>
              <TouchableOpacity
                style={styles.modalCancelButton}
                onPress={() => setInviteModalVisible(false)}
              >
                <Text style={styles.modalCancelText}>Cancel</Text>
              </TouchableOpacity>
 
              <Button
                title="Apply"
                onPress={handleInviteCodeSubmit}
                size="md"
                style={styles.modalApplyButton}
              />
            </View>
          </View>
        </View>
      </Modal>
    </KeyboardAvoidingView>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: AppColors.background,
  },
  scrollContent: {
    flexGrow: 1,
    paddingHorizontal: Spacing.lg,
    paddingTop: Spacing.xl,
    paddingBottom: Spacing.xl,
  },
  backButton: {
    width: 44,
    height: 44,
    justifyContent: 'center',
    alignItems: 'flex-start',
    marginBottom: Spacing.xl,
  },
  iconContainer: {
    alignItems: 'center',
    marginBottom: Spacing.xl,
  },
  iconCircle: {
    width: 100,
    height: 100,
    borderRadius: 50,
    backgroundColor: `${AppColors.primary}15`,
    justifyContent: 'center',
    alignItems: 'center',
  },
  header: {
    alignItems: 'center',
    marginBottom: Spacing.lg,
  },
  title: {
    fontSize: FontSizes['2xl'],
    fontWeight: '700',
    color: AppColors.textPrimary,
    marginBottom: Spacing.md,
    textAlign: 'center',
  },
  subtitle: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
    textAlign: 'center',
  },
  sendingContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.sm,
  },
  sendingText: {
    fontSize: FontSizes.base,
    color: AppColors.primary,
  },
  email: {
    fontSize: FontSizes.base,
    fontWeight: '600',
    color: AppColors.textPrimary,
    marginTop: Spacing.xs,
  },
  inviteBadge: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: `${AppColors.success}15`,
    paddingVertical: Spacing.sm,
    paddingHorizontal: Spacing.md,
    borderRadius: BorderRadius.md,
    marginBottom: Spacing.lg,
    gap: Spacing.xs,
  },
  inviteBadgeText: {
    fontSize: FontSizes.sm,
    color: AppColors.success,
    fontWeight: '500',
  },
  inviteLink: {
    alignItems: 'center',
    paddingVertical: Spacing.md,
    marginBottom: Spacing.sm,
  },
  inviteLinkText: {
    fontSize: FontSizes.base,
    color: AppColors.primary,
    fontWeight: '500',
  },
  buttonContainer: {
    marginTop: Spacing.lg,
  },
  infoContainer: {
    alignItems: 'center',
    marginTop: Spacing.xl,
    paddingHorizontal: Spacing.md,
  },
  infoText: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
    textAlign: 'center',
    lineHeight: 20,
  },
  modalOverlay: {
    flex: 1,
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    justifyContent: 'center',
    alignItems: 'center',
    padding: Spacing.lg,
  },
  modalContent: {
    backgroundColor: AppColors.background,
    borderRadius: BorderRadius.lg,
    padding: Spacing.xl,
    width: '100%',
    maxWidth: 400,
  },
  modalTitle: {
    fontSize: FontSizes.xl,
    fontWeight: '700',
    color: AppColors.textPrimary,
    marginBottom: Spacing.sm,
    textAlign: 'center',
  },
  modalSubtitle: {
    fontSize: FontSizes.sm,
    color: AppColors.textSecondary,
    textAlign: 'center',
    marginBottom: Spacing.lg,
  },
  codeContainer: {
    flexDirection: 'row',
    justifyContent: 'center',
    gap: Spacing.sm,
    marginBottom: Spacing.sm,
  },
  codeCell: {
    width: 44,
    height: 52,
    borderRadius: BorderRadius.md,
    borderWidth: 2,
    borderColor: AppColors.border,
    backgroundColor: AppColors.surface,
    justifyContent: 'center',
    alignItems: 'center',
  },
  codeCellFilled: {
    borderColor: AppColors.primary,
    backgroundColor: `${AppColors.primary}10`,
  },
  codeCellText: {
    fontSize: FontSizes['2xl'],
    fontWeight: '600',
    color: AppColors.textPrimary,
  },
  hiddenInput: {
    position: 'absolute',
    opacity: 0,
    height: 0,
  },
  testCodeHint: {
    alignItems: 'center',
    paddingVertical: Spacing.sm,
    marginBottom: Spacing.md,
  },
  testCodeText: {
    fontSize: FontSizes.xs,
    color: AppColors.primary,
    textDecorationLine: 'underline',
  },
  modalButtons: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    gap: Spacing.md,
  },
  modalCancelButton: {
    flex: 1,
    padding: Spacing.md,
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: BorderRadius.md,
    borderWidth: 1,
    borderColor: AppColors.border,
  },
  modalCancelText: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
  },
  modalApplyButton: {
    flex: 1,
  },
});