From 5a4de49ea8a3f06568fed7863b5085750b94a149 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Mon, 12 Aug 2024 11:07:16 +0900
Subject: fix(frontend): fix an issue where game does not finish

---
 frontend/app/components/GolfPlayApp.client.tsx | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

(limited to 'frontend/app/components/GolfPlayApp.client.tsx')

diff --git a/frontend/app/components/GolfPlayApp.client.tsx b/frontend/app/components/GolfPlayApp.client.tsx
index d527e07..82c0928 100644
--- a/frontend/app/components/GolfPlayApp.client.tsx
+++ b/frontend/app/components/GolfPlayApp.client.tsx
@@ -47,30 +47,27 @@ export default function GolfPlayApp({
 			(gameState === "starting" || gameState === "gaming") &&
 			startedAt !== null
 		) {
-			const timer1 = setInterval(() => {
+			const timer = setInterval(() => {
 				setLeftTimeSeconds((prev) => {
 					if (prev === null) {
 						return null;
 					}
 					if (prev <= 1) {
-						setGameState("gaming");
+						const nowSec = Math.floor(Date.now() / 1000);
+						const finishedAt = startedAt + game.duration_seconds;
+						if (nowSec >= finishedAt) {
+							clearInterval(timer);
+							setGameState("finished");
+						} else {
+							setGameState("gaming");
+						}
 					}
 					return prev - 1;
 				});
 			}, 1000);
 
-			const timer2 = setInterval(() => {
-				const nowSec = Math.floor(Date.now() / 1000);
-				const finishedAt = startedAt + game.duration_seconds;
-				if (nowSec >= finishedAt) {
-					clearInterval(timer2);
-					setGameState("finished");
-				}
-			}, 1000);
-
 			return () => {
-				clearInterval(timer1);
-				clearInterval(timer2);
+				clearInterval(timer);
 			};
 		}
 	}, [gameState, startedAt, game.duration_seconds]);
-- 
cgit v1.2.3-70-g09d2

