aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorasketonim <anton.dubik33@gmail.com>2020-01-03 16:32:36 +0300
committerasketonim <anton.dubik33@gmail.com>2020-01-03 16:32:36 +0300
commit33dc86d661f4cac10a87804ba1bacf92e36c16ce (patch)
tree14d1cdd0d8cb7204d05bf964578ab4eaf2ff12a2 /src
parent2b917f53e806191340a7a26bef3468320dd29917 (diff)
downloadchrono-cube-ui-33dc86d661f4cac10a87804ba1bacf92e36c16ce.tar.gz
Implement full countdown functionality
Diffstat (limited to 'src')
-rw-r--r--src/components/Timer/Timer.js54
1 files changed, 51 insertions, 3 deletions
diff --git a/src/components/Timer/Timer.js b/src/components/Timer/Timer.js
index 367cce5..fea0ed2 100644
--- a/src/components/Timer/Timer.js
+++ b/src/components/Timer/Timer.js
@@ -1,17 +1,62 @@
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
-import {post} from '../../requests';
+import { post } from '../../requests';
const Timer = () => {
- const SPACE = 32
- const [time, setTime] = useState("00:00:00")
+ const SPACE = 32;
+ const countdownInMills = 15000;
+ const [time, setTime] = useState("00:15:00");
const [running, setRunning] = useState(false);
const [timer, setTimer] = useState(0);
+ const [countdown, setCountdown] = useState(0);
+ const [countdownRunning, setCountdownRunning] = useState(false);
let startingTime;
+
+ const handleKeyDown = event => {
+ if (event.keyCode === SPACE && !running ) {
+
+ let timeGap = Date.now() - countdown;
+ if (!countdownRunning) {
+ setCountdown(Date.now());
+ setCountdownRunning(true);
+ timeGap = 0;
+ }
+
+ if (timeGap >= countdownInMills) {
+ setRunning(true);
+ setTime('00:00:00');
+ return;
+ }
+
+ setTime(() => {
+ const timeLeft = Math.floor((countdownInMills - timeGap) / 10);
+ let resultTime = "";
+
+ const minute = Math.floor(timeLeft / 6000);
+ if (minute < 10) resultTime += '0';
+ resultTime += minute + ':';
+
+ let second = Math.floor(timeLeft / 100);
+ if (second < 10) resultTime += '0';
+ if (second > 59) second %= 60;
+ resultTime += second + ':';
+
+ const mill = timeLeft % 100;
+ if (mill < 10) resultTime += '0';
+ resultTime += mill;
+
+ return resultTime;
+ })
+
+
+ }
+ }
+
const handleKeyUp = event => {
+ setCountdownRunning(false);
if (event.keyCode === SPACE){
if (!running) {
startingTime = Date.now();
@@ -48,9 +93,12 @@ const Timer = () => {
useEffect(() => {
window.addEventListener("keyup", handleKeyUp);
+ window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keyup", handleKeyUp);
+ window.removeEventListener("keydown", handleKeyDown);
+
};
})