import React, { useState, useEffect } from "react"; import { Modal, Pressable, ScrollView, StyleSheet, Text, View, } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import { moderateScale, scale, verticalScale, } from "react-native-size-matters"; import BaseTextInput from "@/components/forms/BaseTextInput"; import DropdownField from "@/components/forms/DropdownField"; import FieldLabel from "@/components/forms/FieldLabel"; import PrimaryButton from "@/components/buttons/PrimaryButton"; import { Colors } from "@/theme"; import { getCountryRuleByName, updatePhoneDialCode, validatePhoneNumber, isValidPhoneNumber, parsePhoneNumber, formatE164Phone, cleanPhoneInput, } from "@/constants/countries"; interface EditProfileFormProps { initialName?: string; initialEmail?: string; initialPhone?: string; initialCountry?: string; onSave?: (updatedName: string, updatedPhone: string, updatedCountry: string) => void; isLoading?: boolean; } const COMMON_COUNTRIES = [ "Australia", "Canada", "France", "Germany", "India", "Ireland", "Malaysia", "Netherlands", "New Zealand", "Singapore", "South Africa", "Spain", "Switzerland", "United Arab Emirates", "United Kingdom", "United States", ]; export default function EditProfileForm({ initialName = "User Name", initialEmail = "user@example.com", initialPhone = "", initialCountry = "India", onSave, isLoading = false, }: EditProfileFormProps) { const [fullName, setFullName] = useState(initialName); const [phone, setPhone] = useState(initialPhone || ""); const [country, setCountry] = useState(initialCountry); const [nameError, setNameError] = useState(""); const [phoneError, setPhoneError] = useState(""); const [showCountryModal, setShowCountryModal] = useState(false); const activeRule = getCountryRuleByName(country); useEffect(() => { setFullName(initialName); }, [initialName]); useEffect(() => { setPhone(initialPhone || ""); }, [initialPhone]); useEffect(() => { setCountry(initialCountry); }, [initialCountry]); const handleNameChange = (text: string) => { setFullName(text); if (nameError && text.trim()) { setNameError(""); } }; const handlePhoneChange = (text: string) => { const cleaned = cleanPhoneInput(text, activeRule.maxLength + activeRule.code.length + 2); setPhone(cleaned); if (phoneError) { if (!cleaned.trim() || isValidPhoneNumber(cleaned, activeRule.code, country)) { setPhoneError(""); } } }; const handleCountrySelect = (newCountry: string) => { if (newCountry !== country) { const updatedPhone = updatePhoneDialCode(phone, country, newCountry); setPhone(updatedPhone); setCountry(newCountry); if (phoneError) setPhoneError(""); } setShowCountryModal(false); }; const handleSave = () => { let hasError = false; setNameError(""); setPhoneError(""); const trimmedName = fullName.trim(); if (!trimmedName) { setNameError("Full name is required"); hasError = true; } const trimmedPhone = phone.trim(); if (trimmedPhone) { const rule = getCountryRuleByName(country); const err = validatePhoneNumber(trimmedPhone, rule.code, country); if (err) { setPhoneError(err); hasError = true; } } if (hasError) return; // Normalize phone before saving: ensure no spaces or hyphens, formatted as + let normalizedPhone = ""; if (trimmedPhone) { const rule = getCountryRuleByName(country); const parsed = parsePhoneNumber(trimmedPhone, country); normalizedPhone = formatE164Phone(rule.code, parsed.localDigits, country); } onSave?.(trimmedName, normalizedPhone, country); }; return ( Email address is tied to your account identity and subscriptions. {!phoneError && ( {activeRule.minLength === activeRule.maxLength ? `${activeRule.country} (${activeRule.code}): Requires exactly ${activeRule.maxLength} digits` : `${activeRule.country} (${activeRule.code}): ${activeRule.minLength} to ${activeRule.maxLength} digits`} )} setShowCountryModal(true)} /> {/* Country Selection Modal */} setShowCountryModal(false)} > setShowCountryModal(false)} > Select Country setShowCountryModal(false)} > {COMMON_COUNTRIES.map((c) => { const isSelected = country === c; return ( [ styles.countryItem, isSelected && styles.countryItemSelected, pressed && styles.countryItemPressed, ]} onPress={() => handleCountrySelect(c)} > {c} {isSelected && ( )} ); })} ); } const styles = StyleSheet.create({ container: { marginTop: verticalScale(4), }, field: { marginBottom: verticalScale(16), }, helperText: { marginTop: verticalScale(4), fontSize: moderateScale(11), color: Colors.textSecondary, lineHeight: moderateScale(15), }, buttonWrapper: { marginTop: verticalScale(10), }, modalOverlay: { flex: 1, backgroundColor: "rgba(0, 0, 0, 0.4)", justifyContent: "flex-end", }, modalSheet: { backgroundColor: "#FFFFFF", borderTopLeftRadius: moderateScale(20), borderTopRightRadius: moderateScale(20), maxHeight: "70%", paddingTop: verticalScale(16), paddingBottom: verticalScale(32), }, modalHeader: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: scale(20), paddingBottom: verticalScale(12), borderBottomWidth: 1, borderBottomColor: "#F1F5F9", }, modalTitle: { fontSize: moderateScale(17), fontWeight: "700", color: Colors.textPrimary, }, countryList: { paddingHorizontal: scale(16), paddingTop: verticalScale(8), }, countryItem: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingVertical: verticalScale(12), paddingHorizontal: scale(12), borderRadius: moderateScale(10), }, countryItemSelected: { backgroundColor: "#DCFCE7", }, countryItemPressed: { backgroundColor: "#F1F5F9", }, countryName: { fontSize: moderateScale(14), color: Colors.textPrimary, fontWeight: "500", }, countryNameSelected: { fontWeight: "700", color: "#15803D", }, });