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
} from "lucide-react";
import { validatePhoneNumber } from "~/utils/auth-utils";
import pengelolaAuthService from "~/services/auth/pengelola.service";
// 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 if (!validatePhoneNumber(phone)) {
errors.phone = "Format: 628xxxxxxxxx (9-14 digit setelah 62)";
}
if (Object.keys(errors).length > 0) {
return json({ errors }, { status: 400 });
}
try {
// Call API untuk request OTP register
const response = await pengelolaAuthService.requestOtpRegister({
phone,
role_name: "pengelola"
});
if (response.meta.status === 200) {
// OTP berhasil dikirim, redirect ke halaman verifikasi
return redirect(
`/authpengelola/verifyotptoregister?phone=${encodeURIComponent(phone)}`
);
} else {
return json(
{
errors: { general: response.meta.message || "Gagal mengirim OTP" }
},
{ status: 400 }
);
}
} catch (error: any) {
console.error("Request OTP error:", error);
// Handle specific API errors
if (error.response?.data?.meta?.message) {
return json(
{
errors: { general: error.response.data.meta.message }
},
{ status: error.response.status || 500 }
);
}
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 */}
);
}