Compare commits
3 Commits
a1c127e684
...
2513c85411
| Author | SHA1 | Date |
|---|---|---|
|
|
2513c85411 | |
|
|
62c7a4aead | |
|
|
7e2f3ed927 |
|
|
@ -92,7 +92,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
|
|
||||||
const handleLocalPhoneChange = (newLocal: string) => {
|
const handleLocalPhoneChange = (newLocal: string) => {
|
||||||
setLocalPhone(newLocal);
|
setLocalPhone(newLocal);
|
||||||
setProfilePhone(formatE164Phone(dialCode, newLocal));
|
setProfilePhone(newLocal ? formatE164Phone(dialCode, newLocal) : '');
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
||||||
|
|
@ -192,6 +192,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
onPhoneChange={handleLocalPhoneChange}
|
onPhoneChange={handleLocalPhoneChange}
|
||||||
theme="light"
|
theme="light"
|
||||||
disabled={profileSaving}
|
disabled={profileSaving}
|
||||||
|
error={profileError && profileError.toLowerCase().includes('phone') ? profileError : null}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react';
|
import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react';
|
||||||
import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types';
|
import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types';
|
||||||
|
import { isValidPhoneNumber } from '../../utils/validation';
|
||||||
|
|
||||||
interface CartDrawerProps {
|
interface CartDrawerProps {
|
||||||
cart: ModuleItem[];
|
cart: ModuleItem[];
|
||||||
|
|
@ -113,36 +114,55 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="space-y-3 max-h-[220px] overflow-y-auto pr-1">
|
<div className="space-y-3 max-h-[220px] overflow-y-auto pr-1">
|
||||||
{cart.map((item) => (
|
{cart.map((item) => {
|
||||||
<div
|
const isCompletePackage =
|
||||||
key={item.code}
|
item.code === 'POINTS_PACK' ||
|
||||||
className="flex justify-between items-center gap-4 bg-slate-50 border border-slate-200/60 p-3 rounded-2xl"
|
item.code === 'PATTERN_CORE' ||
|
||||||
>
|
item.code === 'NUTRITION_CORE' ||
|
||||||
<div className="flex-1 min-w-0">
|
item.code === 'THERAPEUTICS_CORE' ||
|
||||||
<p className="text-xs font-bold text-slate-800 truncate">{item.name}</p>
|
item.name.toLowerCase().includes('complete pack') ||
|
||||||
<p className="text-[9px] text-slate-400 tracking-wider uppercase font-semibold">{item.category}</p>
|
item.name.toLowerCase().includes('complete package') ||
|
||||||
|
item.name.toLowerCase().includes('complete bundle') ||
|
||||||
|
item.category === 'Bundle';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={item.code}
|
||||||
|
className="flex justify-between items-center gap-4 bg-slate-50 border border-slate-200/60 p-3 rounded-2xl"
|
||||||
|
>
|
||||||
|
<div className="flex-1 min-w-0">
|
||||||
|
<div className="flex items-center gap-1.5 flex-wrap min-w-0">
|
||||||
|
<p className="text-xs font-bold text-slate-800 truncate">{item.name}</p>
|
||||||
|
{isCompletePackage && (
|
||||||
|
<span className="text-[8px] bg-gradient-to-r from-indigo-500 to-purple-600 text-white font-extrabold px-1.5 py-0.5 rounded tracking-wider uppercase flex-shrink-0">
|
||||||
|
Complete Pack
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<p className="text-[9px] text-slate-400 tracking-wider uppercase font-semibold">{item.category}</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<span className="text-xs font-bold text-indigo-600">
|
||||||
|
{(() => {
|
||||||
|
const calculatedItem = activePricing?.cart_items?.find((ci: any) => ci.code === item.code);
|
||||||
|
const price = calculatedItem
|
||||||
|
? calculatedItem.price
|
||||||
|
: (currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd);
|
||||||
|
const symbol = currency === 'INR' ? '₹' : currency === 'EUR' ? '€' : '$';
|
||||||
|
return `${symbol}${price}`;
|
||||||
|
})()}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
onClick={() => toggleCartItem(item)}
|
||||||
|
className="h-7 w-7 rounded-lg bg-rose-50 text-rose-500 hover:bg-rose-100 flex items-center justify-center transition-colors"
|
||||||
|
title="Remove item"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3.5 w-3.5" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-3">
|
);
|
||||||
<span className="text-xs font-bold text-indigo-600">
|
})}
|
||||||
{(() => {
|
|
||||||
const calculatedItem = activePricing?.cart_items?.find((ci: any) => ci.code === item.code);
|
|
||||||
const price = calculatedItem
|
|
||||||
? calculatedItem.price
|
|
||||||
: (currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd);
|
|
||||||
const symbol = currency === 'INR' ? '₹' : currency === 'EUR' ? '€' : '$';
|
|
||||||
return `${symbol}${price}`;
|
|
||||||
})()}
|
|
||||||
</span>
|
|
||||||
<button
|
|
||||||
onClick={() => toggleCartItem(item)}
|
|
||||||
className="h-7 w-7 rounded-lg bg-rose-50 text-rose-500 hover:bg-rose-100 flex items-center justify-center transition-colors"
|
|
||||||
title="Remove item"
|
|
||||||
>
|
|
||||||
<Trash2 className="h-3.5 w-3.5" />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|
@ -332,12 +352,12 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
|
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
|
||||||
Billing Profile
|
Billing Profile
|
||||||
</span>
|
</span>
|
||||||
{!loggedInUser.phone_number && (
|
{(!loggedInUser.phone_number || !isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India')) && (
|
||||||
<button
|
<button
|
||||||
onClick={onUpdateBillingPhone}
|
onClick={onUpdateBillingPhone}
|
||||||
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
|
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
|
||||||
>
|
>
|
||||||
Add Number
|
{loggedInUser.phone_number ? 'Update Number' : 'Add Number'}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -349,7 +369,11 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
<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">
|
||||||
{loggedInUser.phone_number || (
|
{loggedInUser.phone_number && isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India') ? (
|
||||||
|
loggedInUser.phone_number
|
||||||
|
) : loggedInUser.phone_number ? (
|
||||||
|
<span className="text-amber-600 font-black italic">Incomplete ({loggedInUser.phone_number})</span>
|
||||||
|
) : (
|
||||||
<span className="text-rose-500 font-black italic">Required for checkout</span>
|
<span className="text-rose-500 font-black italic">Required for checkout</span>
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,38 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
// Strictly numeric, capped at maxLength
|
const inputVal = e.target.value;
|
||||||
const cleaned = cleanPhoneInput(e.target.value, activeRule.maxLength, false);
|
const trimmed = inputVal.trim();
|
||||||
|
|
||||||
|
// Check if input contains an explicit international prefix '+'
|
||||||
|
if (trimmed.startsWith('+')) {
|
||||||
|
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||||
|
const matched = sorted.find((c) => trimmed.startsWith(c.code));
|
||||||
|
if (matched) {
|
||||||
|
if (onDialCodeChange && matched.code !== dialCode) {
|
||||||
|
onDialCodeChange(matched.code, matched.country);
|
||||||
|
}
|
||||||
|
const rest = trimmed.slice(matched.code.length).replace(/\D/g, '');
|
||||||
|
onPhoneChange(rest.slice(0, matched.maxLength));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if input was pasted with dial code digits without '+' (e.g. 919876543210)
|
||||||
|
const dialDigits = activeRule.code.replace(/\D/g, '');
|
||||||
|
const rawDigits = trimmed.replace(/\D/g, '');
|
||||||
|
if (
|
||||||
|
dialDigits &&
|
||||||
|
rawDigits.startsWith(dialDigits) &&
|
||||||
|
rawDigits.length > activeRule.maxLength
|
||||||
|
) {
|
||||||
|
const rest = rawDigits.slice(dialDigits.length);
|
||||||
|
onPhoneChange(rest.slice(0, activeRule.maxLength));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strictly numeric local digits, capped at active country's maxLength
|
||||||
|
const cleaned = cleanPhoneInput(inputVal, activeRule.maxLength, false);
|
||||||
onPhoneChange(cleaned);
|
onPhoneChange(cleaned);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -63,6 +93,10 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Allow '+' as leading character to support international number paste/typing
|
||||||
|
if (e.key === '+' && (e.currentTarget.selectionStart === 0 || !phone)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Block any non-digit
|
// Block any non-digit
|
||||||
if (!/^\d$/.test(e.key)) {
|
if (!/^\d$/.test(e.key)) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,11 @@ export const validatePhoneNumber = (
|
||||||
if ((!rule || rule.country === 'Other') && country) {
|
if ((!rule || rule.country === 'Other') && country) {
|
||||||
rule = getCountryRuleByName(country);
|
rule = getCountryRuleByName(country);
|
||||||
}
|
}
|
||||||
|
// If still not found and input starts with '+', match from COUNTRY_CODES
|
||||||
|
if (!rule && trimmed.startsWith('+')) {
|
||||||
|
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||||
|
rule = sorted.find((c) => trimmed.startsWith(c.code));
|
||||||
|
}
|
||||||
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
||||||
if (!rule || rule.country === 'Other') {
|
if (!rule || rule.country === 'Other') {
|
||||||
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
||||||
|
|
@ -62,12 +67,14 @@ export const validatePhoneNumber = (
|
||||||
const dialDigits = rule.code.replace(/\D/g, '');
|
const dialDigits = rule.code.replace(/\D/g, '');
|
||||||
let localDigits = allDigits;
|
let localDigits = allDigits;
|
||||||
|
|
||||||
// If formatted with country code (e.g. +919876543210 or 919876543210 where total length > rule.maxLength), strip country prefix
|
if (trimmed.startsWith('+')) {
|
||||||
if (
|
// Input includes country code prefix (e.g. +919876543210 or +9198765432)
|
||||||
(trimmed.startsWith('+') || allDigits.length > rule.maxLength) &&
|
// Must strip the dial code to extract the actual local number
|
||||||
allDigits.startsWith(dialDigits) &&
|
if (allDigits.startsWith(dialDigits)) {
|
||||||
allDigits.length === dialDigits.length + rule.minLength
|
localDigits = allDigits.slice(dialDigits.length);
|
||||||
) {
|
}
|
||||||
|
} else if (allDigits.length > rule.maxLength && allDigits.startsWith(dialDigits)) {
|
||||||
|
// Input was formatted with dial digits without '+' and exceeds local maxLength (e.g. 919876543210)
|
||||||
localDigits = allDigits.slice(dialDigits.length);
|
localDigits = allDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,11 +144,15 @@ export const parsePhoneNumber = (
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no '+' prefix, check if digits start with fallback country dial digits
|
// If no '+' prefix, check if digits start with fallback country dial digits and exceed national length
|
||||||
const dialDigits = defaultRule.code.replace(/\D/g, '');
|
const dialDigits = defaultRule.code.replace(/\D/g, '');
|
||||||
const rawDigits = trimmed.replace(/\D/g, '');
|
const rawDigits = trimmed.replace(/\D/g, '');
|
||||||
let local = rawDigits;
|
let local = rawDigits;
|
||||||
if (rawDigits.startsWith(dialDigits) && rawDigits.length > dialDigits.length) {
|
if (
|
||||||
|
dialDigits &&
|
||||||
|
rawDigits.startsWith(dialDigits) &&
|
||||||
|
rawDigits.length > defaultRule.maxLength
|
||||||
|
) {
|
||||||
local = rawDigits.slice(dialDigits.length);
|
local = rawDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue