aboutsummaryrefslogtreecommitdiff
path: root/src/pages/Timer/TimerButton.tsx
blob: 0a3bf388eb9a4eca48d5b34b1ae4ed34e4d8a44c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 (): void => clearInterval(repeater);
    }

    if (mode === 'running') {
      const repeater = setInterval(() => {
        setTime(convertTimeToString(Date.now() - timestamp));
      }, 10);
      return (): void => 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 (): void => {
      window.removeEventListener('keyup', handleKeyUp);
      window.removeEventListener('keypress', handleKeyPress);
    };
  });

  const composeHelperText = (): string => {
    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 = (): 'primary' | 'secondary' | 'textSecondary' => {
    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;