import React, { useState, useEffect } from 'react'; import { Head, Link, router } from '@inertiajs/react'; import axios from 'axios'; import { toast } from 'sonner'; import { ArrowLeft, MailCheck, Loader2, RefreshCw, KeyRound, Users, DollarSign, MessageSquare } from 'lucide-react'; export default function VerifyEmail({ email }) { const [otp, setOtp] = useState(''); const [loading, setLoading] = useState(false); const [resending, setResending] = useState(false); const [errors, setErrors] = useState({}); // Cooldown timer state for resending OTP (60s) const [cooldown, setCooldown] = useState(60); useEffect(() => { let timer; if (cooldown > 0) { timer = setTimeout(() => setCooldown(cooldown - 1), 1000); } return () => clearTimeout(timer); }, [cooldown]); const handleOtpChange = (e) => { const val = e.target.value.replace(/[^0-9]/g, '').slice(0, 6); setOtp(val); if (errors.otp) { setErrors({}); } }; const handleSubmit = (e) => { e.preventDefault(); if (otp.length !== 6) { setErrors({ otp: 'Please enter all 6 digits of the code.' }); return; } router.post('/employer/verify-email', { otp }, { onStart: () => { setLoading(true); setErrors({}); }, onFinish: () => { setLoading(false); }, onSuccess: () => { toast.success('Email verified successfully! Please upload your Emirates ID.'); }, onError: (err) => { // Inertia passes validation errors directly in the err object const otpErr = err.otp || 'Verification failed.'; setErrors({ otp: otpErr }); toast.error(otpErr); } }); }; const handleResend = async () => { if (cooldown > 0 || resending) return; setResending(true); try { await axios.post('/employer/resend-otp'); toast.success('A new verification code has been dispatched to your email.'); setCooldown(60); // Reset timer setOtp(''); } catch (err) { if (err.response) { toast.error(err.response.data?.message || 'Failed to dispatch verification code.'); if (err.response.status === 429) { setCooldown(60); // Reset cooldown on too many requests } } else { toast.error('Network error. Please check your connection.'); } } finally { setResending(false); } }; const renderStepIndicator = () => (
Unlock direct access to top domestic candidates with a Migrant premium subscription. Find, interview, and hire directly without middlemen.
Verify your employer registration
We sent a 6-digit verification code to: {email}