Skip to content

Commit

Permalink
fix: 인증 여러 번 눌렀을 때 타이머 빠르게 동작하는 현상 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
chae-won-shin committed Nov 2, 2024
1 parent 3901820 commit 833b7e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
5 changes: 3 additions & 2 deletions get-p-client/src/common/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isValueAssigned } from "./validation";

export const formatTime = (time: number): string => {
const minutes = Math.floor(time / 60);
const seconds = time % 60;
const total = Math.floor(time / 1000);
const minutes = Math.floor(total / 60);
const seconds = total % 60;
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
};

Expand Down
47 changes: 31 additions & 16 deletions get-p-client/src/components/auth/SignUpInputSection/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, useCallback, useRef, useState } from "react";
import { ChangeEvent, useCallback, useRef, useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { toast } from "react-toastify";

Expand Down Expand Up @@ -37,10 +37,12 @@ export default function SignUpInputSection() {
const infoAgreementRef = useRef<HTMLInputElement>(null);
const verificationRef = useRef<HTMLInputElement>(null);

const MINUTES_IN_MS = 5 * 60 * 1000;
const INTERVAL = 1000;
const [isEmailVerificationFieldVisible, setIsEmailVerificationFieldVisible] = useState<boolean>(false);
const [isPasswordCorrect, setIsPasswordCorrect] = useState<boolean>(false);
const [timer, setTimer] = useState<number>(300);
const [isTimerRunning, setIsTimerRunning] = useState<boolean>(false);
const [timeLeft, setTimeLeft] = useState<number>(MINUTES_IN_MS);
const [runningTimer, setRunningTimer] = useState<boolean>(false);

const {
value: email,
Expand All @@ -54,18 +56,31 @@ export default function SignUpInputSection() {
onChange: onPasswordChange,
} = useInputValidation(REGEXP_PASSWORD);

useEffect(() => {
let intervalId: NodeJS.Timeout;

if (runningTimer && timeLeft > 0) {
intervalId = setInterval(() => {
setTimeLeft((prevTime) => {
if (prevTime <= 1000) {
setRunningTimer(false);
return 0;
}
return prevTime - INTERVAL;
});
}, INTERVAL);
}

return () => {
if (intervalId) {
clearInterval(intervalId);
}
};
}, [runningTimer]);

Check warning on line 79 in get-p-client/src/components/auth/SignUpInputSection/index.tsx

View workflow job for this annotation

GitHub Actions / compile_and_lint

React Hook useEffect has a missing dependency: 'timeLeft'. Either include it or remove the dependency array

const startTimer = () => {
setTimer(300);
setIsTimerRunning(true);
const intervalId = setInterval(() => {
setTimer((prevTimer) => {
if (prevTimer === 0) {
clearInterval(intervalId);
return 0;
}
return prevTimer - 1;
});
}, 1000);
setTimeLeft(MINUTES_IN_MS);
setRunningTimer(true);
};

const handleEmailVerificationBtnClick = useCallback(async () => {
Expand Down Expand Up @@ -174,9 +189,9 @@ export default function SignUpInputSection() {
placeholder="인증번호를 입력해주세요"
>
<Button variant="side" width="50px" height="38px">
{isTimerRunning && (
{runningTimer !== null && (
<Text weight="bold" color="point">
{formatTime(timer)}
{formatTime(timeLeft)}
</Text>
)}
</Button>
Expand Down

0 comments on commit 833b7e5

Please sign in to comment.