import React, { useState, useRef, useCallback } from 'react'; import { sendOtp } from '../core/checkout-api'; import type { PhoneStepProps } from '../core/types'; /** Strip everything except digits, enforce 10-digit Indian mobile */ function normaliseMobile(raw: string): string { const digits = raw.replace(/\D/g, '').slice(0, 10); return digits; } const PhoneStep: React.FC = ({ onOtpSent, onSkip, functionBaseUrl, merchantId, }) => { const [mobile, setMobile] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const inputRef = useRef(null); const handleChange = useCallback((e: React.ChangeEvent) => { setMobile(normaliseMobile(e.target.value)); setError(null); }, []); const handleSubmit = useCallback(async (e: React.FormEvent) => { e.preventDefault(); if (mobile.length !== 10) { setError('Enter a valid 10-digit mobile number.'); inputRef.current?.focus(); return; } setLoading(true); setError(null); try { await sendOtp({ functionBaseUrl, merchantId }, `+91${mobile}`); onOtpSent(`+91${mobile}`); } catch (err: unknown) { setError(err instanceof Error ? err.message : 'Failed to send OTP. Please try again.'); } finally { setLoading(false); } }, [mobile, functionBaseUrl, merchantId, onOtpSent]); return (
{/* Icon */}

Faster checkout

Enter your mobile to load saved addresses

{error && ( )}
); }; export default PhoneStep;