This commit is contained in:
cv
2021-04-10 15:30:32 +02:00
parent 7293b0dfd9
commit a65f816477
4 changed files with 30 additions and 19 deletions
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import styles from './Countdown.module.css';
function display(seconds) {
function formatDisplay(seconds) {
const format = (val) => `0${Math.floor(val)}`.slice(-2);
const hours = seconds / 3600;
const minutes = (seconds % 3600) / 60;
@@ -8,10 +9,24 @@ function display(seconds) {
return [hours, minutes, seconds % 60].map(format).join(':');
}
export default function Countdown({ time, small }) {
export default function Countdown(props) {
const { time, small } = props;
const [clock, setClock] = useState(time);
let display = '-- : -- : --';
console.log('websocket: time and t', time);
useEffect(() => {
setClock(time);
console.log('websocket: time changed', time)
}, [time]);
// prepare display string
if (clock != null && !isNaN(clock)) display = formatDisplay(clock);
return (
<div className={small ? styles.countdownClockSmall : styles.countdownClock}>
{(time === null || isNaN(time)) ? '-- : -- : --' : display(time)}
{display}
</div>
);
}