import React, { useState } from "react"; import { motion, Variants } from "framer-motion"; import { useNavigate } from "@tanstack/react-router"; import { useApiMutation } from "../../utils/useApiRequest"; import { useUserStore, useUserProfileStore, } from "../../context/zustand-store/userStore"; import { useToast } from "../../hooks/useToast"; import Textfield from "../../components/Textfeild/Textfeild"; import Button from "../../components/Button/Button"; import Typography from "../../components/Typography/Typography"; import Captcha from "../../components/Captcha/Captcha"; import logo from "../../assets/images/logo.png"; const containerVariants: Variants = { hidden: {}, show: { transition: { staggerChildren: 0.1, }, }, }; const fadeInUp: Variants = { hidden: { opacity: 0, y: 10 }, show: { opacity: 1, y: 0, transition: { duration: 0.4, ease: "easeOut" } }, }; const Login: React.FC = () => { const navigate = useNavigate(); const { setUser } = useUserStore(); const { setUserProfile } = useUserProfileStore(); const showToast = useToast(); const [phoneNumber, setPhoneNumber] = useState(""); const [password, setPassword] = useState(""); const [isValidCaptcha, setIsValidCaptcha] = useState(false); const [captchaImage, setCaptchaImage] = useState(""); const [captchaKey, setCaptchaKey] = useState(""); const [captchaCode, setCaptchaCode] = useState(""); const mutationCaptcha = useApiMutation({ api: "auth/", method: "post", disableBackdrop: true, }); const mutationLogin = useApiMutation({ api: "login", method: "post", disableBackdrop: false, }); const handleGetCaptcha = async () => { try { const data = await mutationCaptcha.mutateAsync({ mobile: phoneNumber || "", state: "login", }); if (data?.captcha_image) { setCaptchaImage(data.captcha_image); setCaptchaKey(data.captcha_key || ""); } } catch (error) { console.error("Error getting captcha:", error); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!phoneNumber || !password) { showToast("لطفا شماره همراه و کلمه عبور را وارد کنید", "error"); return; } if (captchaImage && captchaKey && !captchaCode) { showToast("لطفا کد امنیتی را وارد کنید", "error"); return; } if (!captchaImage && !isValidCaptcha) { showToast("لطفا کد امنیتی را به درستی وارد کنید", "error"); return; } try { const loginData: any = { mobile: phoneNumber, password: password, }; if (captchaImage && captchaKey && captchaCode) { loginData.captcha_code = captchaCode; loginData.captcha_key = `rest_captcha_${captchaKey}.0`; } const response = await mutationLogin.mutateAsync(loginData); if (response?.token || response?.access) { const token = response.token || response.access; setUser({ auth: token }); if (response?.user) { setUserProfile(response.user); } navigate({ to: "/" }); } else { showToast("نام کاربری یا کلمه عبور صحیح نیست!", "error"); handleGetCaptcha(); } } catch (error: any) { console.error("Login error:", error); showToast("نام کاربری یا کلمه عبور صحیح نیست!", "error"); handleGetCaptcha(); } }; return ( سامانه بازرسی رصدبان setPhoneNumber(e.target.value)} /> setPassword(e.target.value)} />
{captchaImage ? (
{ setCaptchaCode(e.target.value); setIsValidCaptcha(e.target.value.length > 0); }} isNumber />
captcha
) : ( setIsValidCaptcha(status)} captchaImage={captchaImage} captchaKey={captchaKey} onRefresh={handleGetCaptcha} /> )}
); }; export default Login;