import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Lock, ChevronLeft, CheckCircle2 } from 'lucide-react'; import { useAuth } from '../contexts/AuthContext'; import Header from '../components/Header'; import './ChangePinScreen.css'; const ChangePinScreen: React.FC = () => { const navigate = useNavigate(); const { changePin } = useAuth(); const [step, setStep] = useState<'current' | 'new' | 'success'>('current'); const [currentPin, setCurrentPin] = useState(''); const [newPin, setNewPin] = useState(''); const [confirmPin, setConfirmPin] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handleVerifyCurrent = (e: React.FormEvent) => { e.preventDefault(); if (currentPin.length !== 6) { setError('PIN must be 6 digits'); return; } setError(null); setStep('new'); }; const handleChangePin = async (e: React.FormEvent) => { e.preventDefault(); if (newPin.length !== 6) { setError('New PIN must be 6 digits'); return; } if (newPin !== confirmPin) { setError('New PINs do not match'); return; } if (newPin === currentPin) { setError('New PIN cannot be the same as current PIN'); return; } setIsLoading(true); setError(null); const result = await changePin(currentPin, newPin); setIsLoading(false); if (result.success) { setStep('success'); setTimeout(() => { navigate('/profile'); }, 2000); } else { setError(result.message); // If "incorrect current PIN", go back to step 1 if (result.message.toLowerCase().includes('current pin')) { setStep('current'); setCurrentPin(''); } } }; return (
navigate('/profile')} showCart={false} />
{step === 'current' && ( <>
Step 1 of 2

Verify Identity

Please enter your current 6-digit PIN to continue.

setCurrentPin(e.target.value.replace(/[^0-9]/g, '').slice(0, 6))} autoFocus />
{error &&
{error}
}
)} {step === 'new' && ( <>
Step 2 of 2

Set New PIN

Create a new 6-digit security PIN for your account.

setNewPin(e.target.value.replace(/[^0-9]/g, '').slice(0, 6))} autoFocus />
setConfirmPin(e.target.value.replace(/[^0-9]/g, '').slice(0, 6))} />
{error &&
{error}
}
)} {step === 'success' && (

PIN Updated!

Your security PIN has been changed successfully. Redirecting you to profile...

)}
); }; export default ChangePinScreen;