|
|
@@ -1,4 +1,4 @@ |
|
|
|
import { useState } from "react"; |
|
|
|
import { createRef, useEffect, useRef, useState } from "react"; |
|
|
|
import styles from "./Question.module.scss"; |
|
|
|
|
|
|
|
|
|
|
@@ -8,8 +8,31 @@ interface OwnProp { |
|
|
|
timeLimit: number; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let timeout: NodeJS.Timeout; |
|
|
|
|
|
|
|
const Question: React.FC<OwnProp> = (props) => { |
|
|
|
|
|
|
|
const COUNTDOWN_AMOUNT_TOTAL = props.timeLimit * 60; |
|
|
|
|
|
|
|
const [seconds, setSeconds] = useState<number>(COUNTDOWN_AMOUNT_TOTAL); |
|
|
|
|
|
|
|
const displaySeconds = seconds % 60; |
|
|
|
const displayMinutes = Math.floor(seconds / 60); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
if (seconds > 0) { |
|
|
|
timeout = setTimeout(() => { |
|
|
|
setSeconds((state) => state - 1); |
|
|
|
}, 1000); |
|
|
|
} else { |
|
|
|
clearTimeout(timeout); |
|
|
|
} |
|
|
|
|
|
|
|
return () => clearTimeout(timeout);; |
|
|
|
}, [seconds]); |
|
|
|
|
|
|
|
return ( |
|
|
|
<section> |
|
|
|
<div className={styles.questionHolder}> |
|
|
@@ -22,7 +45,9 @@ const Question: React.FC<OwnProp> = (props) => { |
|
|
|
|
|
|
|
<div className={styles.quizTimer}> |
|
|
|
<div className={styles.border}> |
|
|
|
<div className={styles.time}>01:00</div> |
|
|
|
<div className={styles.time}> |
|
|
|
{`${displayMinutes < 10 ? "0" : ""}${displayMinutes}:${displaySeconds < 10 ? "0" : ""}${displaySeconds}`} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|