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 {
Phone,
ArrowLeft,
ArrowRight,
AlertCircle,
Loader2,
MessageSquare,
CheckCircle,
LogIn,
Shield
} from "lucide-react";
// Progress Indicator Component untuk Login (3 steps)
const LoginProgressIndicator = ({ currentStep = 1, totalSteps = 3 }) => {
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 RequestOTPLoginActionData {
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 cek apakah nomor terdaftar
const registeredPhones = ["6281234567890", "6281234567891", "6281234567892"];
if (!registeredPhones.includes(phone)) {
return json(
{
errors: {
phone: "Nomor tidak terdaftar. Silakan daftar terlebih dahulu."
}
},
{ status: 404 }
);
}
// Simulasi kirim OTP - dalam implementasi nyata, integrate dengan WhatsApp Business API
try {
console.log("Sending login 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/verifyotptologin?phone=${encodeURIComponent(phone)}`
);
} catch (error) {
return json(
{
errors: { general: "Gagal mengirim OTP. Silakan coba lagi." }
},
{ status: 500 }
);
}
};
export default function RequestOTPForLogin() {
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 */}
Masuk ke Akun Anda
Masukkan nomor WhatsApp yang terdaftar
{/* Error Alert */}
{actionData?.errors?.general && (
{actionData.errors.general}
)}
{/* Form */}
{/* Register Link */}
{/* Back Link */}
{/* Help Card */}
);
}