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"; import { validateOtp, generateDeviceId } from "~/utils/auth-utils"; import pengelolaAuthService from "~/services/auth/pengelola.service"; import { createUserSession } from "~/sessions.server"; // Progress Indicator Component 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") { try { // Call API untuk resend OTP const response = await pengelolaAuthService.requestOtpRegister({ phone, role_name: "pengelola" }); if (response.meta.status === 200) { return json({ success: true, message: "Kode OTP baru telah dikirim ke WhatsApp Anda", otpSentAt: new Date().toISOString() }); } else { return json( { errors: { general: response.meta.message || "Gagal mengirim ulang OTP" } }, { status: 400 } ); } } catch (error: any) { console.error("Resend OTP error:", error); return json( { errors: { general: "Gagal mengirim ulang OTP. Silakan coba lagi." } }, { status: 500 } ); } } if (actionType === "verify") { // Validation const errors: { otp?: string; general?: string } = {}; if (!otp) { errors.otp = "Kode OTP wajib diisi"; } else if (!validateOtp(otp)) { errors.otp = "Kode OTP harus 4 digit angka"; } if (Object.keys(errors).length > 0) { return json({ errors }, { status: 400 }); } try { // Generate device ID const deviceId = generateDeviceId("pengelola_"); // Call API untuk verifikasi OTP const response = await pengelolaAuthService.verifyOtpRegister({ phone, otp, device_id: deviceId, role_name: "pengelola" }); if (response.meta.status === 200 && response.data) { // OTP valid, create session return createUserSession({ request, sessionData: { accessToken: response.data.access_token, refreshToken: response.data.refresh_token, sessionId: response.data.session_id, role: "pengelola", deviceId: deviceId, phone: phone, tokenType: response.data.token_type, registrationStatus: response.data.registration_status, nextStep: response.data.next_step }, redirectTo: "/authpengelola/completingcompanyprofile" }); } else { return json( { errors: { otp: response.meta.message || "Kode OTP tidak valid atau sudah kedaluwarsa" } }, { status: 401 } ); } } catch (error: any) { console.error("Verify OTP error:", error); // Handle specific API errors if (error.response?.data?.meta?.message) { return json( { errors: { otp: error.response.data.meta.message } }, { status: error.response.status || 500 } ); } return json( { errors: { general: "Gagal memverifikasi OTP. Silakan coba lagi." } }, { status: 500 } ); } } 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?.general) && ( {actionData.errors.otp || actionData.errors.general} )} {/* 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
{/* Help Card */}

Mengalami kesulitan?

Hubungi Customer Support
); }