import RetryLogin from "@/components/icons/RetryLogin"; import { changeNumToDefault } from "@/helper"; import { request } from "@/services/response"; import { Button } from "@mui/material"; import { useEffect, useRef } from "react"; const time_resend_code = 120; function Recode({ retryIcon, timer, setUuid, num, setTimer }) { const timerIntervalRef = useRef(null); const animIntervalRef = useRef(null); useEffect(() => { return () => { if (timerIntervalRef.current) clearInterval(timerIntervalRef.current); if (animIntervalRef.current) clearInterval(animIntervalRef.current); }; }, []); const handleTimer = () => { setTimer("loading"); if (timerIntervalRef.current) clearInterval(timerIntervalRef.current); setTimeout(() => { setTimer(time_resend_code); let timePresent = time_resend_code; timerIntervalRef.current = setInterval(() => { timePresent--; setTimer(timePresent); if (!timePresent) { clearInterval(timerIntervalRef.current); timerIntervalRef.current = null; } }, 1000); }, 1000); }; const rotateIcon = () => { if (!retryIcon.current) return; retryIcon.current.classList.add("retry-icon"); setTimeout(() => { retryIcon.current?.classList.remove("retry-icon"); }, 1000); }; const handleReq = () => { rotateIcon(); if (animIntervalRef.current) clearInterval(animIntervalRef.current); animIntervalRef.current = setInterval(() => { rotateIcon(); }, 1200); request .sendCode(changeNumToDefault(num), "") .then((response) => { clearInterval(animIntervalRef.current); animIntervalRef.current = null; if (response.uuid) { setUuid(response.uuid); handleTimer(); } }) .catch(() => { clearInterval(animIntervalRef.current); animIntervalRef.current = null; }); }; return ( ); } export default Recode;