import { Dialog, DialogContent, DialogOverlay } from "./ui/dialog"; import { Button } from "./ui/button"; import { ArrowLeft, Loader2, Phone, RotateCcw, LogIn } from "lucide-react"; import { useState, useEffect } from "react"; import { useSubmit, useRevalidator } from "@remix-run/react"; import { InputOTP, InputOTPGroup, InputOTPSlot, } from "~/components/ui/input-otp"; import AppInput from "~/components/AppInput"; import { useToast } from "~/hooks/use-toast"; import { useServiceSendOtp, useServiceVerifyOtp, } from "~/utils/RequestHandler"; interface LoginRequiredDialogProps { isOpen: boolean; onOpenChange: (isOpen: boolean) => void; loginTitle: string; } // Helper function to convert Persian numbers to English const convertPersianToEnglish = (str: string): string => { const persianNumbers = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"]; const englishNumbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; let result = str; for (let i = 0; i < 10; i++) { result = result.replace( new RegExp(persianNumbers[i], "g"), englishNumbers[i] ); } return result; }; const ResendTimer = ({ initialTime = 120, onTimerEnd, }: { initialTime?: number; onTimerEnd?: () => void; }) => { const [timeLeft, setTimeLeft] = useState(initialTime); useEffect(() => { if (timeLeft <= 0) { if (onTimerEnd) onTimerEnd(); return; } const timer = setInterval(() => { setTimeLeft((prev) => prev - 1); }, 1000); return () => clearInterval(timer); }, [timeLeft, onTimerEnd]); const formatTime = (seconds: number) => { const minutes = Math.floor(seconds / 60); const remainingSeconds = seconds % 60; return `${minutes}:${remainingSeconds < 10 ? `0${remainingSeconds}` : remainingSeconds}`; }; return (
{formatTime(timeLeft)} تا ارسال مجدد کد
); }; export function LoginRequiredDialog({ isOpen, onOpenChange, loginTitle, }: LoginRequiredDialogProps) { const { toast } = useToast(); const submit = useSubmit(); const revalidator = useRevalidator(); const [phoneNumber, setPhoneNumber] = useState(""); const [otpValue, setOtpValue] = useState(""); const [enableVerifySection, setEnableVerifySection] = useState(false); const [canResend, setCanResend] = useState(false); const sendOtpMutation = useServiceSendOtp( phoneNumber, setEnableVerifySection ); const verifyOtpMutation = useServiceVerifyOtp(phoneNumber, otpValue, toast); // Handle successful verification useEffect(() => { if ( verifyOtpMutation.data && verifyOtpMutation.data.accessToken && verifyOtpMutation.data.refreshToken ) { const currentUrl = window.location.pathname; submit( { phoneNumber: phoneNumber, access_token: JSON.stringify(verifyOtpMutation.data.accessToken), refresh_token: JSON.stringify(verifyOtpMutation.data.refreshToken), redirectTo: currentUrl, }, { action: "/login", method: "post", encType: "application/x-www-form-urlencoded", } ); // Close modal and revalidate after successful login onOpenChange(false); revalidator.revalidate(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [verifyOtpMutation.data]); useEffect(() => { if (enableVerifySection) setCanResend(false); }, [enableVerifySection]); const handleTimerEnd = () => { setCanResend(true); }; // Reset state when modal closes useEffect(() => { if (!isOpen) { setPhoneNumber(""); setOtpValue(""); setEnableVerifySection(false); setCanResend(false); } }, [isOpen]); return ( ); }