-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathTimer.final.js
67 lines (63 loc) · 1.78 KB
/
Timer.final.js
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
import * as React from 'react';
import { useReducer } from 'react';
import { faPlay, faPause, faStop } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { timerMachine, timerMachineConfig } from './timerMachine.final';
import { ProgressCircle } from '../ProgressCircle';
export const Timer = () => {
const [state, dispatch] = useReducer(
timerMachine,
timerMachineConfig.initial
);
const { duration, elapsed, interval } = {
duration: 60,
elapsed: 0,
interval: 0.1,
};
return (
<div
className="timer"
data-state={state}
style={{
// @ts-ignore
'--duration': duration,
'--elapsed': elapsed,
'--interval': interval,
}}
>
<header>
<h1>Exercise 00 Solution</h1>
</header>
<ProgressCircle />
<div className="display">
<div className="label">{state}</div>
<div className="elapsed" onClick={() => dispatch({ type: 'TOGGLE' })}>
{Math.ceil(duration - elapsed)}
</div>
<div className="controls">
{state === 'paused' && (
<button onClick={() => dispatch({ type: 'RESET' })}>Reset</button>
)}
</div>
</div>
<div className="actions">
{state === 'running' && (
<button
onClick={() => dispatch({ type: 'TOGGLE' })}
title="Pause timer"
>
<FontAwesomeIcon icon={faPause} />
</button>
)}
{(state === 'paused' || state === 'idle') && (
<button
onClick={() => dispatch({ type: 'TOGGLE' })}
title="Start timer"
>
<FontAwesomeIcon icon={faPlay} />
</button>
)}
</div>
</div>
);
};