import React, { useState, useCallback } from 'react'; import axios from 'axios'; const ForgotPasswordForm = ({ showLogin, showReset }) => { const [mode, setMode] = useState('email'); // 'email' or 'whatsapp' const [identifier, setIdentifier] = useState(''); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const [loading, setLoading] = useState(false); const [emailError, setEmailError] = useState(''); const validateEmail = (email) => { const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return regex.test(email); }; const handleIdentifierChange = (e) => { let value = e.target.value; if (mode === 'email') { if (!validateEmail(value) && value.length > 0) { setEmailError('Please enter a valid email address.'); } else { setEmailError(''); } } else { setEmailError(''); if (value.startsWith('0')) { value = value.substring(1); } value = value.replace(/[^0-9]/g, ''); } setIdentifier(value); }; const handleModeChange = useCallback((newMode) => { setMode(newMode); setIdentifier(''); setEmailError(''); setError(''); setSuccess(''); }, []); const handleSubmit = async (e) => { e.preventDefault(); if (mode === 'email' && emailError) { setError('Please fix the errors before submitting.'); return; } setLoading(true); setError(''); setSuccess(''); const finalIdentifier = mode === 'whatsapp' ? `62${identifier}` : identifier; try { await axios.post('https://api.karyamanswasta.my.id/webhook/forgot-password/adventure', { identifier: finalIdentifier }); setSuccess('Password reset link sent. Please check your email/WhatsApp.'); // showReset(); // You might want to navigate to reset password form after a delay } catch (err) { setError(err.response?.data?.message || 'Failed to send reset link.'); } finally { setLoading(false); } }; const isFormValid = identifier.trim() !== '' && !emailError; return ( <>
Enter your email or WhatsApp to reset