145 lines
4.9 KiB
TypeScript
145 lines
4.9 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { X, Phone, ShieldCheck, RefreshCw } from 'lucide-react';
|
|
import { PhoneInput } from '../common/PhoneInput';
|
|
import { parsePhoneNumber, validatePhoneNumber, formatE164Phone } from '../../utils/validation';
|
|
|
|
interface BillingPhoneModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
currentPhone?: string | null;
|
|
currentCountry?: string;
|
|
onSavePhone: (phoneE164: string, country: string) => Promise<boolean>;
|
|
}
|
|
|
|
export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
currentPhone,
|
|
currentCountry = 'India',
|
|
onSavePhone,
|
|
}) => {
|
|
const [dialCode, setDialCode] = useState('+91');
|
|
const [country, setCountry] = useState(currentCountry);
|
|
const [phone, setPhone] = useState('');
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
const parsed = parsePhoneNumber(currentPhone, currentCountry);
|
|
setCountry(parsed.country);
|
|
setDialCode(parsed.dialCode);
|
|
setPhone(parsed.localDigits);
|
|
setError(null);
|
|
setSaving(false);
|
|
}
|
|
}, [isOpen, currentPhone, currentCountry]);
|
|
|
|
if (!isOpen) return null;
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError(null);
|
|
|
|
if (!phone.trim()) {
|
|
setError('Please enter your mobile number.');
|
|
return;
|
|
}
|
|
|
|
const valError = validatePhoneNumber(phone, dialCode, country);
|
|
if (valError) {
|
|
setError(valError);
|
|
return;
|
|
}
|
|
|
|
setSaving(true);
|
|
const e164 = formatE164Phone(dialCode, phone, country);
|
|
try {
|
|
const success = await onSavePhone(e164, country);
|
|
if (success) {
|
|
onClose();
|
|
}
|
|
} catch (err: any) {
|
|
setError(err?.message || 'Failed to save billing phone number.');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/70 backdrop-blur-sm animate-fadeIn">
|
|
<div
|
|
className="bg-white rounded-3xl max-w-md w-full p-6 shadow-2xl border border-slate-100 space-y-5"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="flex justify-between items-start">
|
|
<div className="flex items-center gap-3">
|
|
<div className="h-10 w-10 rounded-2xl bg-indigo-50 border border-indigo-100 flex items-center justify-center text-indigo-600">
|
|
<Phone className="h-5 w-5" />
|
|
</div>
|
|
<div>
|
|
<h3 className="text-base font-black text-slate-800">Billing Contact Number</h3>
|
|
<p className="text-xs text-slate-400 font-medium">Required for Razorpay invoice & payment updates</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={onClose}
|
|
className="p-1 rounded-full text-slate-400 hover:text-slate-600 hover:bg-slate-100 transition-colors"
|
|
>
|
|
<X className="h-5 w-5" />
|
|
</button>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div className="space-y-1.5">
|
|
<label className="text-xs font-bold text-slate-600 flex items-center gap-1">
|
|
Mobile Number
|
|
</label>
|
|
<PhoneInput
|
|
dialCode={dialCode}
|
|
phone={phone}
|
|
country={country}
|
|
onDialCodeChange={(newCode, newCountry) => {
|
|
setDialCode(newCode);
|
|
setCountry(newCountry);
|
|
setError(null);
|
|
}}
|
|
onPhoneChange={(newPhone) => {
|
|
setPhone(newPhone);
|
|
setError(null);
|
|
}}
|
|
error={error}
|
|
theme="light"
|
|
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">
|
|
<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>
|
|
</div>
|
|
|
|
<div className="flex gap-3 pt-2">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
disabled={saving}
|
|
className="flex-1 py-2.5 px-4 rounded-xl text-xs font-bold bg-slate-100 hover:bg-slate-200 text-slate-600 transition-colors"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={saving}
|
|
className="flex-1 py-2.5 px-4 rounded-xl text-xs font-bold bg-indigo-600 hover:bg-indigo-500 text-white transition-colors flex items-center justify-center gap-1.5 shadow-md shadow-indigo-600/20 disabled:opacity-50"
|
|
>
|
|
{saving ? <RefreshCw className="h-4 w-4 animate-spin" /> : 'Save Phone Number'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|