All files / app/(auth) welcome-back.tsx

0% Statements 0/38
0% Branches 0/32
0% Functions 0/6
0% Lines 0/36

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import React, { useEffect, useState, useRef } from 'react';
import {
  View,
  Text,
  StyleSheet,
  KeyboardAvoidingView,
  Platform,
  ScrollView,
  TouchableOpacity,
  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, FontSizes, Spacing } from '@/constants/theme';
 
export default function WelcomeBackScreen() {
  const { requestOtp, isLoading, error, clearError } = useAuth();
  const params = useLocalSearchParams<{ email: string; name: string }>();
 
  const email = params.email || '';
  const name = params.name || '';
 
  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
    router.push({
      pathname: '/(auth)/verify-otp',
      params: { email, isNewUser: '0' }
    });
  };
 
  const handleBack = () => {
    router.replace('/(auth)/login');
  };
 
  const displayName = name || email.split('@')[0];
 
  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="hand-right" size={48} color={AppColors.primary} />
          </View>
        </View>
 
        <View style={styles.header}>
          <Text style={styles.title}>
            Welcome back{displayName ? `, ${displayName}` : ''}!
          </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>
 
        {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>
 
        <TouchableOpacity style={styles.notYouLink} onPress={handleBack}>
          <Text style={styles.notYouText}>Not you? </Text>
          <Text style={styles.notYouLinkText}>Use another email</Text>
        </TouchableOpacity>
      </ScrollView>
    </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.xl,
  },
  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,
  },
  buttonContainer: {
    marginTop: Spacing.lg,
  },
  notYouLink: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    paddingVertical: Spacing.lg,
    marginTop: Spacing.md,
  },
  notYouText: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
  },
  notYouLinkText: {
    fontSize: FontSizes.base,
    color: AppColors.primary,
    fontWeight: '500',
  },
});