import { json, redirect, type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/node"; import { Form, useActionData, useLoaderData, useNavigation, Link } from "@remix-run/react"; import { useState, useEffect, useRef } from "react"; import { Card, CardContent, CardHeader } from "~/components/ui/card"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Alert, AlertDescription } from "~/components/ui/alert"; import { MessageSquare, ArrowLeft, ArrowRight, AlertCircle, Loader2, Clock, RefreshCw, CheckCircle, Smartphone } from "lucide-react"; // Progress Indicator Component (reuse dari step sebelumnya) const ProgressIndicator = ({ currentStep = 2, totalSteps = 5 }) => { return (
{Array.from({ length: totalSteps }, (_, index) => { const stepNumber = index + 1; const isActive = stepNumber === currentStep; const isCompleted = stepNumber < currentStep; return (
{isCompleted ? : stepNumber}
{stepNumber < totalSteps && (
)}
); })}
); }; // Interfaces interface LoaderData { phone: string; otpSentAt: string; expiryMinutes: number; } interface VerifyOTPActionData { success?: boolean; message?: string; otpSentAt?: string; errors?: { otp?: string; general?: string; }; } export const loader = async ({ request }: LoaderFunctionArgs): Promise => { const url = new URL(request.url); const phone = url.searchParams.get("phone"); if (!phone) { return redirect("/authpengelola/requestotpforregister"); } return json({ phone, otpSentAt: new Date().toISOString(), expiryMinutes: 5 }); }; export const action = async ({ request }: ActionFunctionArgs): Promise => { const formData = await request.formData(); const otp = formData.get("otp") as string; const phone = formData.get("phone") as string; const actionType = formData.get("_action") as string; if (actionType === "resend") { // Simulasi resend OTP console.log("Resending OTP to WhatsApp:", phone); return json({ success: true, message: "Kode OTP baru telah dikirim ke WhatsApp Anda", otpSentAt: new Date().toISOString() }); } if (actionType === "verify") { // Validation const errors: { otp?: string; general?: string } = {}; if (!otp || otp.length !== 4) { errors.otp = "Kode OTP harus 4 digit"; } else if (!/^\d{4}$/.test(otp)) { errors.otp = "Kode OTP hanya boleh berisi angka"; } if (Object.keys(errors).length > 0) { return json({ errors }, { status: 400 }); } // Simulasi verifikasi OTP - dalam implementasi nyata, cek ke database/cache if (otp === "1234") { // OTP valid, lanjut ke step berikutnya return redirect( `/authpengelola/completingcompanyprofile?phone=${encodeURIComponent( phone )}` ); } return json( { errors: { otp: "Kode OTP tidak valid atau sudah kedaluwarsa" } }, { status: 401 } ); } return json( { errors: { general: "Aksi tidak valid" } }, { status: 400 } ); }; export default function VerifyOTPToRegister() { const { phone, otpSentAt, expiryMinutes } = useLoaderData(); const actionData = useActionData(); const navigation = useNavigation(); const [otp, setOtp] = useState(["", "", "", ""]); const [timeLeft, setTimeLeft] = useState(expiryMinutes * 60); // 5 minutes in seconds const [canResend, setCanResend] = useState(false); const inputRefs = useRef<(HTMLInputElement | null)[]>([]); const isSubmitting = navigation.state === "submitting"; const isResending = navigation.formData?.get("_action") === "resend"; const isVerifying = navigation.formData?.get("_action") === "verify"; // Timer countdown useEffect(() => { const timer = setInterval(() => { setTimeLeft((prev) => { if (prev <= 1) { setCanResend(true); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(timer); }, []); // Reset timer when OTP is resent useEffect(() => { if (actionData?.success && actionData?.otpSentAt) { setTimeLeft(expiryMinutes * 60); setCanResend(false); } }, [actionData, expiryMinutes]); // Handle OTP input change const handleOtpChange = (index: number, value: string) => { if (!/^\d*$/.test(value)) return; // Only allow digits const newOtp = [...otp]; newOtp[index] = value; setOtp(newOtp); // Auto-focus next input if (value && index < 3) { inputRefs.current[index + 1]?.focus(); } }; // Handle key down (backspace) const handleKeyDown = (index: number, e: React.KeyboardEvent) => { if (e.key === "Backspace" && !otp[index] && index > 0) { inputRefs.current[index - 1]?.focus(); } }; // Handle paste const handlePaste = (e: React.ClipboardEvent) => { e.preventDefault(); const pastedText = e.clipboardData.getData("text"); const digits = pastedText.replace(/\D/g, "").slice(0, 4); if (digits.length === 4) { const newOtp = digits.split(""); setOtp(newOtp); inputRefs.current[3]?.focus(); } }; // Format time const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins}:${secs.toString().padStart(2, "0")}`; }; // Format phone display const formatPhone = (phoneNumber: string) => { if (phoneNumber.length <= 2) return phoneNumber; if (phoneNumber.length <= 5) return `${phoneNumber.substring(0, 2)} ${phoneNumber.substring(2)}`; if (phoneNumber.length <= 9) return `${phoneNumber.substring(0, 2)} ${phoneNumber.substring( 2, 5 )} ${phoneNumber.substring(5)}`; return `${phoneNumber.substring(0, 2)} ${phoneNumber.substring( 2, 5 )} ${phoneNumber.substring(5, 9)} ${phoneNumber.substring(9)}`; }; return (
{/* Progress Indicator */} {/* Main Card */}

Verifikasi WhatsApp

Masukkan kode OTP 4 digit yang dikirim ke

{formatPhone(phone)}

{/* Success Alert */} {actionData?.success && actionData?.message && ( {actionData.message} )} {/* Error Alert */} {actionData?.errors?.otp && ( {actionData.errors.otp} )} {/* OTP Input Form */}
{/* OTP Input Fields */}
{otp.map((digit, index) => ( (inputRefs.current[index] = el)} type="text" maxLength={1} value={digit} onChange={(e) => handleOtpChange(index, e.target.value)} onKeyDown={(e) => handleKeyDown(index, e)} onPaste={handlePaste} className={`w-14 h-14 text-center text-xl font-bold ${ actionData?.errors?.otp ? "border-red-500" : "" }`} autoFocus={index === 0} /> ))}

Tempel kode OTP atau ketik manual

{/* Timer */}
{timeLeft > 0 ? (
Kode kedaluwarsa dalam {formatTime(timeLeft)}
) : (
Kode OTP telah kedaluwarsa
)}
{/* Verify Button */}
{/* Resend OTP */}

Tidak menerima kode?

{/* Back Link */}
Ganti nomor WhatsApp
{/* Demo Info */}

Demo OTP:

Gunakan kode:{" "} 1234

Atau tunggu countdown habis untuk test resend

); }