refactor(auth): make country and phone code configuration generic across store
- Render country selection options dynamically from COUNTRY_CODES in AuthModal and ProfileDrawer - Synchronize dial code, country, and currency generically using getCountryRuleByName and getCountryRuleByDialCode - Eliminate hardcoded country options and country-specific checks in favor of registry-driven rules
This commit is contained in:
parent
917dd258d2
commit
b476492bd8
22
src/App.tsx
22
src/App.tsx
|
|
@ -2,7 +2,7 @@ import { useState, useEffect } from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
import { CURRICULUM_MODULES } from './constants/modules';
|
import { CURRICULUM_MODULES } from './constants/modules';
|
||||||
import { COUNTRY_CODES } from './constants/countries';
|
import { getCountryRuleByName } from './constants/countries';
|
||||||
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
||||||
import { API_BASE } from './services/api';
|
import { API_BASE } from './services/api';
|
||||||
import { loadRazorpayScript } from './services/razorpay';
|
import { loadRazorpayScript } from './services/razorpay';
|
||||||
|
|
@ -216,16 +216,9 @@ export default function App() {
|
||||||
// Update currency and dial code automatically on country selection during registration
|
// Update currency and dial code automatically on country selection during registration
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authMode === 'register') {
|
if (authMode === 'register') {
|
||||||
if (authCountry === 'India') {
|
const rule = getCountryRuleByName(authCountry);
|
||||||
setCurrency('INR');
|
setCurrency(rule.currency);
|
||||||
setAuthDialCode('+91');
|
setAuthDialCode(rule.code);
|
||||||
} else {
|
|
||||||
setCurrency('USD');
|
|
||||||
const match = COUNTRY_CODES.find(c => c.country.toLowerCase() === authCountry.toLowerCase());
|
|
||||||
if (match) {
|
|
||||||
setAuthDialCode(match.code);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [authCountry, authMode]);
|
}, [authCountry, authMode]);
|
||||||
|
|
||||||
|
|
@ -247,11 +240,8 @@ export default function App() {
|
||||||
setProfileDisplayName(profileRes.data.display_name || '');
|
setProfileDisplayName(profileRes.data.display_name || '');
|
||||||
setProfilePhone(profileRes.data.phone_number || '');
|
setProfilePhone(profileRes.data.phone_number || '');
|
||||||
setProfileCountry(profileRes.data.country || 'India');
|
setProfileCountry(profileRes.data.country || 'India');
|
||||||
if (profileRes.data.country === 'India') {
|
const countryRule = getCountryRuleByName(profileRes.data.country || 'India');
|
||||||
setCurrency('INR');
|
setCurrency(countryRule.currency);
|
||||||
} else {
|
|
||||||
setCurrency('USD');
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to fetch user profile:', err);
|
console.error('Failed to fetch user profile:', err);
|
||||||
handleLogout();
|
handleLogout();
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import {
|
||||||
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
||||||
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { COUNTRY_CODES, getCountryRuleByDialCode } from '../../constants/countries';
|
import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries';
|
||||||
import { cleanPhoneInput } from '../../utils/validation';
|
import { cleanPhoneInput } from '../../utils/validation';
|
||||||
|
|
||||||
interface AuthModalProps {
|
interface AuthModalProps {
|
||||||
|
|
@ -295,9 +295,9 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const newCode = e.target.value;
|
const newCode = e.target.value;
|
||||||
setAuthDialCode(newCode);
|
setAuthDialCode(newCode);
|
||||||
const match = COUNTRY_CODES.find((c) => c.code === newCode);
|
const rule = getCountryRuleByDialCode(newCode);
|
||||||
if (match && match.country) {
|
if (rule && rule.country !== 'Other') {
|
||||||
setAuthCountry(match.country);
|
setAuthCountry(rule.country);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||||
|
|
@ -319,11 +319,9 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-[10px] text-slate-500 font-medium">
|
<p className="text-[10px] text-slate-500 font-medium">
|
||||||
{activeRule.country === 'India'
|
{activeRule.minLength === activeRule.maxLength
|
||||||
? '🇮🇳 India: Enter 10-digit mobile number'
|
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
||||||
: activeRule.minLength === activeRule.maxLength
|
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
||||||
? `${activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
|
||||||
: `${activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
@ -340,18 +338,18 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const newCountry = e.target.value;
|
const newCountry = e.target.value;
|
||||||
setAuthCountry(newCountry);
|
setAuthCountry(newCountry);
|
||||||
const match = COUNTRY_CODES.find((c) => c.country.toLowerCase() === newCountry.toLowerCase());
|
const rule = getCountryRuleByName(newCountry);
|
||||||
if (match) {
|
if (rule) {
|
||||||
setAuthDialCode(match.code);
|
setAuthDialCode(rule.code);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||||
>
|
>
|
||||||
<option value="India">India (Calculates in INR ₹)</option>
|
{COUNTRY_CODES.map((cc) => (
|
||||||
<option value="United States">United States (Calculates in USD $)</option>
|
<option key={cc.code} value={cc.country}>
|
||||||
<option value="United Kingdom">United Kingdom (Calculates in USD $)</option>
|
{cc.country} (Calculates in {cc.currency} {cc.currency === 'INR' ? '₹' : '$'})
|
||||||
<option value="Germany">Germany (Calculates in USD $)</option>
|
</option>
|
||||||
<option value="Australia">Australia (Calculates in USD $)</option>
|
))}
|
||||||
</select>
|
</select>
|
||||||
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-slate-500 text-xs">
|
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-slate-500 text-xs">
|
||||||
▼
|
▼
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import {
|
||||||
Eye, EyeOff, RefreshCw, LogOut
|
Eye, EyeOff, RefreshCw, LogOut
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||||
|
import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries';
|
||||||
import { cleanPhoneInput } from '../../utils/validation';
|
import { cleanPhoneInput } from '../../utils/validation';
|
||||||
|
|
||||||
interface ProfileDrawerProps {
|
interface ProfileDrawerProps {
|
||||||
|
|
@ -71,6 +72,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
}) => {
|
}) => {
|
||||||
const [showOldPassword, setShowOldPassword] = useState(false);
|
const [showOldPassword, setShowOldPassword] = useState(false);
|
||||||
const [showNewPassword, setShowNewPassword] = useState(false);
|
const [showNewPassword, setShowNewPassword] = useState(false);
|
||||||
|
const activeRule = getCountryRuleByName(profileCountry);
|
||||||
|
|
||||||
if (!isOpen || !loggedInUser) return null;
|
if (!isOpen || !loggedInUser) return null;
|
||||||
|
|
||||||
|
|
@ -144,11 +146,16 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={profilePhone}
|
value={profilePhone}
|
||||||
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value))}
|
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
|
||||||
placeholder="+919876543210"
|
placeholder={activeRule.placeholder}
|
||||||
maxLength={18}
|
maxLength={activeRule.maxLength}
|
||||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
|
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
|
||||||
/>
|
/>
|
||||||
|
<p className="text-[10px] text-slate-400 font-medium">
|
||||||
|
{activeRule.minLength === activeRule.maxLength
|
||||||
|
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
||||||
|
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
|
|
@ -156,25 +163,18 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
<select
|
<select
|
||||||
value={profileCountry}
|
value={profileCountry}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setProfileCountry(e.target.value);
|
const newCountry = e.target.value;
|
||||||
if (e.target.value === 'India') {
|
setProfileCountry(newCountry);
|
||||||
setCurrency('INR');
|
const rule = getCountryRuleByName(newCountry);
|
||||||
} else {
|
setCurrency(rule.currency);
|
||||||
setCurrency('USD');
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
|
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||||
>
|
>
|
||||||
<option value="India">India</option>
|
{COUNTRY_CODES.map((cc) => (
|
||||||
<option value="United States">United States</option>
|
<option key={cc.code} value={cc.country}>
|
||||||
<option value="United Kingdom">United Kingdom</option>
|
{cc.country}
|
||||||
<option value="Australia">Australia</option>
|
</option>
|
||||||
<option value="Germany">Germany</option>
|
))}
|
||||||
<option value="France">France</option>
|
|
||||||
<option value="Japan">Japan</option>
|
|
||||||
<option value="China">China</option>
|
|
||||||
<option value="Singapore">Singapore</option>
|
|
||||||
<option value="Canada">Canada</option>
|
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,42 @@
|
||||||
|
import type { Currency } from '../types';
|
||||||
|
|
||||||
export interface CountryCode {
|
export interface CountryCode {
|
||||||
code: string;
|
code: string;
|
||||||
label: string;
|
label: string;
|
||||||
country: string;
|
country: string;
|
||||||
|
currency: Currency;
|
||||||
minLength: number;
|
minLength: number;
|
||||||
maxLength: number;
|
maxLength: number;
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const COUNTRY_CODES: CountryCode[] = [
|
export const COUNTRY_CODES: CountryCode[] = [
|
||||||
{ code: '+91', label: '🇮🇳 +91', country: 'India', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
{ code: '+91', label: '🇮🇳 +91', country: 'India', currency: 'INR', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
||||||
{ code: '+1', label: '🇺🇸 +1', country: 'United States', minLength: 10, maxLength: 10, placeholder: '10-digit phone number' },
|
{ code: '+1', label: '🇺🇸 +1', country: 'United States', currency: 'USD', minLength: 10, maxLength: 10, placeholder: '10-digit phone number' },
|
||||||
{ code: '+44', label: '🇬🇧 +44', country: 'United Kingdom', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+44', label: '🇬🇧 +44', country: 'United Kingdom', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||||
{ code: '+61', label: '🇦🇺 +61', country: 'Australia', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+61', label: '🇦🇺 +61', country: 'Australia', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||||
{ code: '+49', label: '🇩🇪 +49', country: 'Germany', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+49', label: '🇩🇪 +49', country: 'Germany', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||||
{ code: '+33', label: '🇫🇷 +33', country: 'France', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+33', label: '🇫🇷 +33', country: 'France', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||||
{ code: '+81', label: '🇯🇵 +81', country: 'Japan', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+81', label: '🇯🇵 +81', country: 'Japan', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||||
{ code: '+86', label: '🇨🇳 +86', country: 'China', minLength: 11, maxLength: 11, placeholder: '11-digit mobile number' },
|
{ code: '+86', label: '🇨🇳 +86', country: 'China', currency: 'USD', minLength: 11, maxLength: 11, placeholder: '11-digit mobile number' },
|
||||||
{ code: '+971', label: '🇦🇪 +971', country: 'UAE', minLength: 9, maxLength: 9, placeholder: '9-digit mobile number' },
|
{ code: '+971', label: '🇦🇪 +971', country: 'UAE', currency: 'USD', minLength: 9, maxLength: 9, placeholder: '9-digit mobile number' },
|
||||||
{ code: '+65', label: '🇸🇬 +65', country: 'Singapore', minLength: 8, maxLength: 8, placeholder: '8-digit mobile number' },
|
{ code: '+65', label: '🇸🇬 +65', country: 'Singapore', currency: 'USD', minLength: 8, maxLength: 8, placeholder: '8-digit mobile number' },
|
||||||
{ code: '+55', label: '🇧🇷 +55', country: 'Brazil', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+55', label: '🇧🇷 +55', country: 'Brazil', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||||
{ code: '+82', label: '🇰🇷 +82', country: 'South Korea', minLength: 9, maxLength: 11, placeholder: '9 to 11 digits' },
|
{ code: '+82', label: '🇰🇷 +82', country: 'South Korea', currency: 'USD', minLength: 9, maxLength: 11, placeholder: '9 to 11 digits' },
|
||||||
{ code: '+7', label: '🇷🇺 +7', country: 'Russia', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
{ code: '+7', label: '🇷🇺 +7', country: 'Russia', currency: 'USD', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
||||||
{ code: '+27', label: '🇿🇦 +27', country: 'South Africa', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+27', label: '🇿🇦 +27', country: 'South Africa', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
||||||
{ code: '+234', label: '🇳🇬 +234', country: 'Nigeria', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+234', label: '🇳🇬 +234', country: 'Nigeria', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const DEFAULT_COUNTRY = COUNTRY_CODES[0]; // India (+91)
|
||||||
|
|
||||||
export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
||||||
return (
|
return (
|
||||||
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
||||||
code: dialCode,
|
code: dialCode,
|
||||||
label: dialCode,
|
label: dialCode,
|
||||||
country: 'Other',
|
country: 'Other',
|
||||||
|
currency: 'USD',
|
||||||
minLength: 6,
|
minLength: 6,
|
||||||
maxLength: 15,
|
maxLength: 15,
|
||||||
placeholder: 'Phone number',
|
placeholder: 'Phone number',
|
||||||
|
|
@ -39,7 +45,7 @@ export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCountryRuleByName = (countryName: string): CountryCode => {
|
export const getCountryRuleByName = (countryName: string): CountryCode => {
|
||||||
if (!countryName) return COUNTRY_CODES[0];
|
if (!countryName) return DEFAULT_COUNTRY;
|
||||||
const normalized = countryName.toLowerCase();
|
const normalized = countryName.toLowerCase();
|
||||||
return (
|
return (
|
||||||
COUNTRY_CODES.find(
|
COUNTRY_CODES.find(
|
||||||
|
|
@ -54,6 +60,7 @@ export const getCountryRuleByName = (countryName: string): CountryCode => {
|
||||||
code: '+91',
|
code: '+91',
|
||||||
label: '🇮🇳 +91',
|
label: '🇮🇳 +91',
|
||||||
country: countryName,
|
country: countryName,
|
||||||
|
currency: normalized === 'india' ? 'INR' : 'USD',
|
||||||
minLength: 6,
|
minLength: 6,
|
||||||
maxLength: 15,
|
maxLength: 15,
|
||||||
placeholder: 'Phone number',
|
placeholder: 'Phone number',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue