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
This commit is contained in:
parent
1567a96c54
commit
41e885a7c9
|
|
@ -5,7 +5,7 @@ import {
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||||
import { getCountryRuleByName } from '../../constants/countries';
|
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 { PhoneInput } from '../common/PhoneInput';
|
||||||
import { CountrySelect } from '../common/CountrySelect';
|
import { CountrySelect } from '../common/CountrySelect';
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
setDialCode(p.dialCode);
|
setDialCode(p.dialCode);
|
||||||
setLocalPhone(p.localDigits);
|
setLocalPhone(p.localDigits);
|
||||||
}
|
}
|
||||||
}, [profilePhone, profileCountry, isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
const handleLocalPhoneChange = (newLocal: string) => {
|
const handleLocalPhoneChange = (newLocal: string) => {
|
||||||
setLocalPhone(newLocal);
|
setLocalPhone(newLocal);
|
||||||
|
|
@ -99,22 +99,28 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
|
|
||||||
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
||||||
setDialCode(newCode);
|
setDialCode(newCode);
|
||||||
setProfileCountry(newCountry);
|
if (newCountry !== profileCountry) {
|
||||||
const rule = getCountryRuleByName(newCountry);
|
setProfileCountry(newCountry);
|
||||||
setCurrency(rule.currency);
|
const rule = getCountryRuleByName(newCountry);
|
||||||
const clamped = cleanPhoneInput(localPhone, rule.maxLength, false);
|
if (rule) {
|
||||||
setLocalPhone(clamped);
|
setCurrency(rule.currency);
|
||||||
setProfilePhone(formatE164Phone(newCode, clamped));
|
}
|
||||||
|
setLocalPhone('');
|
||||||
|
setProfilePhone('');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCountrySelectChange = (newCountry: string) => {
|
const handleCountrySelectChange = (newCountry: string) => {
|
||||||
setProfileCountry(newCountry);
|
if (newCountry !== profileCountry) {
|
||||||
const rule = getCountryRuleByName(newCountry);
|
setProfileCountry(newCountry);
|
||||||
setDialCode(rule.code);
|
const rule = getCountryRuleByName(newCountry);
|
||||||
setCurrency(rule.currency);
|
if (rule) {
|
||||||
const clamped = cleanPhoneInput(localPhone, rule.maxLength, false);
|
setDialCode(rule.code);
|
||||||
setLocalPhone(clamped);
|
setCurrency(rule.currency);
|
||||||
setProfilePhone(formatE164Phone(rule.code, clamped));
|
}
|
||||||
|
setLocalPhone('');
|
||||||
|
setProfilePhone('');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!isOpen || !loggedInUser) return null;
|
if (!isOpen || !loggedInUser) return null;
|
||||||
|
|
@ -190,6 +196,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
dialCode={dialCode}
|
dialCode={dialCode}
|
||||||
phone={localPhone}
|
phone={localPhone}
|
||||||
country={profileCountry}
|
country={profileCountry}
|
||||||
|
clearOnDialCodeChange={true}
|
||||||
onDialCodeChange={handleDialCodeChange}
|
onDialCodeChange={handleDialCodeChange}
|
||||||
onPhoneChange={handleLocalPhoneChange}
|
onPhoneChange={handleLocalPhoneChange}
|
||||||
theme="light"
|
theme="light"
|
||||||
|
|
|
||||||
|
|
@ -366,6 +366,10 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
<span>Email:</span>
|
<span>Email:</span>
|
||||||
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.email}</span>
|
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.email}</span>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||||||
|
<span>Billing Country:</span>
|
||||||
|
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.country || 'India'}</span>
|
||||||
|
</div>
|
||||||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||||||
<span>Phone:</span>
|
<span>Phone:</span>
|
||||||
<span className="text-slate-800 font-bold">
|
<span className="text-slate-800 font-bold">
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import React, { useState, useEffect } from 'react';
|
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 { PhoneInput } from '../common/PhoneInput';
|
||||||
|
import { CountrySelect } from '../common/CountrySelect';
|
||||||
|
import { getCountryRuleByName } from '../../constants/countries';
|
||||||
import { parsePhoneNumber, validatePhoneNumber, formatE164Phone } from '../../utils/validation';
|
import { parsePhoneNumber, validatePhoneNumber, formatE164Phone } from '../../utils/validation';
|
||||||
|
|
||||||
interface BillingPhoneModalProps {
|
interface BillingPhoneModalProps {
|
||||||
|
|
@ -37,6 +39,27 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
||||||
|
|
||||||
if (!isOpen) return null;
|
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) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
@ -99,11 +122,8 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
||||||
dialCode={dialCode}
|
dialCode={dialCode}
|
||||||
phone={phone}
|
phone={phone}
|
||||||
country={country}
|
country={country}
|
||||||
onDialCodeChange={(newCode, newCountry) => {
|
clearOnDialCodeChange={true}
|
||||||
setDialCode(newCode);
|
onDialCodeChange={handleDialCodeChange}
|
||||||
setCountry(newCountry);
|
|
||||||
setError(null);
|
|
||||||
}}
|
|
||||||
onPhoneChange={(newPhone) => {
|
onPhoneChange={(newPhone) => {
|
||||||
setPhone(newPhone);
|
setPhone(newPhone);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
@ -114,9 +134,24 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<label className="text-xs font-bold text-slate-600 flex items-center gap-1">
|
||||||
|
<Globe className="h-3.5 w-3.5 text-indigo-500" /> Billing Country
|
||||||
|
</label>
|
||||||
|
<CountrySelect
|
||||||
|
value={country}
|
||||||
|
onChange={handleCountryChange}
|
||||||
|
theme="light"
|
||||||
|
size="md"
|
||||||
|
showDialCode={true}
|
||||||
|
showCurrency={false}
|
||||||
|
disabled={saving}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-2 text-[11px] text-slate-500 bg-slate-50 p-3 rounded-xl border border-slate-200/60">
|
<div className="flex items-center gap-2 text-[11px] text-slate-500 bg-slate-50 p-3 rounded-xl border border-slate-200/60">
|
||||||
<ShieldCheck className="h-4 w-4 text-emerald-500 flex-shrink-0" />
|
<ShieldCheck className="h-4 w-4 text-emerald-500 flex-shrink-0" />
|
||||||
<span>This contact number will appear on your compliance tax invoice and payment receipts.</span>
|
<span>This contact number and billing country will appear on your compliance tax invoice and payment receipts.</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-3 pt-2">
|
<div className="flex gap-3 pt-2">
|
||||||
|
|
@ -141,4 +176,3 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@ export const validatePhoneNumber = (
|
||||||
return 'Please enter a valid phone number with numeric digits.';
|
return 'Please enter a valid phone number with numeric digits.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve country rule by dial code or country name
|
// Resolve country rule by country name first, or fallback to dial code
|
||||||
let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined;
|
let rule = country ? getCountryRuleByName(country) : undefined;
|
||||||
if ((!rule || rule.country === 'Other') && country) {
|
if (!rule || rule.country === 'Other') {
|
||||||
rule = getCountryRuleByName(country);
|
rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined;
|
||||||
}
|
}
|
||||||
// If still not found and input starts with '+', match from COUNTRY_CODES
|
// If still not found and input starts with '+', match from COUNTRY_CODES
|
||||||
if (!rule && trimmed.startsWith('+')) {
|
if (!rule && trimmed.startsWith('+')) {
|
||||||
|
|
@ -134,6 +134,17 @@ export const parsePhoneNumber = (
|
||||||
|
|
||||||
// Try matching dial code prefixes from longest to shortest
|
// Try matching dial code prefixes from longest to shortest
|
||||||
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
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));
|
const matched = sorted.find((c) => trimmed.startsWith(c.code));
|
||||||
if (matched) {
|
if (matched) {
|
||||||
const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, '');
|
const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, '');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue