diff --git a/src/components/auth/ProfileDrawer.tsx b/src/components/auth/ProfileDrawer.tsx index f4a8b86..e73610e 100644 --- a/src/components/auth/ProfileDrawer.tsx +++ b/src/components/auth/ProfileDrawer.tsx @@ -5,7 +5,7 @@ import { } from 'lucide-react'; import type { UserProfile, ModuleItem, Currency } from '../../types'; import { getCountryRuleByName } from '../../constants/countries'; -import { cleanPhoneInput, parsePhoneNumber, formatE164Phone, SPECIAL_CHAR_REGEX } from '../../utils/validation'; +import { parsePhoneNumber, formatE164Phone, SPECIAL_CHAR_REGEX } from '../../utils/validation'; import { PhoneInput } from '../common/PhoneInput'; import { CountrySelect } from '../common/CountrySelect'; @@ -97,7 +97,7 @@ export const ProfileDrawer: React.FC = ({ if (setChangePasswordSuccess) setChangePasswordSuccess(false); if (setChangePasswordError) setChangePasswordError(null); } - }, [profilePhone, profileCountry, isOpen, setChangePasswordSuccess, setChangePasswordError]); + }, [isOpen, setChangePasswordSuccess, setChangePasswordError]); const handleLocalPhoneChange = (newLocal: string) => { setLocalPhone(newLocal); @@ -106,22 +106,30 @@ export const ProfileDrawer: React.FC = ({ const handleDialCodeChange = (newCode: string, newCountry: string) => { setDialCode(newCode); - setProfileCountry(newCountry); - const rule = getCountryRuleByName(newCountry); - setCurrency(rule.currency); - const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); - setLocalPhone(clamped); - setProfilePhone(formatE164Phone(newCode, clamped, newCountry)); + if (newCountry !== profileCountry) { + setProfileCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + if (rule) { + setCurrency(rule.currency); + } + setLocalPhone(''); + setProfilePhone(''); + } else { + setProfilePhone(localPhone ? formatE164Phone(newCode, localPhone, profileCountry) : ''); + } }; const handleCountrySelectChange = (newCountry: string) => { - setProfileCountry(newCountry); - const rule = getCountryRuleByName(newCountry); - setDialCode(rule.code); - setCurrency(rule.currency); - const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); - setLocalPhone(clamped); - setProfilePhone(formatE164Phone(rule.code, clamped, newCountry)); + if (newCountry !== profileCountry) { + setProfileCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + if (rule) { + setDialCode(rule.code); + setCurrency(rule.currency); + } + setLocalPhone(''); + setProfilePhone(''); + } }; if (!isOpen || !loggedInUser) return null; @@ -197,6 +205,7 @@ export const ProfileDrawer: React.FC = ({ dialCode={dialCode} phone={localPhone} country={profileCountry} + clearOnDialCodeChange={true} onDialCodeChange={handleDialCodeChange} onPhoneChange={handleLocalPhoneChange} theme="light" diff --git a/src/components/cart/CartDrawer.tsx b/src/components/cart/CartDrawer.tsx index e936b21..fc1814c 100644 --- a/src/components/cart/CartDrawer.tsx +++ b/src/components/cart/CartDrawer.tsx @@ -366,6 +366,10 @@ export const CartDrawer: React.FC = ({ Email: {loggedInUser.email} +
+ Billing Country: + {loggedInUser.country || 'India'} +
Phone: diff --git a/src/components/checkout/BillingPhoneModal.tsx b/src/components/checkout/BillingPhoneModal.tsx index 925a61f..8dc2b08 100644 --- a/src/components/checkout/BillingPhoneModal.tsx +++ b/src/components/checkout/BillingPhoneModal.tsx @@ -1,6 +1,8 @@ import React, { useState, useEffect } from 'react'; -import { X, Phone, ShieldCheck, RefreshCw } from 'lucide-react'; +import { X, Phone, ShieldCheck, RefreshCw, Globe } from 'lucide-react'; import { PhoneInput } from '../common/PhoneInput'; +import { CountrySelect } from '../common/CountrySelect'; +import { getCountryRuleByName } from '../../constants/countries'; import { parsePhoneNumber, validatePhoneNumber, formatE164Phone } from '../../utils/validation'; interface BillingPhoneModalProps { @@ -37,6 +39,27 @@ export const BillingPhoneModal: React.FC = ({ if (!isOpen) return null; + const handleCountryChange = (newCountry: string) => { + if (newCountry !== country) { + setCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + if (rule) { + setDialCode(rule.code); + } + setPhone(''); + setError(null); + } + }; + + const handleDialCodeChange = (newCode: string, newCountry: string) => { + setDialCode(newCode); + if (newCountry !== country) { + setCountry(newCountry); + setPhone(''); + } + setError(null); + }; + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); @@ -99,11 +122,8 @@ export const BillingPhoneModal: React.FC = ({ dialCode={dialCode} phone={phone} country={country} - onDialCodeChange={(newCode, newCountry) => { - setDialCode(newCode); - setCountry(newCountry); - setError(null); - }} + clearOnDialCodeChange={true} + onDialCodeChange={handleDialCodeChange} onPhoneChange={(newPhone) => { setPhone(newPhone); setError(null); @@ -114,9 +134,24 @@ export const BillingPhoneModal: React.FC = ({ />
+
+ + +
+
- This contact number will appear on your compliance tax invoice and payment receipts. + This contact number and billing country will appear on your compliance tax invoice and payment receipts.
@@ -141,4 +176,3 @@ export const BillingPhoneModal: React.FC = ({
); }; - diff --git a/src/components/common/PhoneInput.tsx b/src/components/common/PhoneInput.tsx index 38d8ad2..88be9b7 100644 --- a/src/components/common/PhoneInput.tsx +++ b/src/components/common/PhoneInput.tsx @@ -1,5 +1,12 @@ -import React from 'react'; -import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries'; +import React, { useState, useRef, useEffect, useMemo } from 'react'; +import { Search, ChevronDown, Check, X } from 'lucide-react'; +import { + COUNTRY_CODES, + SORTED_COUNTRY_CODES, + getCountryRuleByDialCode, + getCountryRuleByName, + type CountryCode, +} from '../../constants/countries'; import { cleanPhoneInput } from '../../utils/validation'; export interface PhoneInputProps { @@ -27,6 +34,11 @@ export const PhoneInput: React.FC = ({ id = 'phone-input', clearOnDialCodeChange = false, }) => { + const [isDropdownOpen, setIsDropdownOpen] = useState(false); + const [searchQuery, setSearchQuery] = useState(''); + const dropdownRef = useRef(null); + const searchInputRef = useRef(null); + // Prioritize country name if available, fallback to dial code let activeRule = country ? getCountryRuleByName(country) : undefined; if (!activeRule && dialCode) { @@ -36,17 +48,79 @@ export const PhoneInput: React.FC = ({ activeRule = getCountryRuleByDialCode('+91')!; } - const handleCountrySelect = (e: React.ChangeEvent) => { - const selectedCountryName = e.target.value; - const rule = getCountryRuleByName(selectedCountryName); + // Focus search input when dropdown opens + useEffect(() => { + if (isDropdownOpen) { + setTimeout(() => { + searchInputRef.current?.focus(); + }, 50); + } else { + setSearchQuery(''); + } + }, [isDropdownOpen]); + + // Handle click outside to close dropdown + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { + setIsDropdownOpen(false); + } + }; + + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + setIsDropdownOpen(false); + } + }; + + if (isDropdownOpen) { + document.addEventListener('mousedown', handleClickOutside); + document.addEventListener('keydown', handleKeyDown); + } + + return () => { + document.removeEventListener('mousedown', handleClickOutside); + document.removeEventListener('keydown', handleKeyDown); + }; + }, [isDropdownOpen]); + + // Filter countries alphabetically by country name or dial code + const filteredCountryCodes = useMemo(() => { + if (!searchQuery.trim()) { + return SORTED_COUNTRY_CODES; + } + const q = searchQuery.toLowerCase().trim(); + return SORTED_COUNTRY_CODES.filter((c: CountryCode) => { + const matchCountry = c.country.toLowerCase().includes(q); + const matchCode = c.code.toLowerCase().includes(q); + const rawCode = c.code.replace('+', ''); + const matchRaw = rawCode.includes(q); + return matchCountry || matchCode || matchRaw; + }); + }, [searchQuery]); + + const getFlag = (cc: CountryCode) => { + return cc.label ? cc.label.split(' ')[0] : '🌐'; + }; + + const isSelected = (cc: CountryCode) => { + if (country) { + return cc.country.toLowerCase() === country.toLowerCase() && cc.code === dialCode; + } + return cc.code === dialCode; + }; + + const handleSelectCountry = (cc: CountryCode) => { if (onDialCodeChange) { - onDialCodeChange(rule.code, rule.country); + onDialCodeChange(cc.code, cc.country); } if (clearOnDialCodeChange) { onPhoneChange(''); } else { - onPhoneChange(cleanPhoneInput(phone, rule.maxLength, false)); + onPhoneChange(cleanPhoneInput(phone, cc.maxLength, false)); } + setIsDropdownOpen(false); + setSearchQuery(''); }; const handlePhoneInputChange = (e: React.ChangeEvent) => { @@ -108,27 +182,123 @@ export const PhoneInput: React.FC = ({ return (
- {/* Country Code Dropdown: keyed by unique country name to avoid +1 (US vs CA) collisions */} -
- -
- ▼ -
+
+ {getFlag(activeRule)} + {activeRule.code} +
+ + + + {/* Searchable Dropdown Popover */} + {isDropdownOpen && ( +
+ {/* Search Box */} +
+
+ + setSearchQuery(e.target.value)} + placeholder="Search country or code..." + className={`w-full rounded-xl pl-8 pr-8 py-1.5 text-xs font-semibold focus:outline-none transition-colors ${ + isDark + ? 'bg-slate-950 border border-slate-800 text-white placeholder:text-slate-600 focus:border-indigo-500' + : 'bg-white border border-slate-200 text-slate-800 placeholder:text-slate-400 focus:border-indigo-500' + }`} + /> + {searchQuery && ( + + )} +
+
+ + {/* Alphabetically Sorted Options List */} +
+ {filteredCountryCodes.length === 0 ? ( +
+ No country codes found matching “{searchQuery}” +
+ ) : ( + filteredCountryCodes.map((cc) => { + const selected = isSelected(cc); + return ( + + ); + }) + )} +
+
+ )}
{/* Numeric-Only Local Phone Input */} diff --git a/src/utils/validation.ts b/src/utils/validation.ts index d1bf424..59d2625 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -55,7 +55,7 @@ export const validatePhoneNumber = ( // Resolve country rule: prioritize explicit country name, fallback to dialCode let rule = country ? getCountryRuleByName(country) : undefined; - if (!rule && dialCode) { + if ((!rule || rule.country === 'Other') && dialCode) { rule = getCountryRuleByDialCode(dialCode, country); } if (!rule) { @@ -181,6 +181,18 @@ export const parsePhoneNumber = ( // Fallback regex match from longest dial code const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length); + + // If fallbackCountry is specified and matches the prefix, prioritize it + if (fallbackCountry && trimmed.startsWith(defaultRule.code)) { + const rawLocal = trimmed.slice(defaultRule.code.length).replace(/\D/g, ''); + return { + country: defaultRule.country, + dialCode: defaultRule.code, + localDigits: rawLocal.slice(0, defaultRule.maxLength), + iso2: defaultRule.iso2, + }; + } + const matched = sorted.find((c) => trimmed.startsWith(c.code)); if (matched) { const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, '');