From 19f4db560ee29fc33ed47f0995f81150035218a1 Mon Sep 17 00:00:00 2001 From: aleckslu Date: Sat, 30 Mar 2024 05:40:22 -0700 Subject: [PATCH] updated timer to include hours --- src/app/_components/Dashboard.tsx | 227 ++++++++++++++++-------------- src/app/page.tsx | 2 +- 2 files changed, 126 insertions(+), 103 deletions(-) diff --git a/src/app/_components/Dashboard.tsx b/src/app/_components/Dashboard.tsx index c2e343f..a9ebca9 100644 --- a/src/app/_components/Dashboard.tsx +++ b/src/app/_components/Dashboard.tsx @@ -1,115 +1,138 @@ -import { CSSProperties, useEffect, useMemo, useRef, useState } from "react"; +import { + CSSProperties, + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from "react"; type DashBoardProps = { - startMin: number; - startSec: number; + startHr: number; + startMin: number; + startSec: number; }; -export default function Dashboard({ startMin, startSec }: DashBoardProps) { - const [activeTimer, setActiveTimer] = useState(false); - const [minutes, setMinutes] = useState(startMin); - const [seconds, setSeconds] = useState(startSec); - const [percentage, setPercentage] = useState(100); +export default function Dashboard({ + startHr, + startMin, + startSec, +}: DashBoardProps) { + const [activeTimer, setActiveTimer] = useState(false); + const [hours, setHours] = useState(startHr); + const [minutes, setMinutes] = useState(startMin); + const [seconds, setSeconds] = useState(startSec); + const [percentage, setPercentage] = useState(100); - // turn - const timeToSecs = (min:number, sec:number):number => min * 60 + sec; - - const totalStartSec = useMemo( - () => timeToSecs(startMin, startSec), - [startMin, startSec] - ); + const timeToSecs = (hr: number = 0, min: number, sec: number): number => + hr * 3600 + min * 60 + sec; - const timerRef = useRef(); + const totalStartSec = useMemo( + () => timeToSecs(startHr, startMin, startSec), + [startHr, startMin, startSec] + ); - useEffect(() => { - if (activeTimer) { - timerRef.current = setInterval(() => { - if (seconds > 0) { - setSeconds(seconds - 1); - } - if (seconds === 0) { - if (minutes === 0) { - clearInterval(timerRef.current); - } else { - setMinutes(minutes - 1); - setSeconds(59); - } - } - }, 1000); - } - return () => clearInterval(timerRef.current); - }, [activeTimer, minutes, seconds]); + const timerRef = useRef(); - useEffect(() => { - setPercentage(Math.abs((timeToSecs(minutes, seconds) / totalStartSec) * 100)); - console.log((timeToSecs(minutes, seconds) / totalStartSec) * 100); - }, [minutes, seconds, totalStartSec]); + useEffect(() => { + if (activeTimer) { + timerRef.current = setInterval(() => { + if (seconds === 0) { + if (minutes === 0) { + if (hours === 0) { + setActiveTimer(false); + } else { + setHours(hours - 1); + setMinutes(59); + setSeconds(59); + } + } else { + setMinutes(minutes - 1); + setSeconds(59); + } + } else { + setSeconds(seconds - 1); + } + }, 1000); + } else { + clearInterval(timerRef.current); + } + return () => clearInterval(timerRef.current); + }, [activeTimer, hours, minutes, seconds]); - const restart = () => { - // Clears the interval to stop the timer from updating - setActiveTimer(false); - setMinutes(startMin); - setSeconds(startSec); - }; + useEffect(() => { + setPercentage((timeToSecs(hours, minutes, seconds) / totalStartSec) * 100); + }, [totalStartSec, hours, minutes, seconds]); - const pause = () => { - // Clears the interval to stop the timer from updating - if (activeTimer) { - setActiveTimer(false); - clearInterval(timerRef.current); - } - }; + const restart = () => { + // Clears the interval to stop the timer from updating + setActiveTimer(false); + setHours(startHr); + setMinutes(startMin); + setSeconds(startSec); + }; - return ( -
-
-
-
- - : - - min -
+ const pause = () => { + // Clears the interval to stop the timer from updating + setActiveTimer(false); + }; + + return ( +
+
+
- - - - sec -
-
-
-
- - - -
-
- ); + + : + + hours +
+
+ + : + + min +
+
+ + + + sec +
+
+ +
+ + + +
+
+ ); } diff --git a/src/app/page.tsx b/src/app/page.tsx index a7cf7fc..3317805 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -2,5 +2,5 @@ import Dashboard from "./_components/Dashboard"; export default function Home() { - return ; + return ; }