import { useRef, useState } from "react"; import { Button } from "@mui/material"; import OTPForm from "./element/OTPForm"; import EditLogin from "../icons/EditLogin"; import RetryLogin from "../icons/RetryLogin"; import ArrowRightS from "../icons/ArrowRightS"; import { request } from "@/services/response"; import { changeNumToDefault } from "@/helper"; import axios from "axios"; const time_resend_code = 120; function SetCodePage({ setIsSendMsg, link, num, setStep }) { const retryIcon = useRef(); const [timer, setTimer] = useState(0); const [loading, setLoading] = useState(false); const [code, setCode] = useState(["", "", "", ""]); const [isError, setIsError] = useState(false); const inputRefs = Array.from({ length: 5 }, () => useRef()); const handleChange = (index, val) => { const newCode = [...code]; if (val === "backspace") { newCode[index] = ""; setCode(newCode); if (index > 0) { inputRefs[index - 1].current?.focus(); } return; } newCode[index] = val; if (newCode.join("").length === 4 && isError) { setIsError(false); } // if (newCode.join("").length === 4) { // handleReq(); // } setCode(newCode); if (val && index < inputRefs.length - 1) { inputRefs[index + 1].current?.focus(); } }; const handleTimer = () => { setTimer("loading"); setTimeout(() => { setTimer(time_resend_code); let timePresent = time_resend_code; const timerInterval = setInterval(() => { timePresent--; setTimer(timePresent); !timePresent && clearInterval(timerInterval); }, 1000); }, 1000); }; const handleReq = async () => { const formData = new URLSearchParams(); formData.append("grant_type", "mobile"); formData.append("client_id", process.env.NEXT_PUBLIC_CLIENT_ID); formData.append("client_secret", process.env.NEXT_PUBLIC_CLIENT_SECRET); formData.append("mobile_number", changeNumToDefault(num)); formData.append("code", code.join("")); try { const res = await axios.post( "https://back-dev.clinic-pro.ir/oauth/token", formData, { withCredentials: true, headers: { "Content-Type": "application/x-www-form-urlencoded", }, } ); console.log(res); } catch (err) { console.error("Request failed:", err); } // setLoading(true); // request // .getToken( // "mobile", // process.env.NEXT_PUBLIC_CLIENT_ID, // process.env.NEXT_PUBLIC_CLIENT_SECRET, // changeNumToDefault(num), // code.join("") // ) // .then((response) => { // setLoading(false); // }) // .catch((err) => { // setLoading(false); // }); }; return (
کد تایید را وارد نمایید
کد تایید برای شماره 09013261200 ارسال شد.
!کد را وارد کنید