aboutsummaryrefslogtreecommitdiff
path: root/src/pages/Timer/TimerButton.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-03-21 16:04:26 +0300
committereug-vs <eug-vs@keemail.me>2020-03-21 16:04:26 +0300
commit9e2132cd54e5f5e6b85c7d949ac982cb95566027 (patch)
tree662da2d31d9716d09cde9754a359be47227da41e /src/pages/Timer/TimerButton.tsx
parent146947a665dbc1d2960d2062a22a106de0c71062 (diff)
downloadchrono-cube-ui-9e2132cd54e5f5e6b85c7d949ac982cb95566027.tar.gz
chore: migrate Timer page to Typescript :label:
Diffstat (limited to 'src/pages/Timer/TimerButton.tsx')
-rw-r--r--src/pages/Timer/TimerButton.tsx130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/pages/Timer/TimerButton.tsx b/src/pages/Timer/TimerButton.tsx
new file mode 100644
index 0000000..86bbf76
--- /dev/null
+++ b/src/pages/Timer/TimerButton.tsx
@@ -0,0 +1,130 @@
+import React, { useState, useEffect } from 'react';
+
+import { Paper, Typography } from '@material-ui/core';
+import { makeStyles } from '@material-ui/core/styles';
+
+const useStyles = makeStyles(theme => ({
+ root: {
+ textAlign: 'center',
+ padding: theme.spacing(5),
+ background: theme.palette.background.elevation3,
+ marginTop: theme.spacing(10),
+ },
+}));
+
+
+interface PropTypes {
+ registerResult: (result: string) => void;
+}
+
+type Mode = 'idle' | 'countdown' | 'running' | 'over';
+
+
+const TimerButton: React.FC<PropTypes> = ({ registerResult }) => {
+ const classes = useStyles();
+
+ const SPACE = 32;
+ const maxCountdown = 15000;
+ const [time, setTime] = useState<string>('00:00:00');
+ const [mode, setMode] = useState<Mode>('idle');
+
+ useEffect(()=> {
+ const timestamp = Date.now();
+
+ if (mode === 'countdown') {
+ const repeater = setInterval(() => {
+ const timeDelta = maxCountdown - (Date.now() - timestamp);
+ if (timeDelta <= 0) setMode('over');
+ setTime(convertTimeToString(timeDelta));
+ }, 10);
+ return () => clearInterval(repeater);
+ }
+
+ if (mode === 'running') {
+ const repeater = setInterval(() => {
+ setTime(convertTimeToString(Date.now() - timestamp));
+ }, 10);
+ return () => clearInterval(repeater);
+ }
+
+ if (mode === 'over') {
+ setTime('00:00:00');
+ }
+ }, [mode]);
+
+ const handleKeyPress = (event: KeyboardEvent): void => {
+ event.preventDefault();
+ if (event.keyCode === SPACE && mode === 'idle' ) setMode('countdown');
+ };
+
+ const handleKeyUp = (event: KeyboardEvent): void => {
+ if (event.keyCode === SPACE) {
+ if (mode === 'running') {
+ registerResult(time);
+ setMode('idle');
+ } else if (mode === 'over') {
+ setMode('idle');
+ } else {
+ setMode('running');
+ }
+ }
+ };
+
+ useEffect(() => {
+ window.addEventListener('keyup', handleKeyUp);
+ window.addEventListener('keypress', handleKeyPress);
+
+ return () => {
+ window.removeEventListener('keyup', handleKeyUp);
+ window.removeEventListener('keypress', handleKeyPress);
+ };
+ });
+
+ const composeHelperText = () => {
+ switch (mode) {
+ case 'running': return 'Go fast!';
+ case 'countdown': return 'Release SPACE to begin';
+ case 'over': return 'You are too late!';
+ default: return 'Hold SPACE to start countdown';
+ }
+ };
+
+ const helperColor = () => {
+ switch (mode) {
+ case 'running': return 'primary';
+ case 'over': return 'secondary';
+ default: return 'textSecondary';
+ }
+ };
+
+ return (
+ <Paper className={classes.root}>
+ <Typography variant="h1"> {time} </Typography>
+ <Typography variant="h5" color={helperColor()}>
+ {composeHelperText()}
+ </Typography>
+ </Paper>
+ );
+};
+
+const convertTimeToString = (timeDelta: number): string => {
+ let resultTime = '';
+
+ const minute = Math.floor(timeDelta / 60000);
+ if (minute < 10) resultTime += '0';
+ resultTime += minute + ':';
+
+ let second = Math.floor(timeDelta / 1000);
+ if (second > 59) second %= 60;
+ if (second < 10) resultTime += '0';
+ resultTime += second + ':';
+
+ const mill = Math.floor((timeDelta % 1000) / 10);
+ if (mill < 10) resultTime += '0';
+ resultTime += mill;
+
+ return resultTime;
+};
+
+
+export default TimerButton;