import { json, redirect, type ActionFunctionArgs } from "@remix-run/node";
import { Form, useActionData, useNavigation, Link } from "@remix-run/react";
import { useState } from "react";
import { Card, CardContent, CardHeader } from "~/components/ui/card";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { Label } from "~/components/ui/label";
import { Alert, AlertDescription } from "~/components/ui/alert";
import { Progress } from "~/components/ui/progress";
import {
Phone,
ArrowLeft,
ArrowRight,
AlertCircle,
Loader2,
MessageSquare,
CheckCircle
} from "lucide-react";
// Progress Indicator Component
const ProgressIndicator = ({ currentStep = 1, 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 && (
)}
);
})}
);
};
// Interface untuk action response
interface RequestOTPActionData {
errors?: {
phone?: string;
general?: string;
};
success?: boolean;
}
export const action = async ({ request }: ActionFunctionArgs): Promise => {
const formData = await request.formData();
const phone = formData.get("phone") as string;
// Validation
const errors: { phone?: string; general?: string } = {};
if (!phone) {
errors.phone = "Nomor WhatsApp wajib diisi";
} else {
// Validasi format nomor HP Indonesia
const phoneRegex = /^62[0-9]{9,14}$/;
if (!phoneRegex.test(phone)) {
errors.phone = "Format: 628xxxxxxxxx (9-14 digit setelah 62)";
}
}
if (Object.keys(errors).length > 0) {
return json({ errors }, { status: 400 });
}
// Simulasi kirim OTP - dalam implementasi nyata, integrate dengan WhatsApp Business API
try {
console.log("Sending OTP to WhatsApp:", phone);
// Simulasi delay API call
await new Promise(resolve => setTimeout(resolve, 1000));
// Redirect ke step berikutnya dengan nomor HP
return redirect(`/authpengelola/verifyotptoregister?phone=${encodeURIComponent(phone)}`);
} catch (error) {
return json({
errors: { general: "Gagal mengirim OTP. Silakan coba lagi." }
}, { status: 500 });
}
};
export default function RequestOTPForRegister() {
const actionData = useActionData();
const navigation = useNavigation();
const [phone, setPhone] = useState("");
const isSubmitting = navigation.state === "submitting";
// Format input nomor HP
const handlePhoneChange = (value: string) => {
// Remove non-numeric characters
let cleaned = value.replace(/\D/g, '');
// Ensure starts with 62
if (cleaned.length > 0 && !cleaned.startsWith('62')) {
if (cleaned.startsWith('0')) {
cleaned = '62' + cleaned.substring(1);
} else if (cleaned.startsWith('8')) {
cleaned = '62' + cleaned;
} else {
cleaned = '62' + cleaned;
}
}
// Limit length
if (cleaned.length > 16) {
cleaned = cleaned.substring(0, 16);
}
setPhone(cleaned);
};
// Format display nomor HP
const formatPhoneDisplay = (value: string) => {
if (value.length <= 2) return value;
if (value.length <= 5) return `${value.substring(0, 2)} ${value.substring(2)}`;
if (value.length <= 9) return `${value.substring(0, 2)} ${value.substring(2, 5)} ${value.substring(5)}`;
return `${value.substring(0, 2)} ${value.substring(2, 5)} ${value.substring(5, 9)} ${value.substring(9)}`;
};
return (
{/* Progress Indicator */}
{/* Main Card */}
Selamat Datang! Mari mulai...
Masukkan nomor WhatsApp untuk verifikasi akun
{/* Error Alert */}
{actionData?.errors?.general && (
{actionData.errors.general}
)}
{/* Form */}
{/* Back Link */}
{/* Help Card */}
);
}