From c761853965f4db684c45c18a6f93330377165418 Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 26 Oct 2026 13:31:18 +0530 Subject: [PATCH] issue(44): added country code option before phone number --- src/constants/countries.ts | 185 +++++++++ src/features/auth/components/SignUpForm.tsx | 399 ++++++++++++++++++-- 2 files changed, 561 insertions(+), 23 deletions(-) create mode 100644 src/constants/countries.ts diff --git a/src/constants/countries.ts b/src/constants/countries.ts new file mode 100644 index 0000000..7fb3350 --- /dev/null +++ b/src/constants/countries.ts @@ -0,0 +1,185 @@ +export interface CountryCode { + code: string; + label: string; + country: string; + currency: string; + minLength: number; + maxLength: number; + placeholder: string; +} + +export const COUNTRY_CODES: CountryCode[] = [ + // South Asia + { code: "+91", label: "🇮🇳 +91", country: "India", currency: "INR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+92", label: "🇵🇰 +92", country: "Pakistan", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+880", label: "🇧🇩 +880", country: "Bangladesh", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+94", label: "🇱🇰 +94", country: "Sri Lanka", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+977", label: "🇳🇵 +977", country: "Nepal", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+975", label: "🇧🇹 +975", country: "Bhutan", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+960", label: "🇲🇻 +960", country: "Maldives", currency: "USD", minLength: 7, maxLength: 7, placeholder: "7-digit mobile number" }, + + // North America + { code: "+1", label: "🇺🇸 +1", country: "United States", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" }, + { code: "+1", label: "🇨🇦 +1", country: "Canada", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" }, + { code: "+52", label: "🇲🇽 +52", country: "Mexico", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + + // Europe & UK + { code: "+44", label: "🇬🇧 +44", country: "United Kingdom", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+49", label: "🇩🇪 +49", country: "Germany", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+33", label: "🇫🇷 +33", country: "France", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+39", label: "🇮🇹 +39", country: "Italy", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+34", label: "🇪🇸 +34", country: "Spain", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+31", label: "🇳🇱 +31", country: "Netherlands", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+41", label: "🇨🇭 +41", country: "Switzerland", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+32", label: "🇧🇪 +32", country: "Belgium", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" }, + { code: "+43", label: "🇦🇹 +43", country: "Austria", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+353", label: "🇮🇪 +353", country: "Ireland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+46", label: "🇸🇪 +46", country: "Sweden", currency: "EUR", minLength: 7, maxLength: 10, placeholder: "7 to 10 digits" }, + { code: "+47", label: "🇳🇴 +47", country: "Norway", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+45", label: "🇩🇰 +45", country: "Denmark", currency: "EUR", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+358", label: "🇫🇮 +358", country: "Finland", currency: "EUR", minLength: 6, maxLength: 10, placeholder: "6 to 10 digits" }, + { code: "+351", label: "🇵🇹 +351", country: "Portugal", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+30", label: "🇬🇷 +30", country: "Greece", currency: "EUR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+48", label: "🇵🇱 +48", country: "Poland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+420", label: "🇨🇿 +420", country: "Czech Republic", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+36", label: "🇭🇺 +36", country: "Hungary", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+40", label: "🇷🇴 +40", country: "Romania", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+380", label: "🇺🇦 +380", country: "Ukraine", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+354", label: "🇮🇸 +354", country: "Iceland", currency: "EUR", minLength: 7, maxLength: 9, placeholder: "7 to 9 digits" }, + { code: "+352", label: "🇱🇺 +352", country: "Luxembourg", currency: "EUR", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" }, + { code: "+385", label: "🇭🇷 +385", country: "Croatia", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" }, + { code: "+421", label: "🇸🇰 +421", country: "Slovakia", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+359", label: "🇧🇬 +359", country: "Bulgaria", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" }, + { code: "+7", label: "🇷🇺 +7", country: "Russia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+90", label: "🇹🇷 +90", country: "Turkey", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + + // Asia-Pacific + { code: "+61", label: "🇦🇺 +61", country: "Australia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+64", label: "🇳🇿 +64", country: "New Zealand", currency: "USD", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" }, + { code: "+81", label: "🇯🇵 +81", country: "Japan", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+86", label: "🇨🇳 +86", country: "China", currency: "USD", minLength: 11, maxLength: 11, placeholder: "11-digit mobile number" }, + { code: "+82", label: "🇰🇷 +82", country: "South Korea", currency: "USD", minLength: 9, maxLength: 11, placeholder: "9 to 11 digits" }, + { code: "+852", label: "🇭🇰 +852", country: "Hong Kong", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+886", label: "🇹🇼 +886", country: "Taiwan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+65", label: "🇸🇬 +65", country: "Singapore", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+60", label: "🇲🇾 +60", country: "Malaysia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+62", label: "🇮🇩 +62", country: "Indonesia", currency: "USD", minLength: 9, maxLength: 12, placeholder: "9 to 12 digits" }, + { code: "+63", label: "🇵🇭 +63", country: "Philippines", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+66", label: "🇹🇭 +66", country: "Thailand", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+84", label: "🇻🇳 +84", country: "Vietnam", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + + // Middle East + { code: "+971", label: "🇦🇪 +971", country: "UAE", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+966", label: "🇸🇦 +966", country: "Saudi Arabia", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+974", label: "🇶🇦 +974", country: "Qatar", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+965", label: "🇰🇼 +965", country: "Kuwait", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+968", label: "🇴🇲 +968", country: "Oman", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+973", label: "🇧🇭 +973", country: "Bahrain", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" }, + { code: "+972", label: "🇮🇱 +972", country: "Israel", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+962", label: "🇯🇴 +962", country: "Jordan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+961", label: "🇱🇧 +961", country: "Lebanon", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" }, + + // Latin America + { code: "+55", label: "🇧🇷 +55", country: "Brazil", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+54", label: "🇦🇷 +54", country: "Argentina", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+56", label: "🇨🇱 +56", country: "Chile", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+57", label: "🇨🇴 +57", country: "Colombia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+51", label: "🇵🇪 +51", country: "Peru", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + + // Africa + { code: "+27", label: "🇿🇦 +27", country: "South Africa", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" }, + { code: "+234", label: "🇳🇬 +234", country: "Nigeria", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" }, + { code: "+254", label: "🇰🇪 +254", country: "Kenya", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+233", label: "🇬🇭 +233", country: "Ghana", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+20", label: "🇪🇬 +20", country: "Egypt", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" }, + { code: "+212", label: "🇲🇦 +212", country: "Morocco", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" }, + { code: "+230", label: "🇲🇺 +230", country: "Mauritius", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" }, +]; + +export const DEFAULT_COUNTRY = COUNTRY_CODES[0]; // India (+91) + +export const getCountryRuleByDialCode = (dialCode: string): CountryCode => { + return ( + COUNTRY_CODES.find((c) => c.code === dialCode) || { + code: dialCode, + label: dialCode, + country: "Other", + currency: "USD", + minLength: 6, + maxLength: 15, + placeholder: "Phone number", + } + ); +}; + +export const getCountryRuleByName = (countryName: string): CountryCode => { + if (!countryName) return DEFAULT_COUNTRY; + const normalized = countryName.toLowerCase().trim(); + return ( + COUNTRY_CODES.find( + (c) => + c.country.toLowerCase() === normalized || + normalized.includes(c.country.toLowerCase()) + ) || { + code: "+91", + label: "🇮🇳 +91", + country: countryName, + currency: normalized === "india" ? "INR" : "USD", + minLength: 6, + maxLength: 15, + placeholder: "Phone number", + } + ); +}; + +export const validatePhoneNumber = ( + val?: string | null, + dialCode?: string, + country?: string +): string | null => { + if (!val || !val.trim()) return null; + const trimmed = val.trim(); + + if (/[a-zA-Z]/.test(trimmed)) { + return "Phone number cannot contain alphabetic characters."; + } + + const allDigits = trimmed.replace(/\D/g, ""); + if (allDigits.length === 0) { + return "Please enter a valid phone number with numeric digits."; + } + + let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined; + if ((!rule || rule.country === "Other") && country) { + rule = getCountryRuleByName(country); + } + if (!rule || rule.country === "Other") { + if (dialCode === "+91" || (country && country.toLowerCase() === "india")) { + rule = getCountryRuleByDialCode("+91"); + } + } + + if (rule && rule.country !== "Other") { + if (rule.minLength === rule.maxLength) { + if (allDigits.length !== rule.minLength) { + return `${rule.country} phone number must be exactly ${rule.minLength} digits.`; + } + } else { + if (allDigits.length < rule.minLength) { + return `${rule.country} phone number must be at least ${rule.minLength} digits.`; + } + if (allDigits.length > rule.maxLength) { + return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`; + } + } + return null; + } + + if (allDigits.length < 6) { + return "Phone number must contain at least 6 digits."; + } + if (allDigits.length > 15) { + return "Phone number cannot exceed 15 digits."; + } + return null; +}; diff --git a/src/features/auth/components/SignUpForm.tsx b/src/features/auth/components/SignUpForm.tsx index bc428c0..07f085b 100644 --- a/src/features/auth/components/SignUpForm.tsx +++ b/src/features/auth/components/SignUpForm.tsx @@ -1,12 +1,34 @@ -import React, { useState } from "react"; -import { Alert, StyleSheet, View } from "react-native"; +import React, { useState, useMemo } from "react"; +import { + KeyboardAvoidingView, + Modal, + Platform, + Pressable, + ScrollView, + StyleSheet, + Text, + TextInput, + View, +} from "react-native"; import { useRouter } from "expo-router"; -import { verticalScale } from "react-native-size-matters"; +import { Ionicons } from "@expo/vector-icons"; +import { + moderateScale, + scale, + verticalScale, +} from "react-native-size-matters"; import BaseTextInput from "@/components/forms/BaseTextInput"; import PasswordInput from "@/components/forms/PasswordInput"; import PrimaryButton from "@/components/buttons/PrimaryButton"; - +import { AuthColors } from "../constants/colors"; +import { Colors } from "@/theme"; +import { + COUNTRY_CODES, + CountryCode, + DEFAULT_COUNTRY, + validatePhoneNumber, +} from "@/constants/countries"; import { useAuthStore } from "@/store/auth.store"; import { register } from "@/features/auth/api/auth.api"; @@ -16,9 +38,13 @@ export default function SignUpForm() { const [fullName, setFullName] = useState(""); const [email, setEmail] = useState(""); + const [selectedCountry, setSelectedCountry] = useState(DEFAULT_COUNTRY); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); + const [showCountryModal, setShowCountryModal] = useState(false); + const [countrySearch, setCountrySearch] = useState(""); + const [isLoading, setIsLoading] = useState(false); const [fullNameError, setFullNameError] = useState(""); const [emailError, setEmailError] = useState(""); @@ -26,9 +52,31 @@ export default function SignUpForm() { const [passwordError, setPasswordError] = useState(""); const [confirmPasswordError, setConfirmPasswordError] = useState(""); + const filteredCountries = useMemo(() => { + const q = countrySearch.trim().toLowerCase(); + if (!q) return COUNTRY_CODES; + return COUNTRY_CODES.filter( + (c) => + c.country.toLowerCase().includes(q) || + c.code.includes(q) || + c.label.toLowerCase().includes(q) + ); + }, [countrySearch]); + const handlePhoneChange = (text: string) => { const digits = text.replace(/\D/g, ""); - setPhone(digits.slice(0, 10)); + setPhone(digits.slice(0, selectedCountry.maxLength)); + if (phoneError) setPhoneError(""); + }; + + const handleCountrySelect = (c: CountryCode) => { + setSelectedCountry(c); + setShowCountryModal(false); + setCountrySearch(""); + if (phone.length > c.maxLength) { + setPhone(phone.slice(0, c.maxLength)); + } + if (phoneError) setPhoneError(""); }; const handleSignUp = async () => { @@ -52,9 +100,16 @@ export default function SignUpForm() { hasError = true; } - if (phone.trim() && phone.length < 10) { - setPhoneError("Phone number must be 10 digits"); - hasError = true; + if (phone.trim()) { + const err = validatePhoneNumber( + phone, + selectedCountry.code, + selectedCountry.country + ); + if (err) { + setPhoneError(err); + hasError = true; + } } if (!password) { @@ -87,20 +142,28 @@ export default function SignUpForm() { if (hasError) return; + setIsLoading(true); try { + const formattedPhone = phone.trim() + ? `${selectedCountry.code}${phone.trim()}` + : undefined; + const response = await register({ email: email.trim().toLowerCase(), password, full_name: fullName.trim(), - phone_number: phone.trim() ? phone.trim() : undefined, + phone_number: formattedPhone, + country: selectedCountry.country, }); const { user } = response; - + setRegisteredUser({ id: user.id, - name: user.display_name, + name: user.display_name ?? fullName.trim(), email: user.email, + phone_number: user.phone_number ?? formattedPhone, + country: user.country ?? selectedCountry.country, }); // Redirect directly to login screen without alert block @@ -114,6 +177,8 @@ export default function SignUpForm() { errMsg = detail[0].msg || JSON.stringify(detail[0]); } alert("Registration Failed: " + errMsg); + } finally { + setIsLoading(false); } }; @@ -141,18 +206,43 @@ export default function SignUpForm() { error={emailError} /> - + {/* Phone Number Field with Country Dial Code */} + + + setShowCountryModal(true)} + style={styles.dialCodeButton} + > + {selectedCountry.label} + + + + + + + + {phoneError ? ( + {phoneError} + ) : null} + + + {/* Country Selection Modal */} + { + setShowCountryModal(false); + setCountrySearch(""); + }} + > + + { + setShowCountryModal(false); + setCountrySearch(""); + }} + /> + + + Select Country + { + setShowCountryModal(false); + setCountrySearch(""); + }} + > + + + + + {/* Country Search Bar */} + + + + {countrySearch.length > 0 && ( + setCountrySearch("")} + > + + + )} + + + + {filteredCountries.length === 0 ? ( + + No countries found + + ) : ( + filteredCountries.map((c) => { + const isSelected = selectedCountry.country === c.country; + return ( + [ + styles.countryItem, + isSelected && styles.countryItemSelected, + pressed && styles.countryItemPressed, + ]} + onPress={() => handleCountrySelect(c)} + > + + {c.label} + + {c.country} + + + {isSelected && ( + + )} + + ); + }) + )} + + + + ); } @@ -185,4 +394,148 @@ const styles = StyleSheet.create({ buttonSpacing: { marginTop: verticalScale(10), }, + + phoneFieldWrapper: { + marginBottom: verticalScale(18), + }, + phoneContainer: { + flexDirection: "row", + alignItems: "center", + backgroundColor: AuthColors.inputBackground, + borderWidth: 1, + borderColor: AuthColors.border, + borderRadius: moderateScale(16), + paddingHorizontal: scale(12), + height: verticalScale(56), + }, + dialCodeButton: { + flexDirection: "row", + alignItems: "center", + paddingVertical: verticalScale(8), + paddingHorizontal: scale(4), + }, + dialCodeText: { + fontSize: moderateScale(14), + fontWeight: "700", + color: AuthColors.textPrimary, + }, + phoneDivider: { + width: 1, + height: verticalScale(28), + backgroundColor: AuthColors.border, + marginHorizontal: scale(10), + }, + phoneInput: { + flex: 1, + fontSize: moderateScale(15), + color: AuthColors.textPrimary, + paddingVertical: 0, + }, + + errorBorder: { + borderColor: AuthColors.danger, + }, + errorText: { + color: AuthColors.danger, + fontSize: moderateScale(12), + marginTop: verticalScale(6), + marginLeft: scale(6), + }, + + modalOverlay: { + flex: 1, + backgroundColor: "rgba(0, 0, 0, 0.45)", + justifyContent: "flex-end", + }, + modalSheet: { + backgroundColor: "#FFFFFF", + borderTopLeftRadius: moderateScale(20), + borderTopRightRadius: moderateScale(20), + maxHeight: "75%", + 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", + }, + countryRow: { + flexDirection: "row", + alignItems: "center", + gap: scale(10), + }, + countryLabel: { + fontSize: moderateScale(15), + fontWeight: "600", + color: Colors.textPrimary, + minWidth: scale(65), + }, + countryName: { + fontSize: moderateScale(14), + color: Colors.textPrimary, + fontWeight: "500", + }, + countryNameSelected: { + fontWeight: "700", + color: "#15803D", + }, + modalSearchContainer: { + flexDirection: "row", + alignItems: "center", + backgroundColor: "#F8FAFC", + borderWidth: 1, + borderColor: "#E2E8F0", + borderRadius: moderateScale(12), + marginHorizontal: scale(16), + marginTop: verticalScale(12), + marginBottom: verticalScale(4), + paddingHorizontal: scale(12), + height: verticalScale(42), + gap: scale(8), + }, + modalSearchInput: { + flex: 1, + fontSize: moderateScale(14), + color: Colors.textPrimary, + paddingVertical: 0, + }, + emptyContainer: { + paddingVertical: verticalScale(32), + alignItems: "center", + justifyContent: "center", + }, + emptyText: { + fontSize: moderateScale(14), + color: AuthColors.textSecondary, + fontWeight: "500", + }, }); +