From 41e885a7c9ad165ff3428e4c431b4f8559430db9 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Tue, 17 Nov 2026 13:41:17 +0530 Subject: [PATCH] fix(billing): sync billing country with phone country and allow entering valid phone per country - Synchronize Billing Country and Phone dial code in BillingPhoneModal and ProfileDrawer - Reset phone and clear errors upon country change so user can enter a valid number per country rules - Display Billing Country in CartDrawer's Billing Profile prefill summary - Prioritize country-based rules over dial code in validation to support shared dial codes accurately --- src/components/auth/ProfileDrawer.tsx | 37 ++++++++------ src/components/cart/CartDrawer.tsx | 4 ++ src/components/checkout/BillingPhoneModal.tsx | 50 ++++++++++++++++--- src/utils/validation.ts | 19 +++++-- 4 files changed, 83 insertions(+), 27 deletions(-) diff --git a/src/components/auth/ProfileDrawer.tsx b/src/components/auth/ProfileDrawer.tsx index c0bddec..adceaac 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'; @@ -90,7 +90,7 @@ export const ProfileDrawer: React.FC = ({ setDialCode(p.dialCode); setLocalPhone(p.localDigits); } - }, [profilePhone, profileCountry, isOpen]); + }, [isOpen]); const handleLocalPhoneChange = (newLocal: string) => { setLocalPhone(newLocal); @@ -99,22 +99,28 @@ 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)); + if (newCountry !== profileCountry) { + setProfileCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + if (rule) { + setCurrency(rule.currency); + } + setLocalPhone(''); + setProfilePhone(''); + } }; 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)); + if (newCountry !== profileCountry) { + setProfileCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + if (rule) { + setDialCode(rule.code); + setCurrency(rule.currency); + } + setLocalPhone(''); + setProfilePhone(''); + } }; if (!isOpen || !loggedInUser) return null; @@ -190,6 +196,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 d0e2418..45e2d4d 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/utils/validation.ts b/src/utils/validation.ts index 0e904b0..f7bc4c0 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -46,10 +46,10 @@ export const validatePhoneNumber = ( return 'Please enter a valid phone number with numeric digits.'; } - // Resolve country rule by dial code or country name - let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined; - if ((!rule || rule.country === 'Other') && country) { - rule = getCountryRuleByName(country); + // Resolve country rule by country name first, or fallback to dial code + let rule = country ? getCountryRuleByName(country) : undefined; + if (!rule || rule.country === 'Other') { + rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined; } // If still not found and input starts with '+', match from COUNTRY_CODES if (!rule && trimmed.startsWith('+')) { @@ -134,6 +134,17 @@ export const parsePhoneNumber = ( // Try matching dial code prefixes from longest to shortest 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), + }; + } + const matched = sorted.find((c) => trimmed.startsWith(c.code)); if (matched) { const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, '');