Skip to content

Commit

Permalink
Merge pull request #85 from ConducereIT/complete-platforms
Browse files Browse the repository at this point in the history
add timer
  • Loading branch information
cristim67 authored May 11, 2024
2 parents a78d290 + 21ed6c8 commit 5554018
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client/src/routes/AllRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Logout from "../views/Logout.view.tsx";
import Checkin from "../views/Checkin.view.tsx";
import InscriereParticipantiView from "../views/InscriereParticipanti.view.tsx";
import Cronometrare from "../views/Cronometrare.view.tsx";
import Timer from "../views/Cronometru.view.tsx";

export const allRoutes = createBrowserRouter([
{
Expand Down Expand Up @@ -73,4 +74,8 @@ export const allRoutes = createBrowserRouter([
path:"cronometrare",
element: <Cronometrare/>
},
{
path:"cronometru",
element:<Timer/>
}
]);
63 changes: 63 additions & 0 deletions client/src/views/Cronometru.view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useState, useEffect } from 'react';

const Timer: React.FC = () => {
const [time, setTime] = useState<number>(0);
const [isActive, setIsActive] = useState<boolean>(false);
const [intervalId, setIntervalId] = useState<number | null>(null);

const toggleTimer = (): void => {
setIsActive(!isActive);
};

// Start the timer
useEffect(() => {
if (isActive) {
const id = window.setInterval(() => {
setTime(time => time + 1);
}, 1000); // Update time every second
setIntervalId(id);
} else {
if (intervalId !== null) {
clearInterval(intervalId);
}
setIntervalId(null);
}
return () => {
if (intervalId !== null) {
clearInterval(intervalId);
}
};
}, [isActive, intervalId]);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent): void => {
if (event.code === 'Space') {
toggleTimer();
}
};

window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [isActive]);

useEffect(() => {
const resetTime = () => setTime(0);
window.addEventListener('load', resetTime);
return () => window.removeEventListener('load', resetTime);
}, []);

useEffect(() => {
document.body.style.backgroundColor = "#081043";
}, []);

return (
<div className="flex flex-col items-center justify-center mt-[-5rem] text-white h-screen">
<img src="https://i.ibb.co/H4txyBj/logo-apv-XV.png" alt="Logo Aleargă pentru Viață" className="max-w-90 scale-[230%] h-auto" />
<div className="text-9xl font-bold">
{new Date(time * 1000).toISOString().substr(11, 8)}
</div>
</div>
);
};

export default Timer;

0 comments on commit 5554018

Please sign in to comment.