|
@@ -1,8 +1,7 @@
|
|
-import React, { useEffect } from "react";
|
|
|
|
|
|
+import React, { useEffect, useState } from "react";
|
|
import styled from "styled-components";
|
|
import styled from "styled-components";
|
|
import ms from "pretty-ms";
|
|
import ms from "pretty-ms";
|
|
-import useCountdown from "../../../hooks/useCountdown";
|
|
|
|
-import { saveTimerToLocalStorage } from "../../../domain/localStorageMethods";
|
|
|
|
|
|
+import { useRoundSeconds, dispatch } from "../../../domain/gameStore";
|
|
|
|
|
|
const TimerSpan = styled.span`
|
|
const TimerSpan = styled.span`
|
|
padding: 1px;
|
|
padding: 1px;
|
|
@@ -13,17 +12,29 @@ const TimedOut = styled(TimerSpan)`
|
|
color: red;
|
|
color: red;
|
|
`
|
|
`
|
|
|
|
|
|
-export default ({ seconds, onTimeout }) => {
|
|
|
|
- const [remaining] = useCountdown(seconds, () => onTimeout());
|
|
|
|
|
|
+const useRoundTimer = onTimeout => {
|
|
|
|
+ const remaining = useRoundSeconds();
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (remaining > 0) {
|
|
if (remaining > 0) {
|
|
- saveTimerToLocalStorage(remaining);
|
|
|
|
|
|
+ const timeout = setTimeout(() => { dispatch.setRoundSeconds(remaining - 1); }, 1000);
|
|
|
|
+ return () => clearTimeout(timeout);
|
|
}
|
|
}
|
|
}, [remaining]);
|
|
}, [remaining]);
|
|
-
|
|
|
|
- if (remaining > 0) {
|
|
|
|
- return <TimerSpan>Time: {ms(remaining * 1000)}</TimerSpan>;
|
|
|
|
- }
|
|
|
|
|
|
+ const [called, setCalled] = useState(false);
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (!called && remaining <= 0) {
|
|
|
|
+ onTimeout();
|
|
|
|
+ setCalled(true);
|
|
|
|
+ }
|
|
|
|
+ }, [onTimeout, remaining, called]);
|
|
|
|
+ return remaining;
|
|
|
|
+}
|
|
|
|
|
|
- return <TimedOut>Time's up!</TimedOut>;
|
|
|
|
|
|
+export default ({ onTimeout }) => {
|
|
|
|
+ const remaining = useRoundTimer(onTimeout);
|
|
|
|
+ return (
|
|
|
|
+ remaining > 0
|
|
|
|
+ ? <TimerSpan>Time: {ms(remaining * 1000)}</TimerSpan>
|
|
|
|
+ : <TimedOut>Time's up!</TimedOut>
|
|
|
|
+ );
|
|
}
|
|
}
|